Skip to content

第 6 课:Allure 报告——让领导看了就满意

漂亮的可视化报告

一、安装

bash
pip install allure-pytest
brew install allure    # Mac

二、运行

bash
pytest tests/ --alluredir=./allure-results
allure serve ./allure-results   # 浏览器打开

三、装饰用例

python
import allure

@allure.feature("登录模块")
@allure.story("用户登录")
@allure.title("正确账号密码登录成功")
@allure.severity(allure.severity_level.BLOCKER)
def test_login():
    with allure.step("输入账号密码"):
        username = "admin"
    with allure.step("调用登录接口"):
        resp = requests.post("/login")
    with allure.step("验证结果"):
        assert resp.status_code == 200

四、严重级别

BLOCKER > CRITICAL > NORMAL > MINOR > TRIVIAL

五、失败自动截图

python
@pytest.fixture
def driver():
    d = webdriver.Chrome()
    yield d
    if hasattr(pytest, "failed"):
        allure.attach(d.get_screenshot_as_png(),
                      name="失败截图",
                      attachment_type=allure.attachment_type.PNG)
    d.quit()

六、集成 Jenkins

在 Pipeline 里加 allure includeProperties: true,Jenkins 上直接有报告链接。

💬 给清秀留言