当前正在尝试运行pytest -k表达式,该表达式将运行多个参数化测试。
例如,这运行得很好:
$ pytest tools/test_pinger_tracer.py --collect-only
=============================================== test session starts ========================================
platform linux -- Python 3.7.4, pytest-3.7.0, py-1.8.0, pluggy-0.13.0
rootdir: /home/as2863/pythonProjects/p1-automation, inifile: pytest.ini
plugins: csv-2.0.1, check-0.3.5, pylama-7.6.6, dependency-0.4.0, instafail-0.4.0, ordering-0.6, repeat-0.7.0, reportportal-5.0.3
collected 4 items
<Package '/home/as2863/pythonProjects/p1-automation/tools'>
<Module 'test_pinger_tracer.py'>
<Function 'test_pinger[ping-pinger_topoA_L0_L10.yml]'>
<Function 'test_tracer[traceroute-pinger_topoA_L0_L10.yml]'>
<Function 'test_tracer[traceroute-mplstracer_edgetopoA_L0_L10.yml]'>
<Function 'test_tracer[mpls traceroute-mplstracer_edgetopoA_L0_L10.yml]'>
================================================ no tests ran in 0.01 seconds ==============================如果我只想使用-k选项运行一个测试,它可以很好地工作:
pytest tools/test_pinger_tracer.py --collect-only -k "test_pinger[ping-pinger_topoA_L0_L10.yml]"
============================================== test session starts ===========================================
platform linux -- Python 3.7.4, pytest-3.7.0, py-1.8.0, pluggy-0.13.0
rootdir: /home/as2863/pythonProjects/p1-automation, inifile: pytest.ini
plugins: csv-2.0.1, check-0.3.5, pylama-7.6.6, dependency-0.4.0, instafail-0.4.0, ordering-0.6, repeat-0.7.0, reportportal-5.0.3
collected 4 items / 3 deselected
<Package '/home/as2863/pythonProjects/p1-automation/tools'>
<Module 'test_pinger_tracer.py'>
<Function 'test_pinger[ping-pinger_topoA_L0_L10.yml]'>
================================ 3 deselected in 0.01 seconds ==================但是,如果我尝试在-k表达式中使用"or“语句来运行两个测试,pytest崩溃...
$ pytest tools/test_pinger_tracer.py --collect-only -k "test_pinger[ping-pinger_topoA_L0_L10.yml] or test_tracer[traceroute-mplstracer_edgetopoA_L0_L10.yml]"
================================ test session starts ======================================
platform linux -- Python 3.7.4, pytest-3.7.0, py-1.8.0, pluggy-0.13.0
rootdir: /home/as2863/pythonProjects/p1-automation, inifile: pytest.ini
plugins: csv-2.0.1, check-0.3.5, pylama-7.6.6, dependency-0.4.0, instafail-0.4.0, ordering-0.6, repeat-0.7.0, reportportal-5.0.3
collecting 4 items <Package '/home/as2863/pythonProjects/p1-automation/tools'>
<Module 'test_pinger_tracer.py'>
<Function 'test_pinger[ping-pinger_topoA_L0_L10.yml]'>
<Function 'test_tracer[traceroute-pinger_topoA_L0_L10.yml]'>
<Function 'test_tracer[traceroute-mplstracer_edgetopoA_L0_L10.yml]'>
<Function 'test_tracer[mpls traceroute-mplstracer_edgetopoA_L0_L10.yml]'>
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR> File "/home/as2863/python-venv/p1_netmiko_exper4/lib/python3.7/site-packages/_pytest/main.py", line 178, in wrap_session
INTERNALERROR> session.exitstatus = doit(config, session) or 0
INTERNALERROR> File "/home/as2863/python-venv/p1_netmiko_exper4/lib/python3.7/site-packages/_pytest/main.py", line 214, in _main
INTERNALERROR> config.hook.pytest_collection(session=session)
INTERNALERROR> File "/home/as2863/python-venv/p1_netmiko_exper4/lib/python3.7/site-packages/pluggy/hooks.py", line 286, in __call__
INTERNALERROR> return self._hookexec(self, self.get_hookimpls(), kwargs)
INTERNALERROR> File "/home/as2863/python-venv/p1_netmiko_exper4/lib/python3.7/site-packages/pluggy/manager.py", line 92, in _hookexec
INTERNALERROR> return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR> File "/home/as2863/python-venv/p1_netmiko_exper4/lib/python3.7/site-packages/pluggy/manager.py", line 86, in <lambda>
INTERNALERROR> firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
INTERNALERROR> File "/home/as2863/python-venv/p1_netmiko_exper4/lib/python3.7/site-packages/pluggy/callers.py", line 208, in _multicall
INTERNALERROR> return outcome.get_result()
INTERNALERROR> File "/home/as2863/python-venv/p1_netmiko_exper4/lib/python3.7/site-packages/pluggy/callers.py", line 80, in get_result
INTERNALERROR> raise ex[1].with_traceback(ex[2])
INTERNALERROR> File "/home/as2863/python-venv/p1_netmiko_exper4/lib/python3.7/site-packages/pluggy/callers.py", line 187, in _multicall
INTERNALERROR> res = hook_impl.function(*args)
INTERNALERROR> File "/home/as2863/python-venv/p1_netmiko_exper4/lib/python3.7/site-packages/_pytest/main.py", line 224, in pytest_collection
INTERNALERROR> return session.perform_collect()
INTERNALERROR> File "/home/as2863/python-venv/p1_netmiko_exper4/lib/python3.7/site-packages/_pytest/main.py", line 431, in perform_collect
INTERNALERROR> session=self, config=self.config, items=items
INTERNALERROR> File "/home/as2863/python-venv/p1_netmiko_exper4/lib/python3.7/site-packages/pluggy/hooks.py", line 286, in __call__
INTERNALERROR> return self._hookexec(self, self.get_hookimpls(), kwargs)
INTERNALERROR> File "/home/as2863/python-venv/p1_netmiko_exper4/lib/python3.7/site-packages/pluggy/manager.py", line 92, in _hookexec
INTERNALERROR> return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR> File "/home/as2863/python-venv/p1_netmiko_exper4/lib/python3.7/site-packages/pluggy/manager.py", line 86, in <lambda>
INTERNALERROR> firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
INTERNALERROR> File "/home/as2863/python-venv/p1_netmiko_exper4/lib/python3.7/site-packages/pluggy/callers.py", line 208, in _multicall
INTERNALERROR> return outcome.get_result()
INTERNALERROR> File "/home/as2863/python-venv/p1_netmiko_exper4/lib/python3.7/site-packages/pluggy/callers.py", line 80, in get_result
INTERNALERROR> raise ex[1].with_traceback(ex[2])
INTERNALERROR> File "/home/as2863/python-venv/p1_netmiko_exper4/lib/python3.7/site-packages/pluggy/callers.py", line 187, in _multicall
INTERNALERROR> res = hook_impl.function(*args)
INTERNALERROR> File "/home/as2863/python-venv/p1_netmiko_exper4/lib/python3.7/site-packages/_pytest/mark/__init__.py", line 155, in pytest_collection_modifyitems
INTERNALERROR> deselect_by_keyword(items, config)
INTERNALERROR> File "/home/as2863/python-venv/p1_netmiko_exper4/lib/python3.7/site-packages/_pytest/mark/__init__.py", line 124, in deselect_by_keyword
INTERNALERROR> if keywordexpr and not matchkeyword(colitem, keywordexpr):
INTERNALERROR> File "/home/as2863/python-venv/p1_netmiko_exper4/lib/python3.7/site-packages/_pytest/mark/legacy.py", line 95, in matchkeyword
INTERNALERROR> return eval(keywordexpr, {}, mapping)
INTERNALERROR> File "<string>", line 1, in <module>
INTERNALERROR> AttributeError: 'bool' object has no attribute 'yml'
====================================== no tests ran in 0.02 seconds==================================================注意,如果我从字符串表达式中去掉“方括号”和“句点”,那么它不会崩溃,但也不会匹配任何内容:
$ pytest tools/test_pinger_tracer.py --collect-only -k "test_pingerping-pinger_topoA_L0_L10yml or test_tracertraceroute-mplstracer_edgetopoA_L0_L10yml"
================================================================================================================= test session starts ==================================================================================================================
platform linux -- Python 3.7.4, pytest-3.7.0, py-1.8.0, pluggy-0.13.0
rootdir: /home/as2863/pythonProjects/p1-automation, inifile: pytest.ini
plugins: csv-2.0.1, check-0.3.5, pylama-7.6.6, dependency-0.4.0, instafail-0.4.0, ordering-0.6, repeat-0.7.0, reportportal-5.0.3
collected 4 items / 4 deselected
====================================== 4 deselected in 0.01 seconds ==================================================================对于能够选择多个参数化的pytest运行,有什么建议吗?Thx铝
发布于 2020-07-31 05:38:38
把我的评论变成一个答案,因为pytest==6.0昨天发布了:
在版本6.0中,通过引入新的-k参数解析器,修复了此问题。删除了evaling -k参数作为表达式的旧行为。任何对实际实现感兴趣的人都应该看看#7122's diff。
https://stackoverflow.com/questions/63080032
复制相似问题