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