使用 Pytest 调用带有命令行参数的程序

Calling a program with command line parameters with Pytest

这是我第一次使用 Pytest,我有一个使用命令行参数调用的程序,如:

$ myprog -i value_a -o value_b

我不确定如何使用 Pytest 来测试这个程序的输出。给定 value_a 和 value_b 的值,我希望得到我想要测试的特定输出。

我看到的Pytest例子都是指测试函数,比如有这样一个函数:

import pytest

def add_nums(x,y):
    return x + y

def test_add_nums():
    ret = add_nums(2,2)
    assert ret == 4

但我不确定如何使用 Pytest 调用我的程序,而不仅仅是测试单个函数?我是否需要使用 os.system() 然后以这种方式调用我的程序?

在我的程序中,我使用了 argparse 模块。

解决方案基于monkeypatch fixture。在下面的示例中,myprog 从文件 myprog_input.txt 中读取数字并将其加 2 并将结果存储在 myprog_output.txt

  1. 正在测试的程序
cat myprog.py
#!/usr/bin/python3.9
import argparse
import hashlib

def main():
    parser = argparse.ArgumentParser(description='myprog')
    parser.add_argument('-i')
    parser.add_argument('-o')
    args = parser.parse_args()
    with open(args.i) as f:
        input_data=int(f.read())
    output_data=input_data+2
    f.close()
    with open(args.o,"w") as fo:
        fo.write(str(output_data) + '\n')
    fo.close()
    with open(args.o) as fot:
        bytes = fot.read().encode() # read entire file as bytes
    fot.close()
    readable_hash = hashlib.sha256(bytes).hexdigest();
    return readable_hash 


if __name__ == '__main__':
    print(main())
  1. 测试
cat test_myprog.py
#!/usr/bin/python3.9
import sys
import myprog

def test_myprog(monkeypatch):
    with monkeypatch.context() as m:
        m.setattr(sys, 'argv', ['myprog', '-i', 'myprog_input.txt', '-o', 'myprog_output.txt'])
        assert myprog.main() == 'f0b5c2c2211c8d67ed15e75e656c7862d086e9245420892a7de62cd9ec582a06'
  1. 输入文件
cat myprog_input.txt
3
  1. 运行 程序
myprog.py -i myprog_input.txt -o myprog_output.txt
f0b5c2c2211c8d67ed15e75e656c7862d086e9245420892a7de62cd9ec582a06
  1. 正在测试程序
pytest test_myprog.py
============================================= test session starts =============================================
platform linux -- Python 3.9.5, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /home/<username>/py
plugins: hypothesis-6.23.1
collected 1 item                                                                                              

test_myprog.py .                                                                                        [100%]
============================================== 1 passed in 0.04s ==============================================