Pytest-插件介绍与使用
1. 执行标记用例执行次数
首先安装 repeat:
pip install pytest-repeat
@pytest.mark.repeat(n)执行当前用例 n 次 然后再往下执行其他用例
import pytest
class TestCase:
def test_01(self):
print("\ntest_01")
@pytest.mark.repeat(2)
def test_02(self):
print("\ntest_02")
def test_03(self):
print("\ntest_03")
if __name__ == '__main__':
pytest.main()
2. 调整用例的执行顺序
首先安装 ordering:
pip install pytest-ordering
@pytest.mark.last 最后一个执行用例
@pytest.mark.run(order=1) 第一个执行用例
import pytest
class TestCase:
def test_01(self):
print("\ntest_01")
@pytest.mark.last()
def test_02(self):
print("\ntest_02")
@pytest.mark.run(order=1)
def test_03(self):
print("\ntest_03")
if __name__ == '__main__':
pytest.main()
3. 用例之间的依赖关系
- 这是一个pytest第三方插件,主要解决用例之间的依赖关系。如果依赖的上下文失败后续的用例会被标识为跳过执行,相当于执行了
pytest.mark.skip - dependency可作用的范围有:
session、package、module、class - 安装 pip install pytest-dependency
- 首先我们需要在用例开始的位置打上一个装饰器@pytest.mark.dependency(),这是代表这条用例作为主条件,如果这条用例失败,关联它的用例会跳过执行。
- 在被关联的用例上,也打上带参数的装饰器
@pytest.mark.dependency(),depends接受的参数是关联的依赖用例名。 - 在
depends也可以用别名的方式指定用例名
# 类实现方式
class TestCase:
@pytest.mark.dependency()
def test_01(self):
assert 1 ==11
@pytest.mark.dependency(depends=["TestCase::test_01"])
def test_02(self):
assert 2 == 2
if __name__ == '__main__':
pytest.main()
# test_01失败 test_02跳过执行
# 函数实现方式
@pytest.mark.dependency()
def test_01():
assert 1 == 11
@pytest.mark.dependency(depends=["test_01"])
def test_02():
assert 11 == 11
if __name__ == '__main__':
pytest.main()
# 通过起别名
@pytest.mark.dependency(name="a")
def test_01():
assert 1 == 11
@pytest.mark.dependency(depends=["a"])
def test_02():
assert 11 == 11
if __name__ == '__main__':
pytest.main()
# 定义依赖范围
class TestCase1:
@pytest.mark.dependency()
def test_01(self):
assert True
class TestCase2:
@pytest.mark.dependency(depends=["TestCase1::test_01"], scope="class")
def test_02(self):
assert 11 == 111
if __name__ == '__main__':
pytest.main()
import pytest
@pytest.mark.xfail(reason="deliberate fail")
@pytest.mark.dependency()
def test01():
# print("--01--")
raise Exception('111')
@pytest.mark.dependency(depends=['test01'])
def test02():
print("--02--")
@pytest.mark.dependency()
def test03():
print("--03--")
@pytest.mark.dependency()
def test04():
print("--04--")
@pytest.mark.dependency(depends=['test03','test04'])
def test05():
print("--05--")
先看test05,次用例依赖于test03以及test04,如果test03或test04失败,则跳过执行,相反,test03以及test04都必须成功才能执行。且必须打上标记位@pytest.mark.dependency(),否则无效。再看test02,因为依赖test01,而test01我们主动异常,并且给上了xfail,预失败标志,所以它本身就是不通过的,所以test02会随之跳过执行。
4. 多重校验 pytest-assume
正常情况下一条用例如果有多条断言,一条断言失败了,其他断言就不会执行了,而使用pytest-assume可以继续执行下面的断言
安装:pip install pytest-assume
import pytest
def test_assume():
print('登录操作')
pytest.assume(1 == 2)
print('搜索操作')
pytest.assume(2 == 2)
print('加购操作')
pytest.assume(3 == 2)
5. 分布式测试(pytest-xdist)
功能测试用例非常多时,比如有1千条用例,假设每个用例执行需要1分钟,如果单个测试人员执行需要1000分钟才能跑完
当项目非常紧急时,会需要协调多个测试资源来把任务分成两部分,于是执行时间缩短一半,如果有10个小伙伴,那么执行时间就会变成十分之一,大大节省了测试时间
为了节省项目测试时间,10个测试同时并行测试,这就是一种分布式场景
分布式执行用例的原则:
1.用例之间是独立的,没有依赖关系,完全可以独立运行
2.用例执行没有顺序要求,随机顺序都能正常执行
3.每个用例都能重复运行,运行结果不会影响其他用例
插件安装:
pip3 install pytest-xdist -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
使用方法: pytest test_demo.py -n 2 -s
pytest -n 2 (表示以两个进程的方式并行执行自动化测试用例,默认以用例层面进行分配(用例之间不能有依赖关系)
pytest -n auto
n auto:
1.按照cpu核数自动生成多个进程
2.可以自动检测到系统的CPU核数;从测试结果来看,检测到的是逻辑处理器的数量,即假12核
3.使用auto等于利用了所有CPU来跑用例,此时CPU占用率会特别高
--dist=loadfile 改变分布式执行用例时的分配模式,按照测试文件进行分配(测试文件之间不能有依赖关系)
--dist=loadscope 按照模块分配(测试模块之间不能有依赖关系)
6. 用例失败重跑
pip install pytest-rerunfailures
# 使用方法一: 装饰器
@pytest.mark.flaky(reruns=10, reruns_delay=1) # 重跑50次,每次间隔1s
class TestDou:
def test_case1(self):
# assert 1 == 6
assert 1 == random.randint(1, 5) # 只要在多次RERUN中遇到一次成功,即可停止,并最终结果为PASSED
# 使用方法二: 命令行
class TestYin:
def test_case2(self):
assert 1 == 6
if __name__ == '__main__':
pytest.main(['--reruns', '3', '--reruns-delay', '2', ])
7. 如何设置测试用例的日志输出?
pip install pytest-rerunfailures
# 在测试用例中使用日志输出
import logging
def test_logging(caplog):
logging.info("这是一条日志")
assert 1 == 1
assert "这是一条日志" in caplog.text
8. 如何设置测试用例的断言?
pip install pytest-assume
# 在测试用例中使用断言
import pytest
def test_assume():
pytest.assume(1 == 2)
pytest.assume(2 == 2)
pytest.assume(3 == 3)
9. 如何设置测试用例的覆盖率?
pip install pytest-cov # 运行测试用例并生成覆盖率报告 pytest --cov=.
10. 如何设置测试用例的分布式执行?
# 安装pytest-parallel插件 pip install pytest-parallel # 运行测试用例并使用4个节点分布式执行 pytest --workers 4
11. 如何设置测试用例的性能测试?
# 安装pytest-benchmark插件
pip install pytest-benchmark
# 在测试用例中使用性能测试
import pytest
def test_benchmark(benchmark):
result = benchmark(lambda: sum(range(10000)))
assert result == 49995000
12. 如何设置测试用例的安全测试?
# 安装pytest-security插件
pip install pytest-security
# 在测试用例中使用安全测试
import pytest
def test_security():
pytest.security.scan("http://XXXXX.com")

浙公网安备 33010602011771号