首页 > 解决方案 > Pytest 不取消选择某些测试(但应该在 pytest.ini 中)

问题描述

我已经设置了一个测试套件,并且一直在使用 pytest 和 pytest-django。

提供一些背景知识:我正在尝试使用无头浏览器进行一些集成测试,并让 pytest 忽略在我使用标准(非无头)浏览器时使用的某些测试;我想使用真实的可视浏览器取消选择这些测试,这样它们就不会在我的 CI/CD 管道中触发。

给定pytest.ini下面的示例,如果我运行pytest launcher它会显示我期望有 1 个测试被取消选择(StandardBrowserTestCases 类中只有 1 个测试)。

但是,如果我运行pytest other_app(它也有一个 StandardBrowserTestCases 类),它不会显示任何被取消选择的内容,并且 StandardBrowserTestCases 与其他测试一起运行并且没有被取消选择。

[pytest]
addopts =
    --nomigrations
    --cov-config=.coveragerc
    --cov=my_project
    --cov=other_app
    --cov=launcher
    --cov=people
    ; # Ignore the StandardBrowserTestCases - these are only used for local
    ; # development / visual debug and contain no real valuable tests
    --deselect=other_app/tests/test_integrations.py::StandardBrowserTestCases
    --deselect=launcher/tests/test_integrations.py::StandardBrowserTestCases
    --deselect=people/tests/test_integrations.py::StandardBrowserTestCases
    --junitxml=./test-results/junit.xml
    --cov-report html:./test-results/htmlcov
    --html=./test-results/test_results.html
    --self-contained-html
DJANGO_SETTINGS_MODULE = my_project.unit-test-settings
python_files = tests.py test_*.py *_tests.py

我用--deselect对了吗?我如何才能弄清楚(疑难解答)以了解为什么它适用于一个应用程序(launcher)而不是另一个应用程序( other_app)?如何让 pytest 从每个应用程序中取消选择这些测试,而不仅仅是一个?

标签: pythondjangopytestpytest-django

解决方案


推荐阅读