用于回归测试的pytest插件

pytest-regtest的Python项目详细描述


pytest regtest

pytest regtest是一个用于实现回归测试的pytest-插件。 与功能测试相比,回归测试不测试 软件产生正确的结果,相反,回归测试检查 软件的行为与引入更改前的相同。

有关回归测试的详细信息,请访问 https://en.wikipedia.org/wiki/Regression_testing。回归检验 是重构遗留代码时开始使用的常用技术 缺少测试套件。

pytest regtest允许捕获选定的输出,然后可以 与以前运行的捕获输出进行比较。

要安装并激活此插件,请执行:

$ pip install pytest-regtest

pytest regtest插件提供一个名为regtest的fixture,它可以是 用作记录数据的文件句柄:

from __future__ import print_function

def test_squares_up_to_ten(regtest):

    result = [i*i for i in range(10)]

    # one way to record output:
    print(result, file=regtest)

    # alternative method to record output:
    regtest.write("done")

    # or using a context manager:
    with regtest:
        print("this will be recorded")

如果您第一次使用pytest运行这个测试脚本 到目前为止,此测试功能的记录输出,因此测试将 包含diff的消息失败:

$ py.test
...

regression test output differences for test_demo.py::test_squares_up_to_ten:

>   --- current
>   +++ tobe
>   @@ -1,2 +1 @@
>   -[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>   -done
>   +

输出告诉我们电流输出是什么,“tobe”输出 仍然是空的。

为了接受这个输出,我们运行pytest--reset regtest 标志:

$ py.test --regtest-reset

现在,py.test的下一次执行将成功:

$ py.test

现在,我们通过修改被测代码来计算第一个 十一个方号:

from __future__ import print_function

def test_squares_up_to_ten(regtest):

    result = [i*i for i in range(11)]  # changed !

    # one way to record output:
    print(result, file=regtest)

    # alternative method to record output:
    regtest.write("done")

pytest的下一次运行提供了很好的电流和预期输出的差异 从这个测试函数:

$ py.test

...
>   --- current
>   +++ tobe
>   @@ -1,2 +1,2 @@
>   -[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>   +[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>    done

记录的输出已写入子文件夹中的文件 _regtest_outputs在测试脚本旁边。你可以留着这个 文件夹处于版本控制下。

其他功能

另一种记录输出的方法是regtest重定向fixture:

def test_squares_up_to_ten(regtest_redirect):

    result = [i*i for i in range(10)]

    with regtest_redirect():
        print result

您可以将文件和功能的录制输出分别重置为:

$ py.test --regtest-reset tests/test_00.py
$ py.test --regtest-reset tests/test_00.py::test_squares_up_to_ten

要抑制差异并仅查看统计信息,请使用:

$ py.test --regtest-nodiff

要在测试执行运行期间查看录制的输出:

$ py.test --regtest-tee -s

如果你在混合平台上开发的话,忽略白色可能是有用的。 比较输出时,在行尾加空格。这可能是 通过指定:

$ py.test --regtest-ignore-line-endings
< H2>固定记录输出不可避免的变化

记录的输出可以包含从测试运行更改为测试的数据 运行,例如使用tmpdirfixture或十六进制对象id创建的路径, 打印对象时。

插件已经在记录的输出中替换了这些变化的数据, 在测试中可以在conftest.py中注册自己的转换器 文件夹。例如:

import pytest_regtest

@pytest_regtest.register_converter_pre
def fix_before(txt):
    """modify recorded output before the default fixes
    like temp folders or hex object ids are applied"""

    # remove lines with passwords:
    lines = txt.split('\n')
    lines = [l for l in lines if "password is" not in l]
    return '\n'.join(lines)

@pytest_regtest.register_converter_post
def after(txt):
    """modify recorded output after the default fixes
    like temp folders or hex object ids are applied"""

    # for demo only
    return txt.upper()

这可用于修复子字符串,如“计算需要1.23秒” “所需计算时间”等。

一个人可以注册多个这样的转换器,这些转换器将应用于 登记顺序。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
文件名的java正则表达式限制名称大小和文件扩展名   Mac上的java Android SDK:jspawnhelper意外退出   java SQL Server 2000到Oracle 12c重音字符   在Java中快速比较大数据集中的值和小数据集中的值   java在代码中的许多地方保留对对象的引用   Java规范中私有内部类的jvm访问标志与反射API不一致?   比较2个int数组中匹配的数字   java Apache Commons数学简化回归:get prediction stderr   安卓 Java SDK管理器因命令行输出中的“flashplayerplugin”而崩溃   JavaSQLite:关闭DB时必须关闭游标吗?   泛型Java设计抽象类声明示例说明   java应用程序在添加片段时崩溃   如何在java中使用注释为字段加载值