07 接口自动化-用例管理框架之pytest单元测试框架

文章目录

一、pytest用例管理框架(单元测试框架)

1.分类

  • python:unittest,pytest 非常熟练。
  • java:testng,junit

2.主要作用

  • 发现测试用例:从多个py文件里面按照一定的规则找到测试用例。
  • 执行测试用例:按照一定的顺序执行测试用例,并生成结果。
    • pytest::默认从上到下,可以用装饰器改变规则。
    • unittest:默认安装ASCII顺序去执行。
  • 判断测试结果:断言。
  • 生成测试报告:pytest-html,allure报告。

二、pytest简介

1.基于python的单元测试框架,它可以和selenium,requests,appium结合实现自动化测试。
2.实现用例跳过skip和reruns失败用例重跑。
3.它可以结合allure-pytest插件生成allure报告。
4.很方便和jenkins实现持续集成。
5.有很多强大的插件:

  • pytest-html 生成html测试报告。
  • pytest-xdist 多线程执行测试用例。
  • pytest-ordering 改变测试用例的执行顺序。
  • pytest-rerunfailures 失败用例重跑
  • allure-pytest 生成allure报告。

放到一个requirements.txt的文档中,如:

复制代码
pytest
pytest-html
pytest-xdist
pytest-ordering
pytest-rerunfailures
allure-pytest

然后通过:pip install -r requirements.txt

三、pytest的最基本的测试用例的规则

1.模块名必须以test_开头或者_test结尾。
2.测试类必须以Test开头,并且不能带有init方法。
3.测试用例必须以test_开头。

命令规范:

  • 模块名:一般全小写:多个英文之间用_隔开。
  • 类名:类名一般是首字母大写
  • 方法名:一般全小写:多个英文之间用_隔开。

四、运行方式

1.主函数方式

常见参数:
-v:输出更加详细的信息。比如文件和用例名称等。
-s:输出调试信息。打印信息等。

可以合并成:-vs
--reruns 2:失败重跑N次
-x:出现1个失败就停止测试。
--maxfail=2 出现N个失败就终止测试。
--html=report.html 生成html的测试报告
-n:多线程。
-k:运行测试用例名称中包含指定字符串的用例。
指定字符串运行测试用例:

复制代码
import pytest
if __name__ == "__main__":
    pytest.main(['-vs','--reruns=2','-k','01'])

指定模块运行测试用例:

复制代码
pytest.main(['-vs','testcases/test_01.py'])

指定文件夹运行测试用例:

复制代码
pytest.main(['-vs','testcases/'])

通过node id的方式运行测试用例

复制代码
pytest.main(['-vs','testcases/test_02.py::Test02::test_02'])
2.命令行方式

先切换到要执行的项目根目录下,然后执行pytest 命令

  • 无参数:
    pytest
  • 有参数:
    pytest -vs testcases/test_02.py::Test02::test_01
3.通过pytest.ini的配置文件运行

注:不管是命令行还是主函数都会读取这个配置文件

  • pytest\] 用于标记这个文件是pytest的配置文件

  • testpaths = testcases/ 配置搜索测试用例的范围
  • python_files = test_*.py 改变默认的文件搜索规则
  • python_classes = Test* 改变默认的类搜索规则
  • python_functions = test_* 改变默认的测试用例的搜索规则。
  • markers = 用例分组
    • smoke:冒烟用例
    • product:商品模块用例

特别提示:此文件中最好不要出现中文, 如果有中文的情况下,比如使用notpad++改成GBK的编码。

pytest.ini 文件内容:

复制代码
[pytest]
addopts = -vs -m smoke
testpaths =  testcases/	
python_files = test_*.py   
python_classes = Test*   
python_functions = test_*   
markers =
    smoke: smoke testcases
    product: product testcases

用例里面加标记:

执行的时候通过-m参数指定标记:addopts = ‐vs ‐m smoke

复制代码
import pytest

class Test02:
    @pytest.mark.smoke
    def test_01(self):
        print("Test02--test01")
    @pytest.mark.product
    def test_02(self):
        print("Test02--test02")
        raise Exception("自定义异常Test02--test02")

五、pytest 默认执行测试用例的顺序

默认顺序是从上到下

改变默认用例的执行顺序:在用例上加标记:@pytest.mark.run(order=1)

复制代码
import pytest

class Test02:
    @pytest.mark.run(order=2)
    @pytest.mark.smoke
    def test_01(self):
        print("Test02--test01")

    @pytest.mark.run(order=1)
    @pytest.mark.smoke
    def test_02(self):
        print("Test02--test02")
        raise Exception("自定义异常Test02--test02")

注意:有order装饰器的优先,相同的从上到下,然后再是没有装饰器的,负数不起作用。

六、跳过测试用例

1.无条件跳过 @pytest.mark.skip()
复制代码
    @pytest.mark.skip(reason="粒度不需要")
    @pytest.mark.run(order=2)
    @pytest.mark.smoke
    def test_01(self):
        print("Test02--test01")
2.有条件跳过 @pytest.mark.skipif()
复制代码
    version = 3
    @pytest.mark.skipif(version > 2, reason="以后的版本都不执行")
    @pytest.mark.run(order=1)
    @pytest.mark.smoke
    def test_02(self):
        print("Test02--test02")
        raise Exception("自定义异常Test02--test02")

七、用例的前后置,固件,夹具,钩子函数

复制代码
import pytest

def setup_module(self):
    print("在每个模块之前执行")
def teardown_module(self):
    print("在每个模块之后执行")

class Test03:
    @pytest.mark.smoke
    def test_duo_class(self):
        print("多个类的情况")

class Test02:
    def setup_class(self):
        print("在每个类之前执行,应用举例:创建日志对象(若创建多个日志对象,会导致日志出现重复),创建数据库链接")
    def teardown_class(self):
        print("在每个类之后执行,应用举例:销毁日志对象(,关闭数据库链接")
    def setup(self):
        print("在每个用例之前执行,应用举例:日志开始")
    def teardown(self):
        print("在每个用例之后执行,应用举例:日志结束")

    @pytest.mark.run(order=2)
    @pytest.mark.smoke
    def test_01(self):
        print("Test02--test01")

    @pytest.mark.smoke
    def test_02(self):
        print("Test02--test02")
        raise Exception("自定义异常Test02--test02")
相关推荐
blog_wanghao4 小时前
MFC: 文件加解密(单元测试模块)
c++·单元测试·mfc
电商API_180079052477 小时前
批量获取电商商品数据的解决方案|API接口自动化商品采集|item_get 接口详解
java·大数据·前端·爬虫·数据挖掘·数据分析·自动化
码农捻旧14 小时前
基于GitHub Actions+SSH+PM2的Node.js自动化部署全流程指南
node.js·自动化·ssh·github·express
FF-Studio16 小时前
【MPC控制 - 从ACC到自动驾驶】5. 融会贯通:MPC在ACC中的优势总结与知识体系构建
人工智能·matlab·车载系统·自动化·自动驾驶·汽车·能源
Uranus^16 小时前
深入解析Spring Boot与JUnit 5集成测试的最佳实践
spring boot·单元测试·集成测试·junit 5·mockito
Jassy15917 小时前
论坛系统自动化测试实战
python·selenium·单元测试·json
guygg8818 小时前
原生php单元测试
单元测试·log4j·php
溪饱鱼18 小时前
AppAgentx 开源AI手机操控使用分享
android·人工智能·自动化
FF-Studio20 小时前
【MPC控制 - 从ACC到自动驾驶】1 ACC系统原理与MPC初步认知
深度学习·matlab·车载系统·自动化·自动驾驶·汽车·能源
你好我是小美1 天前
自动化安全脚本学习
运维·学习·自动化