IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发测试 -> pytest Python单元测试框架用法 -> 正文阅读

[开发测试]pytest Python单元测试框架用法

特此声明:本文参考自 [1] Pajankar A . Python Unit Test Automation[M]. Apress, 2017.

1. pytest

nose 是一个比起过时的、基础的 unittest 更好的、高级的 Python 测试框架,
然而,nose 已经没有处于活跃的开发中,nose 用户无法得到软件功能的相应提升,维护以及支持,不应该作为 Python 开发活动的首选测试框架。
作为过时的、基础的 unittest 测试运行器,nose2 也没有处于活跃的开发中,同样已经过时了。
作为一个 Python 程序开发者,势必需要一个比起 unittest更好的 Python 测试框架,以及处于活跃的开发状态中的 Python 测试框架。

本文介绍 python 测试框架 pytest,其具有技术流行的、开发活跃的、性能高级的优点。

1.1. 安装步骤

  1. 命令提示符键入以下命令:
pip install pytest

图2.1.
图1.1.1. 卸载与安装py.test
2. 通过以下命令可以查看安装结果

py.test --version

1.2. 简易测试

def test_case01():
    assert "helloworld".upper() == "HELLOWORLD"
  1. 键入以下CMD命令行输出精简信息
python -m pytest test_model01.py
py.test test_module01.py
  1. 键入以下CMD命令行输出冗余信息
python -m pytest -v  test_model01.py
py.test test_module01.py -v

在这里插入图片描述
图 1.2.1. 左边与右边命令行分别输出精简信息以及冗余信息
注意: pytest 输出测试内容自身是彩色的。

1.3. 类与方法的测试

  • 第1步,创建测试类
class TestClass01:
    def test_case01(self):
        assert 'helloworld'.upper() == 'HELLOWORLD'
    def test_case02(self):
        assert 'HELLOWORLD'.lower() == 'helloworld'
  • 第2步, 创建_init.py 文件,将所要测试的文件包含在内;本文中即为 test_module01 以及 test_module02
all = ["test_module01", "test_module02"]
  • 第3步,在测试文件夹的父文件夹内,使用 pytest 相关命令执行测试功能,测试的范围可调,范围由大到小依次是:包(文件夹),测试模块,测试类,测试类中的方法,测试函数
py.test test  
py.test -v test
py.test -v test/test_module01.py
py.test -v test/test_module02.py::TestClass01
py.test -v test/test_module02.py::TestClass01::test_case01
py.test -v test/test_module01.py::test_case01

在这里插入图片描述
另外,类似常规的unittest、nose测试工具,pytest 能够发现和自动运行测试。

  • 第3步*,也可以在测试文件夹中,运行以下命令也可以执行包(文件夹)测试功能
py.test
py.test -v

在这里插入图片描述
图 1.3.2. 命令行输出精简信息 & 冗余信息

1.3. xUnit-Style 测试夹具

1.3.1. xUnit-Style 测试夹具作用域

名称层级调用时间
setup_module() & teardown_module()模块在模块中的任何其他行为之前和之后都会调用。
setup_class() & teardown_class()在类中的任何其他行为之前和之后都会调用,需要使用@classmethod()修饰符标志。
setup_method() &t eardown_method()方法在每种测试方法测试方法之前和之后运行。
setup_function() & teardown_function()函数在系统中的每个测试函数的之前和之后运行。
  • 运行以下命令查看上述测试夹具运行细节的日志
py.test -vs test_module03.py 
py.test -v test_module03.py

在这里插入图片描述
py.test 支持 unittest编写的xUnit-Style (x单元测试风格)测试夹具以及nose编写的大多数测试夹具,此外,py.test 还提供自定义的测试夹具。

1.4. py.test 自定义测试夹具

  1. 输入以下命令建立针对函数的自定义测试夹具
import pytest


@pytest.fixture()
def fixture01():
    print("\nIn fixture01()...")


def test_case01(fixture01):
    print("\nIn test_case01()...")

  1. 输入以下命令建立针对测试类的自定义测试夹具
import pytest


@pytest.fixture()
def fixture01():
    print("\nIn fixture01()...")


@pytest.mark.usefixtures('fixture01')
class TestClass03:
    def test_case01(self):
        print("I'm the test_case01")


    def test_case02(self):
        print("I'm the test_case02")

在这里插入图片描述
3. 输入以下命令建立针对测试类的自定义测试夹具

import pytest


@pytest.fixture()
def fixture01():
    print("\nIn fixture01()...")


@pytest.mark.usefixtures('fixture01')
class TestClass03:
    def test_case01(self):
        print("I'm the test_case01")

    def test_case02(self):
        print("I'm the test_case02")
  1. 输入以下命令添加具有终结器的、基于函数的自定义测试夹具
import pytest


@pytest.fixture()
def fixture01(request):
    print("\nIn fixture...")

    def fin():
        print("\nFinalized...")
    request.addfinalizer(fin)


@pytest.mark.usefixtures('fixture01')
def test_case01():
    print("\nI'm the test_case01")

在这里插入图片描述

1.4.1. 自定义夹具作用域

类似 xUnit-Style 夹具,py.test 的作用域可以通过 @pytest.fixture(scope=“class”)中scope的值进行指定。默认情况下,scope的值为function,函数测试夹具将分配给所有测试。

scope 值作用域
function每个测试运行一次
class每个测试类运行一次
module每个模块运行一次
session每个会话运行一次

下面示例将展示默认情况下测试夹具的作用域。
在这里插入图片描述

进一步阅读

通过在命令行提示符键入下方命令可以获得关于py.test的进一步帮助。

py.test -h
py.test --help

参考文献

PEP 8 惯例
xUnit 模式

  开发测试 最新文章
pytest系列——allure之生成测试报告(Wind
某大厂软件测试岗一面笔试题+二面问答题面试
iperf 学习笔记
关于Python中使用selenium八大定位方法
【软件测试】为什么提升不了?8年测试总结再
软件测试复习
PHP笔记-Smarty模板引擎的使用
C++Test使用入门
【Java】单元测试
Net core 3.x 获取客户端地址
上一篇文章      下一篇文章      查看所有文章
加:2022-05-05 11:49:55  更:2022-05-05 11:50:01 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 13:43:05-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码