【问题标题】:How to use pytest to test python console script output?如何使用 pytest 测试 python 控制台脚本输出?
【发布时间】:2021-03-24 13:18:51
【问题描述】:

假设您有一个需要测试输出的 python 脚本,如下所示:

print("Hello World!")

如何使用 pytest 插件 pytest-console-scripts 自动化测试?

【问题讨论】:

    标签: python console output pytest script


    【解决方案1】:
    1. 安装 pytest-console-scripts
    2. 使用以下脚本进行自动测试:
    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 ==''
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-25
      • 1970-01-01
      相关资源
      最近更新 更多