PyTest自学 - pytest的各种执行方式

<< 返回目录

1 PyTest自学 - pytest的各种执行方式

  • 不带任何参数执行
      在命令行下将目录切换到测试用例所在目录,执行pytest
cmd 复制代码
tyy@DESKTOP-G7V9IT0 ~
$ cd /cygdrive/d/TYYSOFT/Study/Python/pytest

tyy@DESKTOP-G7V9IT0 /cygdrive/d/TYYSOFT/Study/Python/pytest
$ pytest
  • 带用例文件名执行
cmd 复制代码
$ pytest test_feature_subfeature_sample_002.py
  • 执行指定目录下的文件
cmd 复制代码
 pytest ./pytest/

注意:只支持执行子目录下的文件夹,不支持绝对路径。

  • 执行指定文件中的指定函数

    $ pytest test_feature_subfreature_sample_001_001.py::test_feature_subfeature_plus_001_001

输出:

复制代码
======================================= test session starts =======================================
platform win32 -- Python 3.13.1, pytest-8.3.4, pluggy-1.5.0
rootdir: D:\TYYSOFT\Study\Python\pytest
collected 1 item

test_feature_subfreature_sample_001_001.py .                                                 [100%]

======================================== 1 passed in 0.01s ========================================
  • 执行类中的所有用例
cmd 复制代码
$ pytest test_feature_subfeature_sample_002.py::TestGroup

输出:

cmd 复制代码
======================================= test session starts =======================================
platform win32 -- Python 3.13.1, pytest-8.3.4, pluggy-1.5.0
rootdir: D:\TYYSOFT\Study\Python\pytest
collected 2 items

test_feature_subfeature_sample_002.py .F                                                     [100%]

============================================ FAILURES =============================================
________________________ TestGroup.test_feature_subfeature_sample_002_002 _________________________

self = <test_feature_subfeature_sample_002.TestGroup object at 0x0000027875F2A350>

    def test_feature_subfeature_sample_002_002(self):
>       assert 6 + 3 == 7
E       assert (6 + 3) == 7

test_feature_subfeature_sample_002.py:9: AssertionError
===================================== short test summary info =====================================
FAILED test_feature_subfeature_sample_002.py::TestGroup::test_feature_subfeature_sample_002_002 - as
sert (6 + 3) == 7
=================================== 1 failed, 1 passed in 0.07s ===================================
  • 执行指定类中的指定用例
cmd 复制代码
$ pytest test_feature_subfeature_sample_002.py::TestGroup::test_feature_subfeature_sample_002_001

输出:

cmd 复制代码
======================================= test session starts =======================================
platform win32 -- Python 3.13.1, pytest-8.3.4, pluggy-1.5.0
rootdir: D:\TYYSOFT\Study\Python\pytest
collected 1 item

test_feature_subfeature_sample_002.py .                                                      [100%]

======================================== 1 passed in 0.01s ========================================
  • 执行多组测试数据
      使用装饰器@pytest.mark.parametrize可以给测试用例传递多组测试数据,每组测试数据会单独执行。
py 复制代码
import pytest


@pytest.mark.parametrize("input1, input2, expected", [
    (1, 2, 3),
    (4, 5, 7),
    (-1, 1, 0),
    (0, 0, 0)
])
def test_addition(input1, input2, expected):
    assert input1 + input2 == expected

执行结果

cmd 复制代码
$ pytest test_feature_subfeature_withdata_001.py
======================================= test session starts =======================================
platform win32 -- Python 3.13.1, pytest-8.3.4, pluggy-1.5.0
rootdir: D:\TYYSOFT\Study\Python\pytest
collected 4 items

test_feature_subfeature_withdata_001.py .F..                                                 [100%]

============================================ FAILURES =============================================
______________________________________ test_addition[4-5-7] _______________________________________

input1 = 4, input2 = 5, expected = 7

    @pytest.mark.parametrize("input1, input2, expected", [
        (1, 2, 3),
        (4, 5, 7),
        (-1, 1, 0),
        (0, 0, 0)
    ])
    def test_addition(input1, input2, expected):
>       assert input1 + input2 == expected
E       assert (4 + 5) == 7

test_feature_subfeature_withdata_001.py:11: AssertionError
===================================== short test summary info =====================================
FAILED test_feature_subfeature_withdata_001.py::test_addition[4-5-7] - assert (4 + 5) == 7
=================================== 1 failed, 3 passed in 0.06s ===================================
  • 不使用pytest命令行,直接运行py文件执行测试用例
      有些读者觉得老要使用命令行执行用例就比较烦,能不能直接在py文件中直接运行?
    只需要在py源文件中做两件事就可以达到这些效果:
  1. import pytest
  2. 添加pytest.main()

示例如下:

py 复制代码
import pytest


@pytest.mark.parametrize("input1, input2, expected", [
    (1, 2, 3),
    (4, 5, 7),
    (-1, 1, 0),
    (0, 0, 0)
])
def test_addition(input1, input2, expected):
    assert input1 + input2 == expected

pytest.main()

再运行这个文件时,就会执行测试用例!


作者声明:本文用于记录和分享作者的学习心得,可能有部分文字或示例来源自豆包AI,由于本人水平有限,难免存在表达错误,欢迎留言交流和指教!

Copyright © 2022~2025 All rights reserved.

<< 返回目录

相关推荐
微刻时光20 分钟前
影刀RPA开发-CSS选择器介绍
css·python·低代码·自动化·rpa·影刀rpa·影刀实战
水水沝淼㵘43 分钟前
嵌入式开发学习日志(数据结构--单链表)Day20
c语言·开发语言·数据结构·学习·算法
举一个梨子zz44 分钟前
Java—— 可变参数、集合工具类、集合嵌套、不可变集合
java·开发语言·intellij-idea·需求分析
iangyu1 小时前
【windows server脚本每天从网络盘复制到本地】
开发语言·windows·php
程序员拂雨1 小时前
Python知识框架
开发语言·python
灏瀚星空1 小时前
地磁-惯性-视觉融合制导系统设计:现代空战导航的抗干扰解决方案
图像处理·人工智能·python·深度学习·算法·机器学习·信息与通信
泽02021 小时前
C++类和对象之相关特性
java·开发语言·c++
Code_流苏1 小时前
《Python星球日记》 第72天:问答系统与信息检索
python·微调·问答系统·bert·应用场景·基于检索·基于生成
敲键盘的小夜猫1 小时前
深入理解Python逻辑判断、循环与推导式(附实战案例)
开发语言·python
Looooking1 小时前
Python 之 selenium 打开浏览器指定端口进行接续操作
python·selenium