pytest测试框架
allure + pytest生成报告
1. 获取运行结果 pytest pytest_demo2.py --alluredir=./result/test
2. 生成报告 allure generate ./result/test -o ./testreport/test1 --clean
@allure.link('www.baidu.com', name='issue链接')
1. pytest -v test.py 查询用例执行的详细信息
2. pytest -s test.py 测试用例中的print信息都会打印到终端上
3. pytest test.py::TestDemo 只执行魔游客类下的测试用例
4. pytest test.py -k "关键字" 执行关键字中的测试用例 或者忽略关键字中测试用例
5. pytest -x test.py 一旦遇到报错就会停止
6. pytest --maxfail=[num] 当运行错误达到num的时候机会停止
7. pytest -v --reruns 3 test.py 失败的用例重复执行3遍
修改pycharm的运行方式。 默认为unnitest
settings> python intergrated tools > default test runner > pytest
pytest.fixture
在登录函数前面加上@pytest.fixture()
之后需要先登录的用例,在方法中加上登录函数名
conftest.py
配置公共文件
@pytest.fixture(scope="module")
def open_browser():
print('\n打开浏览器,打开百度')
yield
print("执行teardown")
print("最后关闭浏览器")
@pytest.fixture(autouse=True)
def open():
print('打开浏览器')
@pytest.mark.parametrize("test_input, expected",[("3+5",8), ("2+5",7),("7*5",35)])
def test_eval(test_input, expected):
assert eval(test_input) == expected
@pytest.mark.skip
跳过相应测试用例
@pytest.makr.xfail
功能测试尚未实施或者尚未修复的错误,结果会体现为XPASS或者XFAIL,在测试报告中体现
自定义标记mark
@pytest.mark.name
运行方式
pytest file.py -m name
多线程和分布式执行
pytest file.py -n 3