Python 有没有办法指定从文件运行哪些 pytest 测试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36456920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to specify which pytest tests to run from a file?
提问by Sharad
Is there a way to select pytest tests to run from a file? For example, a file foo.txt containing a list of tests to be executed:
有没有办法选择从文件中运行的 pytest 测试?例如,文件 foo.txt 包含要执行的测试列表:
tests_directory/foo.py::test_001
tests_directory/bar.py::test_some_other_test
Or is there a way to select multiple tests, having no common pattern in test name, from different directories with pytest?
或者有没有办法从pytest的不同目录中选择多个测试,在测试名称中没有共同的模式?
py.test -k <pattern>
allows a single pattern.
py.test -k <pattern>
允许单一模式。
One option is to have a pytest.mark against each test, but my requirement is to run different combination of tests from different files.
一种选择是针对每个测试使用 pytest.mark,但我的要求是运行来自不同文件的不同测试组合。
Is there a way to specify multiple patterns and a test file name for each pattern? Or is there a way to specify the exact test paths in a file and feed that file as an input to pytest? Or Is there a hook function that can be utilized for this purpose?
有没有办法为每个模式指定多个模式和测试文件名?或者有没有办法在文件中指定确切的测试路径并将该文件作为 pytest 的输入?或者是否有可用于此目的的钩子函数?
回答by supamaze
You can use -k
optionto run test cases with different patterns:
您可以使用-k
选项来运行具有不同模式的测试用例:
py.test tests_directory/foo.py tests_directory/bar.py -k 'test_001 or test_some_other_test'
This will run test cases with name test_001and test_some_other_testdeselecting the rest of the test cases.
这将运行名为test_001和test_some_other_test 的测试用例,取消选择其余的测试用例。
Note: This will select any test case starting with test_001 or test_some_other_test. For example, if you have test case test_0012it will also be selected.
注意:这将选择以test_001 或 test_some_other_test开头的任何测试用例。例如,如果您有测试用例test_0012,它也会被选中。
回答by Amritpal Singh
Specifying tests / selecting tests
指定测试/选择测试
Pytest supports several ways to run and select tests from the command-line.
Pytest 支持多种从命令行运行和选择测试的方法。
Run tests in a module
在模块中运行测试
pytest test_mod.py
pytest test_mod.py
Run tests in a directory
在目录中运行测试
pytest testing/
pytest testing/
Run tests by keyword expressions
通过关键字表达式运行测试
pytest -k "MyClass and not method"
pytest -k "MyClass and not method"
This will run tests which contain names that match the given string expression, which can include Python operators that use filenames, class names and function names as variables. The example above will run TestMyClass.test_something
but not TestMyClass.test_method_simple
.
这将运行包含与给定字符串表达式匹配的名称的测试,其中可以包括使用文件名、类名和函数名作为变量的 Python 运算符。上面的例子会运行,TestMyClass.test_something
但不会TestMyClass.test_method_simple
。
Run tests by node ids
按节点 ID 运行测试
Each collected test is assigned a unique nodeid
which consist of the module filename followed by specifiers like class names, function names and parameters from parametrization, separated by ::
characters.
每个收集到的测试都被分配了一个唯一的nodeid
,它由模块文件名后跟类名、函数名和参数化参数等说明::
符组成,用字符分隔。
To run a specific test within a module:
要在模块内运行特定测试:
pytest test_mod.py::test_func
pytest test_mod.py::test_func
Another example specifying a test method in the command line:
在命令行中指定测试方法的另一个示例:
pytest test_mod.py::TestClass::test_method
pytest test_mod.py::TestClass::test_method
Run tests by marker expressions
通过标记表达式运行测试
pytest -m slow
pytest -m slow
Will run all tests which are decorated with the @pytest.mark.slow
decorator.
将运行所有用@pytest.mark.slow
装饰器装饰的测试。
For more information see marks.
有关更多信息,请参阅标记。
Run tests from packages
从包运行测试
pytest --pyargs pkg.testing
pytest --pyargs pkg.testing
This will import pkg.testing
and use its filesystem location to find and run tests from.
这将导入pkg.testing
并使用其文件系统位置来查找和运行测试。
Source: https://docs.pytest.org/en/latest/usage.html#specifying-tests-selecting-tests
来源:https: //docs.pytest.org/en/latest/usage.html#specifying-tests-selecting-tests
回答by Peter
If you have the same method name in two different classes and you just want to run one of them, this works:
如果您在两个不同的类中具有相同的方法名称,而您只想运行其中一个,则此方法有效:
pytest tests.py -k 'TestClassName and test_method_name'
回答by u11371569
According to the doc about Run tests by node ids
根据关于按节点 ID 运行测试的文档
since you have all node ids in foo.txt, just run
由于您在 foo.txt 中拥有所有节点 ID,因此只需运行
pytest `cat foo.txt | tr '\n' ' '`
this is same with below command (with file content in the question)
这与以下命令相同(问题中有文件内容)
pytest tests_directory/foo.py::test_001 tests_directory/bar.py::test_some_other_test
回答by Arindam Roychowdhury
Method 1: Randomly selected tests. Long and ugly.
方法 1:随机选择的测试。又长又丑。
python -m pytest test/stress/test_performance.py::TestPerformance::test_continuous_trigger test/integration/test_config.py::TestConfig::test_valid_config
Method 2: Use Keyword Expressions.
方法 2:使用关键字表达式。
Note:I am searching by testcase names. Same is applicable to TestClass names.
注意:我正在按测试用例名称进行搜索。同样适用于 TestClass 名称。
Case 1:The below will run whichever is found. Since we have used 'OR' .
情况 1:以下将运行找到的任何一个。由于我们使用了 'OR' 。
python -m pytest -k 'test_password_valid or test_no_configuration'
Lets say the two above are actually correct, 2 tests will be run.
假设以上两个实际上是正确的,将运行 2 个测试。
Case 2:Now an incorrect name and another correct name.
案例 2:现在一个不正确的名字和另一个正确的名字。
python -m pytest -k 'test_password_validzzzzzz or test_no_configuration'
Only one is found and run.
只找到并运行一个。
Case 3: If you want all tests to run or no one, then use AND
情况 3:如果您希望所有测试都运行或不运行任何测试,则使用 AND
python -m pytest -k 'test_password_valid and test_no_configuration'
Both will be run if correct or none.
如果正确或不正确,两者都将运行。
Case 4:Run test only in one folder:
情况 4:仅在一个文件夹中运行测试:
python -m pytest test/project1/integration -k 'test_password_valid or test_no_configuration'
Case 5:Run test from only one file.
案例 5:仅从一个文件运行测试。
python -m pytest test/integration/test_authentication.py -k 'test_password_expiry or test_incorrect_password'
Case 6:Run all tests except the match.
案例 6:运行除匹配项之外的所有测试。
python -m pytest test/integration/test_authentication.py -k 'not test_incorrect_password'
回答by asterio gonzalez
Maybe using pytest_collect_file()
hook you can parse the content of a .txt
o .yaml
file where the tests are specify as you want, and return them to the pytest core.
也许使用pytest_collect_file()
钩子可以解析.txt
o.yaml
文件的内容,其中测试是根据需要指定的,并将它们返回到 pytest 核心。
A nice example is shown in the pytest documentation. I think what you are looking for.
pytest 文档中显示了一个很好的示例。我想你在找什么。
回答by JL Peyret
Here's a possible partial answer, because it only allows selecting the test scripts, not individual tests within those scripts.
这是一个可能的部分答案,因为它只允许选择测试脚本,而不是这些脚本中的单个测试。
And it also limited by my using legacy compatibility mode vs unittest
scripts, so not guaranteeing it would work with native pytest.
而且它也受到我使用旧版兼容模式与unittest
脚本的限制,因此不能保证它可以与本机 pytest 一起使用。
Here goes:
开始:
- create a new dictory, say
subset_tests_directory
. ln -s tests_directory/foo.py
ln -s tests_directory/bar.py
be careful about imports which implicitly assume files are in
test_directory
. I had to fix several of those by runningpython foo.py
, from withinsubset_tests_directory
and correcting as needed.Once the test scripts execute correctly, just
cd subset_tests_directory
andpytest
there. Pytest will only pick up the scripts it sees.
- 创建一个新的字典,比如说
subset_tests_directory
。 ln -s tests_directory/foo.py
ln -s tests_directory/bar.py
注意隐含假设文件在
test_directory
. 我不得不通过运行python foo.py
, 从内部subset_tests_directory
并根据需要进行更正来修复其中的几个。一旦测试脚本执行正确,公正
cd subset_tests_directory
和pytest
有。Pytest 只会选取它看到的脚本。
Another possibility is symlinking within your current test directory, say as ln -s foo.py subset_foo.py
then pytest subset*.py
. That would avoid needing to adjust your imports, but it would clutter things up until you removed the symlinks. Worked for me as well.
另一种可能性是在您当前的测试目录中进行符号链接,例如ln -s foo.py subset_foo.py
then pytest subset*.py
。这将避免需要调整您的导入,但在您删除符号链接之前它会使事情变得混乱。也为我工作。