PyCharm 2022.2.4为何误将普通函数识别为测试用例?
PyCharm升级后自动用pytest运行非规范测试文件报错问题
问题详情
我有一个名为1_selection_sort.py的脚本,里面定义了test_selection_sort函数用来验证选择排序逻辑,代码如下:
""" Schrijf een functie selection_sort dat een lijst in dalende volgorde sorteert m.b.v. selection sort. """ def selection_sort(lijst): for i in range(len(lijst)): for j, number in enumerate(lijst): if number < lijst[i]: lijst[j] = lijst[i] lijst[i] = number return lijst def test_selection_sort(lijst, check): print(lijst) result = selection_sort(lijst) print(result) print(check) assert result == check print("Begin controle selection_sort") test_selection_sort([1, 3, 45, 32, 65, 34], [65, 45, 34, 32, 3, 1]) test_selection_sort([1], [1]) test_selection_sort([54, 29, 12, 92, 2, 100], [100, 92, 54, 29, 12, 2]) test_selection_sort([], []) print("Controle selection_sort succesvol")
在PyCharm 2022.2.2和2022.2.3版本中,以<current file>方式运行脚本完全正常,但升级到2022.2.4版本后,PyCharm自动调用pytest运行该函数,出现以下错误:
"C:\Program Files\Anaconda3\python.exe" "C:/Users/r0944584/AppData/Local/JetBrains/PyCharm Community Edition 2022.2.4/plugins/python-ce/helpers/pycharm/_jb_pytest_runner.py" --path "C:\Users\r0944584\Downloads\skeletons(4)\skeletons\1_selection_sort.py" Testing started at 14:13 ... Launching pytest with arguments C:\Users\r0944584\Downloads\skeletons(4)\skeletons\1_selection_sort.py --no-header --no-summary -q in C:\Users\r0944584\Downloads\skeletons(4)\skeletons ============================= test session starts ============================= collecting ... collected 1 item 1_selection_sort.py::test_selection_sort ERROR [100%] test setup failed file C:\Users\r0944584\Downloads\skeletons(4)\skeletons\1_selection_sort.py, line 15 def test_selection_sort(lijst, check): E fixture 'lijst' not found > available fixtures: anyio_backend, anyio_backend_name, anyio_backend_options, cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory > use 'pytest --fixtures [testpath]' for help on them. C:\Users\r0944584\Downloads\skeletons(4)\skeletons\1_selection_sort.py:15 ========================= 1 warning, 1 error in 0.01s ========================= Process finished with exit code 1
我确认脚本文件名没有遵循pytest的测试文件命名规范(test_*.py或*_test.py),想知道这是版本变更的原因,还是我安装操作有误?
原因与解决办法
这不是安装错误,是PyCharm 2022.2.4版本对测试用例自动检测逻辑的调整:即使文件名不符合pytest的命名规范,只要文件内存在以test_开头的函数,PyCharm就会默认调用pytest作为运行器,而不是直接执行整个脚本。
pytest会把测试函数的参数识别为需要注入的fixture,而你的test_selection_sort函数参数是自定义的测试数据,pytest找不到对应的fixture,所以报错。
可以通过以下几种方式解决:
- 修改运行配置:点击运行按钮旁的配置下拉框,选择「Edit Configurations」,添加新的「Python」运行配置,指定该脚本路径,之后用这个配置运行即可恢复直接执行脚本的行为。
- 重命名测试函数:把
test_selection_sort改成不以test_开头的名称,比如verify_selection_sort,这样PyCharm不会把它识别为pytest测试用例,会直接执行脚本。 - 适配pytest规范:如果想继续用pytest运行,需要调整测试函数格式,比如用pytest的参数化装饰器传递测试数据,或者把测试数据写在函数内部,示例:
import pytest @pytest.mark.parametrize("lijst, check", [ ([1, 3, 45, 32, 65, 34], [65, 45, 34, 32, 3, 1]), ([1], [1]), ([54, 29, 12, 92, 2, 100], [100, 92, 54, 29, 12, 2]), ([], []) ]) def test_selection_sort(lijst, check): result = selection_sort(lijst) assert result == check
内容的提问来源于stack exchange,提问作者Nice Zombies
相关产品推荐
相关产品推荐

