在 pytest 的报告中用 space 替换破折号
replace dash by space in pytest's report
我正在通过pytest
通过堆叠参数
来简化一组测试
import pytest
@pytest.mark.parametrize("option_a", ["-r", "-s"])
@pytest.mark.parametrize("option_b", ["--alpha", "--beta", "--gamma"])
def test_foo(option_a, option_b):
assert option_a in ["-r", "-s"]
正如预期的那样,上述 MWE 执行了详尽的排列:
======================================= test session starts =======================================
platform linux -- Python 3.9.7, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /home/guest/Desktop/project, configfile: pytest.ini
collected 6 items
test_ping.py::test_foo[--alpha--r] PASSED [ 16%]
test_ping.py::test_foo[--alpha--s] PASSED [ 33%]
test_ping.py::test_foo[--beta--r] PASSED [ 50%]
test_ping.py::test_foo[--beta--s] PASSED [ 66%]
test_ping.py::test_foo[--gamma--r] PASSED [ 83%]
test_ping.py::test_foo[--gamma--s] PASSED [100%]
======================================== 6 passed in 0.02s ========================================
让我有点困扰的是第一个参数后面的附加破折号(--alpha
、--beta
、--gamma
)。对 pytest
不够熟悉的人可能会认为测试的第二个参数是 --r
(而不是 -r
)或 --s
(而不是 -s
)。
是否有一种实用的方法可以用空格替换此分隔符 space?由于这只是 CLI 输出格式的问题,我不想为每个测试函数定义多个固定装置。
为了总结我们的评论,可以使用 ids 并记住测试顺序。 pytest
将使用层数较高的参数作为外层循环(此处:option_b
),层数较少的参数作为内层循环(此处:option_a
)。此外,我添加了 custom_format
函数作为如何自定义参数视图的方法示例。
import pytest
@pytest.mark.parametrize("option_a", ["-r", "-s"], ids=[" -r", " -s"])
@pytest.mark.parametrize("option_b", ["--alpha", "--beta", "--gamma"], ids=["--alpha ", "--beta ", "--gamma "])
def test_foo(option_a, option_b):
assert option_a in ["-r", "-s"]
def custom_format(val):
return f" {val} "
@pytest.mark.parametrize("option_a", ["-r", "-s"], ids=custom_format)
@pytest.mark.parametrize("option_b", ["--alpha", "--beta", "--gamma"], ids=custom_format)
def test_foo_1(option_a, option_b):
assert option_a in ["-r", "-s"]
它不会删除参数之间的连字符,但可以减少混淆。
collected 12 items
test_.py::test_foo[--alpha - -r] PASSED [ 8%]
test_.py::test_foo[--alpha - -s] PASSED [ 16%]
test_.py::test_foo[--beta - -r] PASSED [ 25%]
test_.py::test_foo[--beta - -s] PASSED [ 33%]
test_.py::test_foo[--gamma - -r] PASSED [ 41%]
test_.py::test_foo[--gamma - -s] PASSED [ 50%]
test_.py::test_foo_1[ --alpha - -r ] PASSED [ 58%]
test_.py::test_foo_1[ --alpha - -s ] PASSED [ 66%]
test_.py::test_foo_1[ --beta - -r ] PASSED [ 75%]
test_.py::test_foo_1[ --beta - -s ] PASSED [ 83%]
test_.py::test_foo_1[ --gamma - -r ] PASSED [ 91%]
test_.py::test_foo_1[ --gamma - -s ] PASSED [100%]
我正在通过pytest
通过堆叠参数
import pytest
@pytest.mark.parametrize("option_a", ["-r", "-s"])
@pytest.mark.parametrize("option_b", ["--alpha", "--beta", "--gamma"])
def test_foo(option_a, option_b):
assert option_a in ["-r", "-s"]
正如预期的那样,上述 MWE 执行了详尽的排列:
======================================= test session starts =======================================
platform linux -- Python 3.9.7, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /home/guest/Desktop/project, configfile: pytest.ini
collected 6 items
test_ping.py::test_foo[--alpha--r] PASSED [ 16%]
test_ping.py::test_foo[--alpha--s] PASSED [ 33%]
test_ping.py::test_foo[--beta--r] PASSED [ 50%]
test_ping.py::test_foo[--beta--s] PASSED [ 66%]
test_ping.py::test_foo[--gamma--r] PASSED [ 83%]
test_ping.py::test_foo[--gamma--s] PASSED [100%]
======================================== 6 passed in 0.02s ========================================
让我有点困扰的是第一个参数后面的附加破折号(--alpha
、--beta
、--gamma
)。对 pytest
不够熟悉的人可能会认为测试的第二个参数是 --r
(而不是 -r
)或 --s
(而不是 -s
)。
是否有一种实用的方法可以用空格替换此分隔符 space?由于这只是 CLI 输出格式的问题,我不想为每个测试函数定义多个固定装置。
为了总结我们的评论,可以使用 ids 并记住测试顺序。 pytest
将使用层数较高的参数作为外层循环(此处:option_b
),层数较少的参数作为内层循环(此处:option_a
)。此外,我添加了 custom_format
函数作为如何自定义参数视图的方法示例。
import pytest
@pytest.mark.parametrize("option_a", ["-r", "-s"], ids=[" -r", " -s"])
@pytest.mark.parametrize("option_b", ["--alpha", "--beta", "--gamma"], ids=["--alpha ", "--beta ", "--gamma "])
def test_foo(option_a, option_b):
assert option_a in ["-r", "-s"]
def custom_format(val):
return f" {val} "
@pytest.mark.parametrize("option_a", ["-r", "-s"], ids=custom_format)
@pytest.mark.parametrize("option_b", ["--alpha", "--beta", "--gamma"], ids=custom_format)
def test_foo_1(option_a, option_b):
assert option_a in ["-r", "-s"]
它不会删除参数之间的连字符,但可以减少混淆。
collected 12 items
test_.py::test_foo[--alpha - -r] PASSED [ 8%]
test_.py::test_foo[--alpha - -s] PASSED [ 16%]
test_.py::test_foo[--beta - -r] PASSED [ 25%]
test_.py::test_foo[--beta - -s] PASSED [ 33%]
test_.py::test_foo[--gamma - -r] PASSED [ 41%]
test_.py::test_foo[--gamma - -s] PASSED [ 50%]
test_.py::test_foo_1[ --alpha - -r ] PASSED [ 58%]
test_.py::test_foo_1[ --alpha - -s ] PASSED [ 66%]
test_.py::test_foo_1[ --beta - -r ] PASSED [ 75%]
test_.py::test_foo_1[ --beta - -s ] PASSED [ 83%]
test_.py::test_foo_1[ --gamma - -r ] PASSED [ 91%]
test_.py::test_foo_1[ --gamma - -s ] PASSED [100%]