pytest单元测试框架简介

一、什么是单元测试框架

单元测试框架是指:在软件开发当中,针对软件的最小单位(函数,方法)进行正确性的检查测试。

二、单元测试框架的分类

Java: Juint、TestNG
Python: unnitest、pytest

三、单元测试框架是用来做什么的?

  • 测试发现:从多个文件里面找到测试用例
  • 测试执行:按照一定的顺序和规则去执行,并生成结果
  • 测试判断:通过断言来判断预期结果和实际结果的差异
  • 测试报告:统计测试进度、耗时、用例的通过率,生成测试报告

四、单元测试框架和自动化测试框架有什么区别?

(1)什么是自动化测试框架?

去完成一个指定的系统的自动化测试,而封装的一整套的,完善的代码框架。

(2)自动化测试框架的作用

  • 提高测试效率,降低维护成本‘

  • 减少人工干预,提高测试的准确性,增加代码的复用性

  • 核心思想就是:让不懂代码的人也能够通过该框架实现自动化测试。
    (3)pytest单元测试框架和自动化测试框架的关系

  • pytest单元测试框架只是自动化测试框架的部分之一。

  • pom:只是自动化测试框架的部分之一

  • 数据驱动:

  • 关键字驱动:

  • 全局配置文件的封装

  • 日志监控:

  • seleinumr、equests二次封装

  • 断言

  • 测试报告

  • 邮件

五、pytest框架简介

1、pytest是一个非常成熟的python的单元测试框架,比unittest更灵活,更容易上手。
2、pytest可以和seleium、appinum、requests实现Web自动化,App 自动化,和接口自动化。
3、pytest可以实现测试用例的跳过,和reruns失败用例测重试
4、pytest可以结合,allure实现非常美观的测试报告
5、pytest可以和Jenkins实现持续集成。
6、pytest有非常多的强大的插件,并且这些插件可以实现很多实用的操作。
pytest
pytest-html (生成html格式的报告)
pytest-xdist (测试用例分布执行,多CPU分发)
pytest-ordering (用于改变测试用例的执行顺序)
pytest-rerunfailures (用于失败用例重试)
allure-pytest(用于生成美观的测试报告)

批量下载插件
放到requirements.txt文件中,执行 pip install -r requirements.txt 这个命令

在这里插入图片描述

六、使用pytest默认的测试用例的规则和基础使用

1、pytest的使用规则

  • 模块名必须以test_开头或者_test结尾
  • 测试类必须以Test开头,并且不能有init方法
  • 测试方法必须以test开头

2、通过读取pytest.ini配置文件运行

七、pytest测试用例的运行方式

1、主函数模式
(1)运行所有:pytest.main()
(2)指定模块:pytest.main(["-vs", “test_login.py”])
(3)指定目录: pytest.main(["-vs", “./interface_testCase”])
(4)通过nodeid指定用例运行,nodeid由模块名,分隔符,类名和方法名组成
pytest.main(["-vs", “./interface_testCase/test_interface.py::test_04_func”])
pytest.main(["-vs", “./interface_testCase/test_interface.py::TestInterface::test_03_shaheshang”])

在这里插入图片描述
test_interface.py

def test_04_func():
    print("测试函数")


class TestInterface:
    def test_03_shaheshang(self):
        print("测试沙和尚")

all.py

import pytest

if __name__ == '__main__':
    pytest.main(["-vs", "./interface_testCase/test_interface.py::test_04_func"])

运行结果:
interface_testCase/test_interface.py::test_04_func 测试函数
PASSED

2、命令行模式
(1)运行所有:pytest
(2)执行模块:cd web_testCase 切换到指定模块所在的目录
pytest -vs test_login.py
(3)指定目录: pytest -vs ./interface_testCase
(4)通过nodeid指定用例运行: pytest -vs ./interface_testCase/test_interface.py::test_04_func

3、通过读取pytest.ini配置文件运行
pytest.ini 这个文件它是pytest单元测试框架的核心配置文件。

  • 位置:一般放在项目的根目录下。
  • 编码:必须是ANSI
  • 作用: 改变pytest默认的行为规则
  • 运行的规则:不管是主函数的模式运行,还是命令行模式运行,都会去读取这个配置文件。
[pytest]
addopts = -vs   # 命令行参数,用空格分隔
# 测试用例路径
testpaths = './web_testCase'
# 模块名规则
python_files = 'test*.py'
# 类名的规则
python_classes = Test*
# 方法名的规则
python_functions = test

4、参数详解

参数详解:
-s: 表示输出调试信息,包括print打印的信息
-v: 显示更详细的信息,包括用例所在的模块名和类名
-vs: 这两个参数可以一起使用

--reruns 2: 失败用例重跑
-x :表示只要有一个用例报错,那么测试停止
--maxfail=2: 表示出现两个用例失败就停止
-k: 根据测试用例的部分字符串指定测试用例
def test_01_sunwukong  def test_02_xiaohong
     eg: pytest -vs ./web_testCase -k 'on'  
--html ./report/report.html   生成html格式的测试报告
-m : 执行指定标记的用例  eg:  -m ’smoke‘  标志只执行smoke用例
 -n:支持多线程或者分布式运行测试用例    
 - 主函数模式
    pytest.main(["-vs", "./web_testCase/test_login", "-n=2"])
 - 命令行模式书写
    pytest -vs ./web_testCase/test_login -n 2
`如果有5个用例,分配两个线程来执行的话,那么第一个线程会执行1 3 5 用例,第二个线程会执行2 4 用例


八、失败用例重试

失败用例重试使用的参数是:–reruns==2
test_login中有5个用例,模拟让第二个用例执行失败

class TestLogin:
    def test_01_sunwukong(self):
        print("测试孙悟空")

    def test_02_xiaohong(self):
        print("测试小红")
        assert 1 == 2

    def test_03_huahua(self):
        print("测试花花")

    def test_01_xiaoming(self):
        print("测试小明")

    def test_01_dudu(self):
        print("测试嘟嘟")

all.py

import pytest

if __name__ == '__main__':
    pytest.main(["-vs", "./web_testCase", '--reruns=2'])

使用命令行模式执行:

 pytest -vs ./web_testCase --reruns 2 

九、pytest执行测试用例的顺序

unittest: 通过ASCII的大小来决定执行的顺序
pytest: 默认从上到下

安装:pytest-ordering 这个插件
该插件作用:用于改变测试用例的执行顺序
可以通过mark 标记来指定用例执行的顺序

 @pytest.mark.run(order=1)
    def test_07_xiaohong(self):
        print("测试小红")

十、如何分组执行

1、分组执行使用场景
冒烟、分模块执行、分接口和web执行
smoke:冒烟用例,分布在各个模块里面
在pytest.ini 配置文件中添加

markers =
    smoke: 冒烟用例
    usermanage: 用户管理模块
    productmanage:商品管理模块

在用例中添加标记

    @pytest.mark.smoke
    @pytest.mark.usermanager
    def test_07_xiaohong(self):
        print("测试小红")

pytest -vs -m ‘smoke’
pytest -vs -m ‘somke or usermanager’

或者在pytest.ini文件中,配置运行指定标记的用例

[pytest]
# 命令行参数,用空格分隔
addopts = -vs -m 'smoke'
# 测试用例文件夹,可自己配置
testpaths = './web_testCase'
# 配置测试搜索的模块文件名称
python_files = 'test*.py'
# 配置测试搜索的测试类名
python_classes = Test*
# 配置测试搜索的测试函数名
python_functions = test
markers =
    smoke: 冒烟用例
    usermanage: 用户管理模块
    productmanage:商品管理模块

在test_login中,用例标记

    @pytest.mark.smoke
    def test_07_xiaohong(self):
        print("测试小红")

在all.py文件中执行

import pytest
if __name__ == '__main__':
    pytest.main(["-vs", "./web_testCase"])

十一、pytest跳过测试用例

使用场景:只执行正向用例,不执行反向用例,异常用例

在用例上添加skip标记
无条件跳过:
@pytest.mark.skip(reason=“不执行花花用例”)
有条件跳过:
@pytest.mark.skipif(age >= 18, “还未成年”)

  • 1
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值