使用pytest --collect-only仅返回单个测试名称

Lom*_*bax 4 python testing pytest

我试图解决如何使用pytest从测试文件中获取测试名称的列表我已经做到这一点:

$ pytest --collect-only my_test_file.py
============================================================================================= test session starts ==============================================================================================
platform darwin -- Python 3.6.4, pytest-3.2.5, py-1.5.2, pluggy-0.4.0
rootdir: /Users/.../automation, inifile:
plugins: xdist-1.20.1, metadata-1.7.0, html-1.17.0, forked-0.2, cloud-2.0.0
collected 6 items
<Module 'my_test_file.py'>
  <UnitTestCase 'TestFile'>
    <TestCaseFunction 'test_general'>
    <TestCaseFunction 'test_search_0'>
    <TestCaseFunction 'test_search_1'>
    <TestCaseFunction 'test_search_2'>
    <TestCaseFunction 'test_search_3'>
    <TestCaseFunction 'test_search_4'>
Run Code Online (Sandbox Code Playgroud)

虽然这是一个良好的开端,但它是太多的信息.我应该通过什么来获取测试名称本身的列表?

hoe*_*ing 8

使用--quiet改变集合输出.

$ pytest --collect-only -q
test_eggs.py::test_bacon[1]
test_eggs.py::test_bacon[2]
test_eggs.py::test_bacon[3]
test_spam.py::test_foo
test_spam.py::test_bar

no tests ran in 0.03 seconds
Run Code Online (Sandbox Code Playgroud)

您可以使用剥离状态行

$ pytest --collect-only -q | head -n -2
Run Code Online (Sandbox Code Playgroud)

使用两次(或更多次)时,--quiet将打印每个模块的测试数量:

$ pytest --collect-only -qq
test_eggs.py: 3
test_spam.py: 2
Run Code Online (Sandbox Code Playgroud)

  • 正确的答案,尽管这很奇怪,并且我认为这是 pytest 的设计缺陷。为什么我应该提供 `--quiet` 才能列出我的测试的全名 - 这是没有意义的。 (3认同)