有没有办法选择pytest测试从文件运行?例如,包含要执行的测试列表的文件foo.txt:
tests_directory/foo.py::test_001
tests_directory/bar.py::test_some_other_test
Run Code Online (Sandbox Code Playgroud)
或者有没有办法从pytest的不同目录中选择多个测试,测试名称中没有共同的模式?
py.test -k <pattern> 允许单一模式.
一种选择是针对每个测试使用pytest.mark,但我的要求是运行来自不同文件的不同测试组合.
有没有办法为每个模式指定多个模式和测试文件名?或者有没有办法在文件中指定确切的测试路径并将该文件作为pytest的输入提供?或者是否有可用于此目的的钩子功能?
sup*_*aze 172
您可以使用-k选项来运行具有不同模式的测试用例:
py.test tests_directory/foo.py tests_directory/bar.py -k 'test_001 or test_some_other_test'
Run Code Online (Sandbox Code Playgroud)
这将运行名为test_001的测试用例和test_some_other_test,以取消选择其余的测试用例.
注意:这将选择以test_001或test_some_other_test开头的任何测试用例.例如,如果您有测试用例test_0012,它也将被选中.
lmi*_*asf 102
我的回答提供了一种在不同场景中运行测试子集的方法。
pytest
Run Code Online (Sandbox Code Playgroud)
要从一个目录运行所有测试,请使用该目录作为参数
pytest:
pytest tests/my-directory
Run Code Online (Sandbox Code Playgroud)
要运行一个充满测试的文件,列出文件的相对路径作为参数pytest:
pytest tests/my-directory/test_demo.py
Run Code Online (Sandbox Code Playgroud)
要运行单个测试函数,请添加::和测试函数名称:
pytest -v tests/my-directory/test_demo.py::test_specific_function
Run Code Online (Sandbox Code Playgroud)
-v 使用,以便您可以查看运行了哪个函数。
要只运行一个类,就像我们对函数所做的那样::,然后将类名添加到文件参数中:
pytest -v tests/my-directory/test_demo.py::TestClassName
Run Code Online (Sandbox Code Playgroud)
如果您不想运行所有测试类,只运行一个方法,只需添加另一个::和方法名称:
pytest -v tests/my-directory/test_demo.py::TestClassName::test_specific_method
Run Code Online (Sandbox Code Playgroud)
该-k选项使您能够传入表达式以运行具有由表达式指定的特定名称作为测试名称的子字符串的测试。可以使用and , or , 而不是创建复杂的表达式。
例如,要运行名称中包含 _raises 的所有函数:
pytest -v -k _raises
Run Code Online (Sandbox Code Playgroud)
小智 50
Pytest支持从命令行运行和选择测试的几种方法。
pytest test_mod.py
pytest testing/
pytest -k "MyClass and not method"
这将运行包含与给定字符串表达式匹配的名称的测试,其中可能包括使用文件名,类名和函数名作为变量的Python运算符。上面的示例将运行,TestMyClass.test_something但不会运行TestMyClass.test_method_simple。
每个收集的测试都分配有一个唯一的nodeid名称,该名称由模块文件名后跟说明符(例如类名,函数名和参数化参数)组成,并用::字符分隔。
pytest test_mod.py::test_func
在命令行中指定测试方法的另一个示例:
pytest test_mod.py::TestClass::test_method
pytest -m slow
将运行用@pytest.mark.slow装饰器装饰的所有测试。
有关更多信息,请参见标记。
pytest --pyargs pkg.testing
这将导入pkg.testing并使用其文件系统位置来查找并运行测试。
来源:https://docs.pytest.org/en/latest/usage.html#specifying-tests-selecting-tests
小智 22
根据关于按节点 ID 运行测试的文档
因为 foo.txt 中有所有节点 ID,所以只需运行
pytest $(tr '\n' ' ' <foo.txt)
Run Code Online (Sandbox Code Playgroud)
这与下面的命令相同(问题中包含文件内容)
pytest tests_directory/foo.py::test_001 tests_directory/bar.py::test_some_other_test
Run Code Online (Sandbox Code Playgroud)
Pet*_*ter 16
如果您在两个不同的类中具有相同的方法名称,而您只想运行其中一个,则此方法有效:
pytest tests.py -k 'TestClassName and test_method_name'
Run Code Online (Sandbox Code Playgroud)
Ari*_*ury 16
方法 1:随机选择的测试。又长又丑。
python -m pytest test/stress/test_performance.py::TestPerformance::test_continuous_trigger test/integration/test_config.py::TestConfig::test_valid_config
Run Code Online (Sandbox Code Playgroud)
方法 2:使用关键字表达式。
注意:我正在按测试用例名称进行搜索。同样适用于 TestClass 名称。
情况 1:以下将运行找到的任何一个。由于我们使用了 'OR' 。
python -m pytest -k 'test_password_valid or test_no_configuration'
Run Code Online (Sandbox Code Playgroud)
假设以上两个实际上是正确的,将运行 2 个测试。
案例 2:现在一个不正确的名字和另一个正确的名字。
python -m pytest -k 'test_password_validzzzzzz or test_no_configuration'
Run Code Online (Sandbox Code Playgroud)
只找到并运行一个。
案例 3:如果您想运行所有提到的测试或无,则使用 AND
python -m pytest -k 'test_password_valid and test_no_configuration'
Run Code Online (Sandbox Code Playgroud)
如果正确或不正确,两者都将运行。
情况 4:仅在一个文件夹中运行测试:
python -m pytest test/project1/integration -k 'test_password_valid or test_no_configuration'
Run Code Online (Sandbox Code Playgroud)
案例 5:仅从一个文件运行测试。
python -m pytest test/integration/test_authentication.py -k 'test_password_expiry or test_incorrect_password'
Run Code Online (Sandbox Code Playgroud)
案例 6:运行除匹配项之外的所有测试。
python -m pytest test/integration/test_authentication.py -k 'not test_incorrect_password'
Run Code Online (Sandbox Code Playgroud)
Ger*_*y C 10
尝试:
pytest path/file_test.py::test_name
Run Code Online (Sandbox Code Playgroud)
正如其他人所建议的那样使用-k将匹配带有该子字符串的任何测试,而不仅仅是特定的测试。
举例来说,您有一个名为test.py. 在此文件中,您有一个名为 的类TestSomeService。该类有 2 个函数,即test_service1和test_service2。
pytest test.py::TestSomeService::test_service1pytest test.py::TestSomeService::test_service2pytest test.py::TestSomeService