前言:最近熬夜太多,老是失眠,那就努力更新博客,让大家多多学习,自己也梳理知识点
1、pytest的安装(pytest不允许测试类出现初始化方法,不然整个类就不会被当成测试类)
pip install pytest
2、查看pytest版本号
pytest -V
3、pytestkuk框架的简介
4、pip install pytest-sugar(对运行过程进行界面美化)
pip install pytest-rerunfailures(重新运行出错的测试用例)
pip install pytest-xdist(多任务同时并发的执行测试用例)
pip install pytest-assume(添加断言,同一条测试用例每一条的断言都会反馈出来)
pip install pytest-html(生成美观的HTML测试报告)
5、测试用例的识别和运行
6、命令栏详细的使用介绍
pytest 文件名(运行该文件下以test命名的所有测试用例)
pytest -v -s 文件名(会输出详细信息)
pytest --maxfail=2文件名(当运行错误的用例达到设置的次数就停止,2可以改为其他数字)
7、pytest执行-失败重新运行
pytest -v -s --reruns 3 --reruns-delay 2 文件名(失败用例重新运行3次,间隔2秒)
8、pytest执行-多条断言有失败也都运行
pytest data1.py::TestDemo::test_four(运行效果如下图)
9、使用pycharm运行pytest
10、pytest框架结构
代码例子:
#!/usr/bin/env python
# _*_ coding: utf-8 _*_
# @File : data2.py
# @Time : 2020/3/28 15:49
# @Author : 李文良
import pytest
def setup_module():
print("这是一个setup_module方法")
def teardown_module():
print("这是一个teardown_module方法")
def setup_function():
print("setup_function")
def teardown_function():
print("teardown_function")
def test_login():
print("这是一个外部的方法")
class TestDemo():
def setup_class(self):
print("setup_class")
def setup_method(self):
print("setup_method")
def setup(self):
print("setup")
def teardown_class(self):
print("teardown_class")
def teardown_method(self):
print("teardown_method")
def teardown(self):
print("teardown")
def test_one(self):
print("请开始执行 test_one 方法")
x = "this"
assert "t" in x
# pytest.assume("t" in x)
# pytest.assume(1 == 2)
def test_two(self):
print("请开始执行 test_two 方法")
x = "this"
assert "h" in x
def test_three(self):
print("请开始执行 test_three 方法")
x = "this"
assert "i" in x
def test_four(self):
print("请开始执行 test_four 方法")
x = "this"
assert "i" in x
# pytest.assume("1" in x)
# pytest.assume("1" not in x)
# pytest.assume("s" in x)
# pytest.assume("s" not in x)
def test_five(self):
print("请开始执行 test_five 方法")
x = "this"
assert "s" in x
if __name__ == "__main__":
pytest.main("-v -s")
# pytest.main(['-v','-s'])
执行结果:
11、pytest-fixture的用法
例子:
12、公共模块conftest的应用
13、yield的应用,用来进行数据回收
例子:scope="module"是可以修改的,默认function
14、不想原有测试方法有任何改动,或全部都自动实现自动应用,我们使用fixture中参数autouse=Ture实现
autouse=Ture默认是False,可以自己更改成Ture
15、fixture带参数传递
fixture固件函数需要传参,如果@pytest.fixture()里面没有传参,可以通过@pytest.mark.parametrize()给固件函数传参,即indirect=True。并且固件函数的返回值作为Test函数的参数,不写的话传参的函数是不会调用的
16、某些场景跳过个别用例执行,这时我们需要用到@pytest.mark.skip和@pytest.mark.xfail
17、对用例进行标记
例子:
去除标签警告:
18、多线程并行与分布式执行用例
19、生成htm报告
待更新,营养跟不上了