1. pytest 简明教程(结合allure输出网页报告)

2024-01-01 18:20:05

1.安装?

cmd> pip install pytest

2.执行

1.执行单独

2. 执行顺序

import pytest


@pytest.mark.run(order=2)
def test_1():
    assert 2 > 1


@pytest.mark.run(order=1)
def test_2():
    assert 3 == 4

import pytest

if __name__ == "__main__":
    pytest.main(['-v','-s', '-k', 'test_a', '--html= report/test2.html'])

3.mark标记

3.1 skip

@pytest.mark.skip(reason=None)

@pytest.mark.skip(condition, reason="模块未开发好,跳过执行")

@pytest.mark.xfail(reson="预期失败")

@pytest.mark.xfail(reson="预期失败", run=False)

fixtrue
import pytest


@pytest.fixture()
def con():
    print("before test ")
    yield 
    print("after test")

@pytest.mark.run(order=2)
def test_1():
    assert 2 > 1


@pytest.mark.usefixtures('con')
def test_2():
    assert 1 == 1

import pytest


@pytest.fixture()
def con():
    print("before test ")
    yield
    print("after test")


@pytest.mark.run(order=2)
def test_1():
    assert 2 > 1


@pytest.mark.smoke
@pytest.mark.usefixtures('con')
def test_2():
    assert 1 == 1


@pytest.mark.smoke
def test_4():
    assert 1 == 1


@pytest.mark.smoke
def test_5():
    assert 1 == 1

pytest -v -m 'not smoke'

pytest -v -m smoke

approx函数

import pytest
from _pytest.python_api import approx
def test_one():
    assert 0.3 - 0.2 == approx(0.1)

4.结合allure输出网页报告

目录所示

4.1 生成allure中间文件

pytest --alluredir=./allure-results

4.2 将allure中间文件生成allure网页报文服务

allure serve ./allure-results

文章来源:https://blog.csdn.net/weixin_39023319/article/details/135306530
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。