gpt4 book ai didi

python - 使用 side_effects 修补 os.path.isfile 在列表理解中不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 18:58:33 26 4
gpt4 key购买 nike

我正在尝试在 Python 类中练习以下方法:

def find_tests(self):
tests_dir = glob.glob("tests/*")
if not tests_dir:
return None

# Filter out only the files in tests/ then get the test names.
tests = [os.path.basename(t) for t in tests_dir if os.path.isfile(t)]

通过以下测试:

@patch('glob.glob')
@patch('os.path.isfile')
def test_conductor_find_tests(self, mock_isfile, mock_glob):
tests_directory = ['tests/00-test', 'tests/02-juju', 'tests/lib',
'tests/01-ubuntu']
test_names = ['00-test', '01-ubuntu', '02-juju']
files_exist = [True, True, False, True]
mock_isfile.side_effects = files_exist
mock_glob.return_value = tests_directory

args = Arguments(tests = 'dummy')
c = juju_test.Conductor(args)
results = c.find_tests()

mock_glob.assert_called_with('tests/*')
self.assertEqual(results, test_names)

mock_isfile 运行(使用 mock_isfile.call_count 验证),但是,它不会为第三个列表元素返回 False,而是让所有元素飞过,我最终得到以下结果:

======================================================================
FAIL: test_conductor_find_tests (test.test_juju_test.JujuTestPluginTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/mock.py", line 1201, in patched
return func(*args, **keywargs)
File "/home/marco/Projects/juju-plugins/test/test_juju_test.py", line 51, in test_conductor_find_tests
self.assertEqual(results, test_names)
AssertionError: Lists differ: ['00-test', '01-ubuntu', '02-j... != ['00-test', '01-ubuntu', '02-j...

First list contains 1 additional elements.
First extra element 3:
lib

- ['00-test', '01-ubuntu', '02-juju', 'lib']
? -------

+ ['00-test', '01-ubuntu', '02-juju']

----------------------------------------------------------------------

我需要做什么才能正确获取 os.path.isfile 补丁以尊重这些返回值?

最佳答案

该属性称为 side_effect,而不是 side_effects

关于python - 使用 side_effects 修补 os.path.isfile 在列表理解中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16651553/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com