首页 > 解决方案 > pytest:一次执行多个测试

问题描述

我想一次执行多个测试,但得到一个 nastygram:

pytest tests -k '1.1.18 or 4.1.12 or 6.1.11 or 6.1.12' ...

...

ERROR: Wrong expression passed to '-k': 1.1.18 or 4.1.12 or 6.1.11 or 6.1.12

pytest tests -k 1.1.18 ...可以很好地执行一项测试。我的团队使用特定于操作系统发行版的套件文件构建了我们的测试,并将测试编号与脚本和方法相关联,如下所示:

tests:
- test_function: "foo.py:test_bar1"
  test_id: "1.1.1.1"
- test_function: "foo.py:test_bar2"
  test_id: "1.2"
...

我究竟做错了什么?

标签: pytest

解决方案


首先pip install pytest-xdist 然后使用pytest -n NUM其中 NUM 是您要并行运行的数字的整数。

所以要并行运行3。

pytest -n3

或者

pytest -n 3

文档链接: https ://docs.pytest.org/en/3.0.1/xdist.html

编辑


-k接受测试函数名称。你希望它们是独一无二的。

tests:
- test_function1: "foo.py:test_bar1"
  test_id: "1.1.1.1"
- test_function2: "foo.py:test_bar2"
  test_id: "1.2"
...

pytest -k "test_function1 or test_function1"

注意在 windows cmd 中你必须使用"not'


推荐阅读