有没有办法指定从文件运行哪些pytest测试?

Sha*_*rad 133 python pytest

有没有办法选择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,它也将被选中.

  • 谢谢。但是如果 foo.py 和 bar.py 中有一个同名的测试(比如 test_001),它最终会从两者执行。 (11认同)
  • 它应该是`py.test path/to/test/file.py -k function_name_test` (10认同)
  • 但这对我来说是个问题。我想选择从哪个文件执行哪些测试。 (3认同)
  • 是的,任何匹配模式的东西都将按照我在笔记中提到的那样执行 (2认同)

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)

  • 有趣的是,其中一些似乎扩展到参数化测试,例如,如果您的测试在名称“test_demo.py::test_function[test_parameters_1]”下运行,那么您可以在命令行上给出它以仅运行此参数实例。 (2认同)

小智 50

指定测试/选择测试

Pytest支持从命令行运行和选择测试的几种方法。

在模块中运行测试

pytest test_mod.py

在目录中运行测试

pytest testing/

通过关键字表达式运行测试

pytest -k "MyClass and not method"

这将运行包含与给定字符串表达式匹配的名称的测试,其中可能包括使用文件名,类名和函数名作为变量的Python运算符。上面的示例将运行,TestMyClass.test_something但不会运行TestMyClass.test_method_simple

按节点ID运行测试

每个收集的测试都分配有一个唯一的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

  • 这是一个比上面更好的答案 (9认同)
  • test_mod.py::TestClass::test_method 最适合我 (3认同)

小智 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)


小智 10

也许使用hook 您可以解析o文件pytest_collect_file()的内容,其中根据需要指定测试,并将它们返回到 pytest 核心。.txt.yaml

pytest 文档中显示了一个很好的示例。我想你在寻找什么。


Ger*_*y C 10

尝试:

pytest path/file_test.py::test_name
Run Code Online (Sandbox Code Playgroud)

正如其他人所建议的那样使用-k将匹配带有该子字符串的任何测试,而不仅仅是特定的测试。


Pra*_*rma 8

举例来说,您有一个名为test.py. 在此文件中,您有一个名为 的类TestSomeService。该类有 2 个函数,即test_service1test_service2

  1. 现在要运行特定的功能测试,您可以这样做
  • pytest test.py::TestSomeService::test_service1
  • pytest test.py::TestSomeService::test_service2
  1. 运行特定的类
  • pytest test.py::TestSomeService