有没有办法指定从文件运行哪些 pytest 测试? [英] Is there a way to specify which pytest tests to run from a file?

查看:40
本文介绍了有没有办法指定从文件运行哪些 pytest 测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从文件中选择 pytest 测试来运行?例如,包含要执行的测试列表的文件 foo.txt:

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:

tests_directory/foo.py::test_001
tests_directory/bar.py::test_some_other_test

或者,有没有办法用pytest从不同的目录中选择多个测试,在测试名称中没有共同的模式?

Or, is there a way to select multiple tests, having no common pattern in test name, from different directories with pytest?

pytest -k 允许单个模式.

一种选择是针对每个测试使用 pytest.mark,但我的要求是运行来自不同文件的不同测试组合.

One option is to have a pytest.mark against each test, but my requirement is to run different combination of tests from different files.

有没有办法为每个模式指定多个模式和一个测试文件名?

Is there a way to specify multiple patterns and a test file name for each pattern?

有没有办法在文件中指定确切的测试路径并将该文件作为输入提供给 pytest?

Is there a way to specify the exact test paths in a file and feed that file as an input to pytest?

是否有可以用于此目的的钩子函数?

Is there a hook function that can be utilized for this purpose?

推荐答案

您可以使用 -k 选项以运行不同模式的测试用例:

You can use -k option to run test cases with different patterns:

py.test tests_directory/foo.py tests_directory/bar.py -k 'test_001 or test_some_other_test'

这将运行名为 test_001test_some_other_test 的测试用例,取消选择其余的测试用例.

This will run test cases with name test_001 and test_some_other_test deselecting the rest of the test cases.

注意:这将选择以test_001 或 test_some_other_test 开头的任何测试用例.例如,如果您有测试用例 test_0012,它也会被选中.

Note: This will select any test case starting with test_001 or test_some_other_test. For example, if you have test case test_0012 it will also be selected.

这篇关于有没有办法指定从文件运行哪些 pytest 测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆