用于回归测试的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>固定记录输出不可避免的变化
记录的输出可以包含从测试运行更改为测试的数据
运行,例如使用tmpdir
fixture或十六进制对象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秒” “所需计算时间”等。
一个人可以注册多个这样的转换器,这些转换器将应用于 登记顺序。