首页 > 解决方案 > 在运行 .exe 文件的 pytest 中使用 2 个参数进行参数化测试

问题描述

@pytest.mark.parametrize("x", range(17))
@pytest.mark.parametrize("y", range(11))
def test_foo(x, y)
my_test.exe, my_command 'x' 'y'

我想从“my_test.exe”运行命令“my_command x y”,x 和 y 来自上述范围。

标签: pythoncmdpytest

解决方案


实际上,您在参数化调用方面有了一个良好的开端,它的工作原理与此完全一样。您只需要在您的方法中添加流程调用。

一个例子:

import subprocess
import pytest

@pytest.mark.parametrize("x", range(17))
@pytest.mark.parametrize("y", range(11))
def test_foo(x, y):
    rc = subprocess.check_call(['/bin/echo', str(x), str(y)])
    assert rc == 0

    stdout = subprocess.check_output(['/bin/echo', str(x), str(y)])
    assert stdout

check_call或者check_output程序出错时会引发异常,因此测试失败。您也可以测试输出。


推荐阅读