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的配置文件
  • addopts = -vs 命令行参数,多个参数之间用空格分隔。 addoptions
  • 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")
相关推荐
承渊政道1 天前
【Python编程快速上手—让繁琐工作自动化】(字典和结构化数据:用键值对搭出棋盘和游戏背包)
python·游戏·pycharm·自动化·字典和结构化数据
2601_967980112 天前
python pytest怎样测试代码
python·pytest·测试·文档·断言
zhensherlock2 天前
Protocol Launcher 系列:CodeHub 移动端 GitHub 协作利器
前端·javascript·typescript·node.js·自动化·github·js
承渊政道2 天前
【Python编程快速上手—让繁琐工作自动化】(列表:把数据排成队,从下标取值到深拷贝)
开发语言·python·pycharm·自动化·列表
2601_967980112 天前
python pytest如何编写测试
python·测试用例·pytest·函数·assert
2601_967980112 天前
Python pytest如何实现参数化测试
测试用例·pytest·python测试框架·参数化测试·ids参数
wuyk5552 天前
Python办公自动化-第01讲-环境搭建与自动化认知
python·自动化·pyqt
如夏倾城4 天前
五大远程桌面协议:RDP、SSH、VNC/RFB、Citrix ICA/HDX 与 VMware Blast Extreme
运维·windows·网络协议·安全·自动化·ssh·远程工作
IT大白鼠5 天前
当 AI 开始自动化渗透:FBI ARTEX 警报的防守对策
运维·人工智能·自动化
惜.己5 天前
Java结合idea简单使用Playwright
java·开发语言·自动化