[英]Pytest: Getting addresses of all tests
When I run pytest --collect-only
to get the list of my tests, I get them in a format like <Function: test_whatever>
. 当我运行
pytest --collect-only
来获取我的测试列表时,我会以<Function: test_whatever>
类的格式获取它们。 However, when I use pytest -k ...
to run a specific test, I need to input the "address" of the test in the format foo::test_whatever
. 但是,当我使用
pytest -k ...
运行特定测试时,我需要以foo::test_whatever
格式输入测试的“地址”。 Is it possible to get a list of all the addresses of all the tests in the same format that -k
takes? 是否有可能以
-k
采用的相同格式获取所有测试的所有地址列表?
The usage isn't as you specify it. 用法不是您指定的用法。 From the documentation: http://doc.pytest.org/en/latest/usage.html
来自文档: http : //doc.pytest.org/en/latest/usage.html
pytest -k stringexpr # only run tests with names that match the
# "string expression", e.g. "MyClass and not method"
# will select TestMyClass.test_something
# but not TestMyClass.test_method_simple
so what you need to pass to '-k' is a string contained in all the callable functions you want to check (you can use logical operator between these strings). 所以你需要传递给'-k'的是你要检查的所有可调用函数中包含的字符串(你可以在这些字符串之间使用逻辑运算符)。 For your example (assuming all defs are prefixed by a
foo::
: 对于你的例子(假设所有defs都以
foo::
为前缀:
pytest -k "foo::"
In conftest.py, you can override the 'collection' hooks to print information about collected test 'items'. 在conftest.py中,您可以覆盖“集合”挂钩以打印有关收集的测试“项目”的信息。
You may introduce your own command line option (like --collect-only). 您可以引入自己的命令行选项(如--collect-only)。 If this option is specified, print the test items (in whichever way you like) and exit.
如果指定了此选项,则打印测试项目(以您喜欢的任何方式)并退出。
Sample conftest.py below (tested locally): 下面的示例conftest.py(在本地测试):
import pytest
def pytest_addoption(parser):
parser.addoption("--my_test_dump", action="store", default=None,
help="Print test items in my custom format")
def pytest_collection_finish(session):
if session.config.option.my_test_dump is not None:
for item in session.items:
print('{}::{}'.format(item.fspath, item.name))
pytest.exit('Done!')
For more information on pytest hooks, see: 有关pytest钩子的更多信息,请参阅:
http://doc.pytest.org/en/latest/_modules/_pytest/hookspec.html http://doc.pytest.org/en/latest/_modules/_pytest/hookspec.html
If you are using -k switch, you don't need to specify the full path separated by colons. 如果使用-k开关,则无需指定以冒号分隔的完整路径。 If the path is unique, you can use just part of the path.
如果路径是唯一的,则只能使用路径的一部分。 You need full path of tests only when you are not using -k switch.
只有在不使用-k开关时才需要完整的测试路径。
eg 例如
pytest -k "unique_part_of_path_name"
for pytest tests/a/b/c.py::test_x
, you can use pytest -k "a and b and c and x"
. 对于
pytest tests/a/b/c.py::test_x
,你可以使用pytest -k "a and b and c and x"
。
You can use boolean logic for -k switch. 您可以对-k开关使用布尔逻辑。
BTW, pytest --collect-only
does give the file name of the test in <Module
line just above the test names of the file. BTW,
pytest --collect-only
确实在<Module
line中提供测试的文件名,就在文件的测试名称之上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.