繁体   English   中英

pytest -k 表达式在尝试匹配多个参数化测试时失败

[英]pytest -k expression failing when trying to match multiple parameterized tests

目前正在尝试使 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 表达式中使用“或”语句来运行两个测试,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 ==================================================================

关于能够运行 select 多个参数化 pytest 的任何建议? 谢谢铝

将我的评论变成答案,因为pytest==6.0昨天发布:

这已在 6.0 版中修复,并为-k参数引入了新的解析器。 eval ing -k参数作为表达式的旧行为已被删除。 任何对实际实现感兴趣的人都应该看看#7122 的 diff

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM