使用 Pytest 进行测试

介绍

使用 pytest 进行测试

pytest可用于所有类型和级别的软件测试。许多项目——包括 Mozilla 和 Dropbox——从 unittest 或nose 切换到 pytest。

使用 Pytest 的简单第一个示例

pytest 用于测试的测试文件必须以 test_ 开头或以 _test.py 结尾 我们将通过为文件 fibonacci.py 编写测试文件 test_fibonacci.py 来演示工作方式。两个文件都在一个目录中:

第一个文件是应该测试的文件。我们假设它被保存为 fibonacci_p.py:

def fib(n):
    旧的,新的 = 0, 1
    对于 _ 范围(n):
        旧,新 = 新,旧 + 新
    返老还童

现在,我们必须提供文件 test_fibonacci.py 的代码。'pytest' 将使用此文件:

从 fibonacci_p 导入 fib
def test_fib():
    断言 fib(0) == 0
    断言 fib(1) == 1
    断言 fib(10) == 55

我们在上面显示的两个文件所在的目录中的命令 shell 中调用 pytest:

 pytest 

这段代码的结果如下所示:

============================ 测试会话开始 ================== ============
linux 平台——Python 3.7.1、pytest-4.0.2、py-1.7.0、pluggy-0.8.0
rootdir: /home/bernd/, inifile:
插件:remotedata-0.3.1、openfiles-0.3.1、doctestplus-0.2.0、arraydiff-0.3
收集了 1 项
test_fibonacci.py 。[100%]
============================ 1 在 0.01 秒内通过 ================== ==========

我们现在创建了一个错误的 fib 版本。我们将两个起始值从 0 和 1 更改为值 2 和 1。这是卢卡斯数列的开始,但由于我们要实现斐波那契数列,这是错误的。这样,我们可以研究 pytest 在这种情况下的行为:

def fib(n):
    旧的,新的 = 2, 1
    对于 _ 范围(n):
        旧,新 = 新,旧 + 新
    返老还童

使用斐波那契的这种错误实现调用 'pytest' 会得到以下结果:

$ pytest ============================== 测试会话开始 ================ ==============
linux 平台——Python 3.7.1、pytest-4.0.2、py-1.7.0、pluggy-0.8.0
rootdir: /home/bernd/, inifile:
插件:remotedata-0.3.1、openfiles-0.3.1、doctestplus-0.2.0、arraydiff-0.3
收集了 1 项
test_fibonacci.py F [100%]
====================================失败============== ======================
___________________________________ test_fib ___________________________________
    def test_fib():
> 断言 fib(0) == 0
E 断言 2 == 0
E + 其中 2 = fib(0)
test_fibonacci.py:5: 断言错误
========================== 0.03 秒内失败 1 ================== ==========

另一个 Pytest 示例 (1)

在下一个示例中,我们将更接近“现实”。在现实生活中,我们通常会有多个文件,对于每个文件,我们可能有一个对应的测试文件。每个测试文件可能包含各种测试。我们的示例文件夹 ex2 中有各种文件:

需要测试的文件:

  • 斐波那契.py
  • foob​​ar_plus.py
  • foob​​ar.py

测试文件:

  • test_fibonacci.py
  • test_foobar_plus.py
  • test_foobar.py

我们在目录 'ex2' 中启动 'pytest' 并得到以下结果:

$ pytest
==================== 测试会话开始 ======================
linux 平台——Python 3.7.3、pytest-4.3.1、py-1.8.0、pluggy-0.9.0
rootdir: /home/bernd/ex2, inifile:
插件:remotedata-0.3.1、openfiles-0.3.2、doctestplus-0.3.0、arraydiff-0.3
收集 4 项
test_fibonacci.py 。[ 25%]
test_foobar.py .. [ 75%]
test_foobar_plus.py 。[
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 pytest 进行测试的基本步骤如下: 1. 安装 pytest:在终端或命令行中使用 pip 命令安装 pytest,例如:`pip install pytest` 2. 创建测试文件:在项目目录下创建一个以 `test_` 开头或以 `_test` 结尾的测试文件,例如 `test_example.py`。 3. 编写测试用例:在测试文件中编写测试用例,一个测试用例就是一个普通的函数,以 `test_` 开头的函数名。例如: ```python def test_addition(): assert 1 + 1 == 2 def test_subtraction(): assert 5 - 3 == 2 ``` 4. 运行测试:在终端或命令行中运行 `pytest` 命令,它会自动发现并运行测试文件中的测试用例。例如:`pytest` 5. 查看测试结果:pytest 会输出测试运行的结果,显示每个测试用例的运行状态(通过/失败),以及详细的错误信息(如果有)。例如: ``` ================================ test session starts ================================ ... collected 2 items test_example.py .F [100%] ===================================== FAILURES ====================================== _________________________________ test_subtraction __________________________________ def test_subtraction(): > assert 5 - 3 == 4 E assert (5 - 3) == 4 test_example.py:6: AssertionError ========================== short test summary info =========================== FAILED test_example.py::test_subtraction - assert (5 - 3) == 4 ``` 通过上述步骤,你就可以使用 pytest 来编写和运行测试用例,以验证代码的正确性。除了基本的断言外,pytest 还提供了许多其他功能,如参数化测试测试夹具等,可以根据需要进行使用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值