pytest | 框架的简单使用

这里写目录标题

pytest框架的使用示例

pytest将运行当前目录及其子目录中test_*.py*_test.py 形式的所有 文件

文件内的函数名称 可以test* 或者test_* 开头

单个文件测试方法

  • 运行方式- terminal

    pytest 不指定任何参数表示将执行当前目录下的所有test_文件 如上述pytest目录下的三个测试函数

F 代表测试失败,点 (.) 代表测试成功。

-q 选项会让 pytest 以"quiet"模式运行,这意味着它将减少输出的日志信息,只显示测试结果的摘要。

pytest 命令将执行所有格式的文件*test_*要么_test**在当前目录和子目录中

  • 指定文件运行

pytest test_xx.py

  • 指定类实例名称运行

pytest -k TestClassDemoInstance -q

python 复制代码
class TestClassDemoInstance:
    value = 0
    def test_one(self):
        self.value = 1
        assert self.value == 1

    def test_two(self):
        assert self.value == 1

以下是命令 pytest -k TestClassDemoInstance -q 的详细解释:

  • pytest:这是调用Python测试框架的命令。
  • -k TestClassDemoInstance:这个参数告诉 pytest 只运行测试名称包含 TestClassDemoInstance 的测试用例。【表示要在测试名称中搜索的子字符串】这可以是完整的测试函数名,也可以是类名或部分名称。
  • -q:这是 pytest 的一个命令行选项,表示"quiet",即减少控制台输出。
  • TestClassDemoInstance:这是你想要运行的测试类或测试函数名称的一部分。

执行这个命令后,pytest 会搜索所有测试文件,查找名称中包含 TestClassDemoInstance 的测试用例,并只运行这些测试。【截至目前运行的测试文件,只有一个文件含有TestClassDemoInstance 实例】测试结果将以简洁的格式输出,通常只包含测试的总数和失败的测试数量。

  • 查看详细的运行信息

pytest -v test_square.py

python 复制代码
import math

def test_sqrt():
   num = 25
   assert math.sqrt(num) == 5

def testsquare():
   num = 7
   assert 7*7 == 40

def tesequality():
   assert 10 == 11

执行测试套件的子集

Pytest 提供了两种方法来运行测试套件的子集:

  • 根据测试名称的子字符串匹配选择要运行的测试。
  • 根据应用的标记选择要运行的测试组

测试名称的子字符串

  • 测试文件所在目录内容下的文件

测试函数中只包含great字样的两个测试的文件内容如下 :

python 复制代码
# test_square.py文件
import math

def test_sqrt():
   num = 25
   assert math.sqrt(num) == 5

def testsquare():
   num = 7
   assert 7*7 == 40

def tesequality():
   assert 10 == 11

def test_greater_num():
   num = 100
   assert num == 100
    
# test_compare.py文件
def test_greater():
   num = 100
   assert num > 100
def test_greater_equal():
   num = 100
   assert num >= 100
def test_less():
   num = 100
   assert num < 200
  • 执行测试

1 failed, 2 passed, 11 deselected in 0.12s 在结果中,我们可以看到 11 个测试被取消选择。这是因为那些测试名称不包含单词great在他们之中

根据应用的标记进行选择

类似于装饰器的作用,使用的方式如下

python 复制代码
@pytest.mark.<markername>
  • 执行
python 复制代码
pytest -m <markername> -v
python 复制代码
import pytest

@pytest.mark.great
def test_greater():
   num = 100
   assert num > 100

@pytest.mark.great
def test_greater_equal():
   num = 100
   assert num >= 100


@pytest.mark.others
def test_less():
   num = 100
   assert num < 200

其他常见的测试命令

  • 安装 pytest...
  • 识别测试文件和测试功能。
  • 使用 pytest -v 执行所有测试文件。
  • 使用 pytest -v 执行特定文件。
  • 通过匹配 pytest -k -v 的子字符串执行测试。
  • 基于标记 pytest -m <marker_name> -v 执行测试。
  • 使用 @pytest.fixture 创建夹具。
  • conftest.py 允许从多个文件访问固定装置。
  • 使用 @pytest.mark.parametrize 参数化测试。
  • 使用 @pytest.mark.xfail 进行 Xfailing 测试。
  • 使用 @pytest.mark.skip 跳过测试。
  • 使用 pytest --maxfail = 在 n 次失败时停止测试执行。
  • 使用 pytest -n 并行运行测试。
    使用 @pytest.mark.parametrize 参数化测试。
  • 使用 @pytest.mark.xfail 进行 Xfailing 测试。
  • 使用 @pytest.mark.skip 跳过测试。
  • 使用 pytest --maxfail = 在 n 次失败时停止测试执行。
  • 使用 pytest -n 并行运行测试。
  • 使用 pytest -v --junitxml = "result.xml" 生成结果 xml。
相关推荐
大耳朵-小飞象3 小时前
电力安全运维的智能密码:BACS如何破解设备全生命周期管理难题,让电网安全“看得见、管得住”?
运维·安全·智慧城市·能耗系统·楼宇智控·未来生活
极客侃科技4 小时前
制造企业 MES/APS 选型:SAP PP/DS 集成、ERP-MES 边界划分与一体化架构要点
运维·架构·制造
生活爱好者!7 小时前
我把NAS当作下载机,docker一键部署qb
运维·docker·容器
尽兴-8 小时前
企业业务系统架构选型与渐进式演进
运维·系统架构·devops
通信小小昕9 小时前
Ubuntu 26.04 中文输入法安装
linux·运维·ubuntu
壮哥_icon9 小时前
【Android 系统开发】使用 BAT 脚本高效自动化管理 /system/priv-app/ 系统应用(安装与卸载)
android·运维·自动化
RD_daoyi9 小时前
外链权重暴跌至13%,品牌提及反超传统链接——2026年不做Digital PR,你的独立站等于隐形
运维·网络·学习·机器学习·搜索引擎
电商API_1800790524710 小时前
企业ERP进销存场景|京东商品详情接口自动同步方案|凭证鉴权批量调用技术实操
大数据·运维·人工智能·爬虫·数据挖掘·网络爬虫
张小姐的猫10 小时前
【Linux】网络编程 —— HTTP协议(上)
linux·运维·服务器·网络·http·单例模式·策略模式
小稻穗10 小时前
市场调研样本选型坐标系:2026六家国内样本平台能力横向校验
大数据·运维·数据分析