首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试匹配多个参数化测试时,pytest -k表达式失败

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

Stack Overflow用户
提问于 2020-07-25 03:06:52
回答 1查看 288关注 0票数 2

当前正在尝试运行pytest -k表达式,该表达式将运行多个参数化测试。

例如,这运行得很好:

代码语言:javascript
复制
$ 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选项运行一个测试,它可以很好地工作:

代码语言:javascript
复制
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崩溃...

代码语言:javascript
复制
$ 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==================================================

注意,如果我从字符串表达式中去掉“方括号”和“句点”,那么它不会崩溃,但也不会匹配任何内容:

代码语言:javascript
复制
$ 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铝

EN

回答 1

Stack Overflow用户

发布于 2020-07-31 05:38:38

把我的评论变成一个答案,因为pytest==6.0昨天发布了:

在版本6.0中,通过引入新的-k参数解析器,修复了此问题。删除了evaling -k参数作为表达式的旧行为。任何对实际实现感兴趣的人都应该看看#7122's diff

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63080032

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档