有没有办法从多个文件夹中运行所有pytest案例



假设test_case1.py在文件夹A中,test_case2.py在文件夹B中。我可以使用单个pytest命令将它们一起运行吗?

文件夹结构:

projectfolder/A/test_case1.py
projectfolder/B/test_case2.py

您可以为pytest提供多个文件夹来发现中的测试

python -m pytest projectfolder/A projectfolder/B

或者如果您想使用配置文件:

# pyproject.toml
[tool.pytest.ini_options]
testpaths = [
"projectfolder/A",
"projectfolder/B",
]

您可以在多个文件夹中运行所有测试。

实际上,默认情况下,您可以运行tests1/test_1.pytests1/subtests/test_2.pytests2/test_1.pytests2/subtests/test_2.py中的所有测试,如下所示,因为默认情况下Pytest可以根据python_files、python_classes和python_functions运行文件test_*.py*_test.py,以Test为前缀的类和以test为前缀的函数,而不是apple.py文件、Apple类和apple(self)函数,Pytest可以运行任何文件夹,如tests1tests2subtests*我的回答说明了这一点:

project
|-pytest.ini
|-tests1
|  |-__init__.py
|  |-test_1.py # Here
|  └-subtests
|     |-__init__.py
|     └-test_2.py # Here
└-tests2
|-__init__.py
|-test_1.py # Here
└-subtests
|-__init__.py
└-test_2.py # Here

此外,如果在pytest.ini中将tests1tests2/subtests设置为testpath,则可以运行除tests2/test_1.py:之外的所有测试

# "pytest.ini"
[pytest]
testpaths = tests1 tests2/subtests # Here

并且,如果下面指定的testpaths不存在,则运行所有测试:

# "pytest.ini"
[pytest]
testpaths = my_tests # Doesn't exist

此外,您可以根据文档使用以下命令运行tests2/subtests/test_2.py

pytest tests2/subtests

或者:

pytest tests2/subtests/test_2.py

最新更新