假设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.py
、tests1/subtests/test_2.py
、tests2/test_1.py
和tests2/subtests/test_2.py
中的所有测试,如下所示,因为默认情况下Pytest可以根据python_files、python_classes和python_functions运行文件test_*.py
和*_test.py
,以Test
为前缀的类和以test
为前缀的函数,而不是apple.py
文件、Apple
类和apple(self)
函数,Pytest可以运行任何文件夹,如tests1
、tests2
和subtests
*我的回答说明了这一点:
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中将tests1
和tests2/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