python - 如何使用 pytest 测试 python 控制台脚本输出?
问题描述
假设您有一个需要测试输出的 python 脚本,如下所示:
print("Hello World!")
如何使用 pytest 插件 pytest-console-scripts 进行自动化测试?
解决方案
- 安装 pytest-console-scripts
- 使用以下脚本进行自动测试:
from pytest_console_scripts import ScriptRunner
class TestHelloWorld:
def test_success(self, script_runner: ScriptRunner) -> None:
ret = script_runner.run('helloworld.py', print_result=True, shell=True)
assert ret.success
assert ret.stdout == 'Hello World!\n'
assert ret.stderr ==''
推荐阅读
- python - 如何通过 Django App 中的 Mixins 跟踪 ListView、其他视图和 url
- perl - 如何通过 last/next 强制退出 perl 子例程/闭包以自动使程序失败?
- ruby-on-rails - 在 Rails 中,当我运行 bin/rspec 时如何运行所有模型测试?
- angular6 - 如何从 y 分量执行 x 分量中的函数?
- wordpress - 关闭后,华丽的弹出式 Wordpress 音频简码继续播放
- node.js - React 从服务器拉取 SQL 数据
- facebook - 向 Facebook 应用程序订阅页面时出错 - 代码 200(权限不足)
- mysql - 如何在 SQL 中进行条件插入
- excel-formula - 如何在excel中做四倍IF
- python - 请求 Python Jupyter Notebook 等待输入并避免超时