假设文件夹
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.py
、tests1/subtests/subtest.py
、tests2/test.py
和tests2/subtests/subtest.py
,如下所示:
project
|-pytest.ini
|-tests1
| |-__init__.py
| |-test.py # Here
| └-subtests
| |-__init__.py
| └-subtest.py # Here
└-tests2
|-__init__.py
|-test.py # Here
└-subtests
|-__init__.py
└-subtest.py # Here
此外,如果你在
pytest.ini中将
tests1
和tests2/subtests
设置为testpaths,则可以运行除tests2/test.py
之外的所有内容:
# "pytest.ini"
[pytest]
testpaths = tests1 tests2/subtests # Here
此外,您可以根据
文档使用以下命令运行
tests2/subtests/subtest.py
:
pytest tests2/subtests
或者:
pytest tests2/subtests/subtest.py