有没有办法从多个文件夹运行所有 pytest 用例?

问题描述 投票:0回答:2

假设文件夹

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

文件夹结构:

projectfolder/A/test_case1.py
projectfolder/B/test_case2.py
python unit-testing testing pytest subdirectory
2个回答
8
投票

您可以为 pytest 提供多个文件夹来发现以下位置的测试:

python -m pytest projectfolder/A projectfolder/B

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

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

0
投票

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

实际上默认情况下,您可以运行所有

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
© www.soinside.com 2019 - 2024. All rights reserved.