用于数据分析管道功能测试的pytest插件
项目描述
pytest-pipeline
pytest-pipeline是用于数据分析管道功能测试的pytest插件。它们通常是具有多个输入和/或输出文件/目录的长时间运行的脚本或可执行文件。该插件适用于端到端测试,您可以在管道运行之前和之后测试条件(输出文件、校验和等)。
pytest-pipeline已在Python版本2.7、3.3、3.4、3.5和3.6上进行了测试。
安装
pip install pytest-pipeline
安装指南
在我们的示例中,我们将使用一个超级简单的管道,该管道写入文件并打印到stdout
#!/usr/bin/env python
from __future__ import print_function
if __name__ == "__main__":
with open("result.txt", "w") as result:
result.write("42\n")
print("Result computed")
目前这只是一个简单的脚本,但它应该足以说明插件。此外,如果您想跟上来,将上述文件保存为run_pipeline。
有了上述管道,以下是您的测试看起来像pytest_pipeline
import os
import shutil
import unittest
from pytest_pipeline import PipelineRun, mark, utils
# we can subclass `PipelineRun` to add custom methods
# using `PipelineRun` as-is is also possible
class MyRun(PipelineRun):
# before_run-marked functions will be run before the pipeline is executed
@mark.before_run
def test_prep_executable(self):
# copy the executable to the run directory
shutil.copy2("/path/to/run_pipeline", "run_pipeline")
# ensure that the file is executable
assert os.access("run_pipeline", os.X_OK)
# a pipeline run is treated as a test fixture
run = MyRun.class_fixture(cmd="./run_pipeline", stdout="run.stdout")
# the fixture is bound to a unittest.TestCase using the usefixtures mark
@pytest.mark.usefixtures("run")
# tests per-pipeline run are grouped in one unittest.TestCase instance
class TestMyPipeline(unittest.TestCase):
def test_result_md5(self):
assert utils.file_md5sum("result.txt") == "50a2fabfdd276f573ff97ace8b11c5f4"
def test_exit_code(self):
# the run fixture is stored as the `run_fixture` attribute
assert self.run_fixture.exit_code == 0
# we can check the stdout that we capture as well
def test_stdout(self):
assert open("run.stdout", "r").read().strip() == "Result computed"
如果上面的测试保存为test_demo.py,您可以通过执行py.test -v test_demo.py来运行测试。您应该看到执行了三个测试,全部四个都通过了。
发生了什么?
您刚刚执行了您的第一个管道测试。该插件本身为您提供
测试目录创建(每个类获得一个目录)。默认情况下,测试目录都创建在/tmp/pipeline_test目录下。您可以通过提供--base-pipeline-dir命令行标志来调整此位置。
管道的自动执行。无需import subprocess,只需通过PipelineRun对象定义命令。我们可选地将标准输出捕获到名为run.stdout的文件中。不喜欢做磁盘I/O?您还可以将stdout和/或stderr设置为True,并让它们的值在内存中捕获。
超时控制。对于长时间运行的管道,您还可以提供一个timeout参数,该参数限制管道进程可以运行的时间。
由于这是一个py.test插件,测试发现和执行是通过py.test完成的。
获取和提供帮助
请使用问题跟踪器来报告错误或功能请求。您也可以分叉并提交一个pull请求。
许可证
查看LICENSE。
变更日志
版本 0.3
发布 0.3.0
发布日期:2017年1月24日
允许在内存中捕获stdout和/或stderr。这可以通过在创建运行固定时设置相应的关键字参数为True来实现。
版本 0.2
发布 0.2.0
发布日期:2015年3月31日
管道运行现在以不同的方式建模。它们不再是类属性,而是现在作为pytest固定点创建。这使得管道运行可以在非unittest.TestCase测试中使用。
after_run装饰器已弃用。
命令行标志–xfail-pipeline和–skip-run已弃用。
版本 0.1
发布 0.1.0
发布日期:2014年8月25日
首次在PyPI上发布。
项目详情
pytest-pipeline-0.3.0.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | ae366e78d35b61f7a38c1aacb70ff5d296c002f8231030d2d5de786039cce38e |
|
MD5 | 6151c8c9edb2406b0359d171022ee212 |
|
BLAKE2b-256 | edec67d86da83fcfafb8ee040e2a6edd9cb9aeb9e1ea218eb8a612bad563b2d5 |