pytest框架基础

1.生成时间戳的html报告

pytest 运行项目
import pytest
from datetime import datetime

获取时间戳,now()获取现在的时间,strftime()转化成字符串
report_time = datetime.now().strftime("%Y%m%d%H%M%S")
字符串拼接
filename = f'pyreport-{report_time}.html'
收集用例并运行用例
命令行参数放入列表中
pytest.main([f"--html={filename}"])

2.fixture夹具

实现夹具复用,就要封装,测试用例函数调用夹具时,就需要导入模块,但是每次都导入,容易出现错误,那么有没有一种方法可以实现不需要导入而自动读取呢?共享fixture的出现就解决了这个问题。

共享fixture的实现:

1)将所有的夹具全部放到一个固定的模块文件, conftest.py 文件名固定

conftest.py:

import pytest

声明这是一个夹具,这个夹具就是个函数
@pytest.fixture()
def fixt():
    # setUp
    print("每次测试都会执行的")
    # yield 分割线 前置和后置
    yield
    # tearDown
    print("每次测试用例后都会执行的")

2)所有导入夹具的操作就可以省略,pytest运行时会自动在 conftest.py中查找

测试用例:不需要进行导入操作

import pytest

class TestFixture:
    # 调用夹具
    def test_fixture(self, fixt):
        assert 1 + 1 == 2

class TestFixture1:
    # 调用夹具
    def test_fixture1(self, fixt):
        assert 1 + 1 == 2

运行结果:

pytest的夹具除了有共享fixture的特性外,还有非常灵活的作用域管理。

  • function
  • class
  • module
  • package
  • session

定义夹具conftest.py:

import pytest

声明这是一个夹具,这个夹具就是个函数
@pytest.fixture()
def fixt():
    # setUp
    print("每次测试都会执行的")
    # yield 分割线 前置和后置
    yield
    # tearDown
    print("每次测试用例后都会执行的")

@pytest.fixture()
def function_fixt():
    # setUp
    print("function_fixt start")
    # yield 分割线 前置和后置
    yield
    # tearDown
    print("function_fixt finished")

class级别必须定义scope='class'
@pytest.fixture(scope='class')
def class_fixt():
    # setUp
    print("class_fixt start")
    # yield 分割线 前置和后置
    yield
    # tearDown
    print("class_fixt finished")

module级别必须定义scope='module'
@pytest.fixture(scope='module')
def module_fixt():
    # setUp
    print("module_fixt start")
    # yield 分割线 前置和后置
    yield
    # tearDown
    print("module_fixt finished")

测试用例:

class TestFixtureFunction:
    def test_fixture_function(self, function_fixt, class_fixt,module_fixt):
        assert 1 + 1 == 2

    def test_fixture_function_2(self, function_fixt, class_fixt,module_fixt):
        assert 1 + 1 == 2

class TestFixtureFunction2:
    def test_fixture_function21(self, function_fixt, class_fixt,module_fixt):
        assert 1 + 1 == 2

    def test_fixture_function_22(self, function_fixt, class_fixt,module_fixt):
        assert 1 + 1 == 2

运行结果(从运行结果中我们可以知道不同作用域的夹具执行的顺序和次数):

从上例中可以看出,每次测试用例调用夹具时,还需要写上夹具的方法名称,有没有方法实现自动调用呢?

只需要在 conftest.py文件中封装夹具方法时,在声明中添加参数 autouse=True

import pytest

声明这是一个夹具,这个夹具就是个函数
@pytest.fixture(autouse=True)
def fixt():
    # setUp
    print("每次测试都会执行的")
    # yield 分割线 前置和后置
    yield
    # tearDown
    print("每次测试用例后都会执行的")

@pytest.fixture(scope='function', autouse=True)
def function_fixt():
    # setUp
    print("function_fixt start")
    # yield 分割线 前置和后置
    yield
    # tearDown
    print("function_fixt finished")

class级别必须定义scope='class'
@pytest.fixture(scope='class', autouse=True)
def class_fixt():
    # setUp
    print("class_fixt start")
    # yield 分割线 前置和后置
    yield
    # tearDown
    print("class_fixt finished")

module级别必须定义scope='module'
@pytest.fixture(scope='module', autouse=True)
def module_fixt():
    # setUp
    print("module_fixt start")
    # yield 分割线 前置和后置
    yield
    # tearDown
    print("module_fixt finished")

测试用例:

class TestFixtureFunction:
    def test_fixture_function(self):
        assert 1 + 1 == 2

    def test_fixture_function_2(self):
        assert 1 + 1 == 2

class TestFixtureFunction2:
    def test_fixture_function21(self):
        assert 1 + 1 == 2

    def test_fixture_function_22(self):
        assert 1 + 1 == 2

运行结果:

3.参数化parametrize

pytest的参数化和夹具与unittest不兼容。

import pytest

data = [1, 2, 3]

class TestParams:

    # info存储遍历data的结果
    @pytest.mark.parametrize('info', data)
    # 测试用例参数必须与声明中获取的变量同名即info
    def test_params(self, info):
        print(info)
        assert 1 + 1 == 2

运行结果:

实战当中会有两种模式:

模式1:

1,unittest编写用例,使用unittest ddt 和 夹具 setUp

2,pytest运行

模式2:

全部用pytest,完全舍弃unittest

4.用例筛选

  • 第一步,提前把标记名注册到 pytest 里面。

有两种方法:

1)pytest.ini文件中配置

[pytest]
markers =
    slow: marks tests as slow (deselect with '-m "not slow"')
    serial

2) pyproject.toml文件中配置

[tool.pytest.ini_options]
markers = [
    "slow: marks tests as slow (deselect with '-m \"not slow\"')",
    "serial",
]

如本例中是在pytest.ini(存放在项目目录下)进行配置:

[pytest]
markers =
    success
    login
  • 第二步,通过pytest在用例方法或者测试类上做一个标记;
import pytest

class TestMarker:
    # 调用夹具
    @pytest.mark.success
    def test_mark(self):
        assert 1 + 1 == 2

    def test_mark1(self):
        assert 1 + 1 == 2

@pytest.mark.success
class TestMarker1:
    # 调用夹具
    @pytest.mark.login
    def test_mark(self):
        assert 1 + 1 == 2

    def test_mark1(self):
        assert 1 + 1 == 2
  • 第三步,在运行用例时,通过标记执行。

进入目录

使用 pytest -m “success” (必须用双引号)指令运行有 success标记的测试类和测试方法。

使用 pytest -m “login and success”(必须用双引号)指令运行有 successlogin标记的测试类和测试方法。

使用 pytest -m “login or success”(必须用双引号)指令运行有 successlogin标记的测试类和测试方法。

标记支持逻辑运算 and or not ,如” not success” 反向执行。

Original: https://blog.csdn.net/Ly_LittleStar/article/details/121362235
Author: 胖困困
Title: pytest框架基础

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/772736/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • Matplotlib day1 for cvpytorch

    英文官方教程链接 https://matplotlib.org/stable/tutorials/introductory/pyplot.html https://matplotl…

    Python 2023年9月2日
    045
  • scrapy 搜索关键字_Scrapy抓取动态网页

    动态网页指几种可能: 1)需要用户交互,如常见的登录操作; 2)网页通过JS/ AJAX动态生成,如一个html里有 aaa 3)点击输入关键字后进行查询,而浏览器url地址不变 …

    Python 2023年10月4日
    039
  • conda命令记录、torch、torchvision安装

    目录 一、conda 创建、激活、退出、删除虚拟环境 1、conda 本地环境常用操作 2、conda创建虚拟环境 3、激活虚拟环境 4、退出虚拟环境 5、删除和复制虚拟环境 6、…

    Python 2023年9月8日
    079
  • drf 频率类

    频率类源码 入口: 入口: 1)APIView的dispath方法的 self.initial(request,*args,**keargs)点进去 2) self。check_t…

    Python 2023年6月12日
    051
  • 全面Centos7部署django+nginx+uwsgi+mysql详细步骤

    没事写了个网站,用内网穿透玩的很是不过瘾,正巧华为云搞活动,就像把项目部署到华为云上,下面是步骤也是经历:背景:本人在本地写代码测试的时候,由于电脑配置实在是不行,数据库用的事sq…

    Python 2023年8月4日
    049
  • pytest+Allure+Gitee+jenkins接口自动化实现持续集成

    记录下自己工作中的接口自动化框架实现持续集成的过程: 1.接口测试框架:Pytest2.使用Gitee作为本次记录(工作当中用的是GitLab)3.报告使用Allure4.持续集成…

    Python 2023年9月10日
    045
  • pytest + yaml 框架 -4.用例参数化parameters功能实现

    当一个用例用到多组测试数据的时候,我们必然会用到参数化,接下来看下如何在yaml文件中实现参数化 pip 安装插件 pip install pytest-yaml-yoyo 参数化…

    Python 2023年9月13日
    047
  • 构建端到端的开源现代数据平台

    了解使用开源技术构建现代数据栈的详细指南。 在过去的几年里,数据工程领域的重要性突飞猛进,为加速创新和进步打开了大门——从今天开始,越来越多的人开始思考数据资源以及如何更好地利用它…

    Python 2023年10月17日
    036
  • Python Scrapy遇到的问题(二)

    目录 Scrapy xx.py文件中import出错的问题 Scrapy开启多条管道下载时的问题 (一) settings中管道设置名错误 (二)下载图片时看不到图片/找不到存放图…

    Python 2023年10月4日
    047
  • 【Python K均值聚类算法】

    K均值聚类算法 1. 什么是聚类 * 1.1 聚类概念: 1.2 聚类常用的距离判定: 1.3 聚类目的: 2. K均值算法实现过程 * 2.1 K是什么? Means是什么? 2…

    Python 2023年8月3日
    063
  • 【Django | 安全防护】CSRF跨站伪请求和SQL注入攻击

    🤵‍♂️ 个人主页: @计算机魔术师👨‍💻 作者简介:CSDN内容合伙人,全栈领域优质创作者。 🌐 推荐一款找工作神器网站: 牛客网🎉🎉|笔试题库|面试经验|实习招聘内推还没账户的…

    Python 2023年8月6日
    077
  • 【c++提高1】二叉树&二叉堆(万字总结)

    大纲 一、二叉树 二叉树:1.二叉树简介二叉树:2.二叉树的性质二叉树:3.二叉树的存储二叉树:4.二叉树的遍历二叉树:5.求解先序、后序、层次遍历序列二叉树:6.例题 二、二叉堆…

    Python 2023年9月17日
    040
  • 【原创】i.MXRT J-Flash烧写算法使能eFuse熔丝位写入

    ​临近年底,终于又憋了一篇文章出来,本来年初的时候是有计划把去年总结的一些东西整理下发布出来的,结果还是被工作和生活上各种琐事给耽搁了。哎,今年刚过了自己35岁的生日,眼瞅着这个人…

    Python 2023年10月17日
    024
  • 使用SimpleITK读取、保存、处理nii文件

    目录 前言 nii格式 读取nii成numpy格式 将numpy格式保存成nii 什么是origin、Direction、Spacing,以及如何设置它们 示例 重采样 * 重采样…

    Python 2023年8月24日
    071
  • sklearn中的决策树(分类)

    本文在我的知乎上同步更新:sklearn中的决策树(分类) – 知乎 Sklearn库有很多机器学习模型,不同的模型有着不同的特点,针对不同的问题,选取对应的模型,可以…

    Python 2023年8月2日
    050
  • list

    1.内存不连续 2.插入删除方便,不用整体移动,只需要断开相应指针 3.访问难受,不管访问哪个,都需要从第一个开始遍历 4.定义 5.初始化 6.基本操作 7. 8. 9.遍历,内…

    Python 2023年6月12日
    086
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球