Pytest测试管理

在pytest数量很多的时候,有时候只想运行某一类测试,例如:在日常的CICD流水线上只运行冒烟测试,每天晚上运行一次回归测试,发版前运行性能测试。

可以使用pytest的marking功能将测试进行标记:

import pytest

@pytest.mark.smoke
def test_homepage_loads():
    # Test to check if the homepage loads quickly
    assert ...

@pytest.mark.feature1
@pytest.mark.regression
def test_login_successful():
    # Test to check if the login process works as expected
    assert ...

每个测试可以有多个标记,也可以对整个文件进行标记:

import pytest
pytestmark = pytest.mark.smoke

or

pytestmark = [pytest.mark.smoke, pytest.mark.feature1]

标记后可以通过 -m 参数运行指定标记的测试:

pytest -m smoke

参考链接:

https://docs.pytest.org/en/stable/example/markers.html

https://pytest-with-eric.com/pytest-best-practices/pytest-markers/

comments powered by Disqus