使用带参数的pytest运行Python脚本

问题描述:

我想使用pytest执行此脚本,其参数将由同一脚本使用。使用带参数的pytest运行Python脚本

这是脚本的名称test.py

#!/usr/bin/env python 
import subprocess 
import os 
import sys 
import pytest 
a = sys.argv[3] # the argument that we need 
if len(a)== "0": 
    sys.exit(1) 
def test_function(): 
    assert a == "g" 

我想在这种情况下 运行使用pytest与争论这个脚本的说法是:g

我试过命令pytest -q test.py g,但它不起作用。

+0

A = sys.argv中[2]#我们所需要的参数 - ------------->输入错误 –

+0

如果你通过'g','pytest'认为它是一个文件参数。你为什么要通过这样的测试参数? – Nurjan

py.test扫描测试并运行它们。将相同的参数传递给每个测试文件是一个坏主意。

你可以让你测试 '自运行':

if __name__ == "__main__": 
    pytest.main("-vv") 
    # or pytest.main("-vv --cov --cov-report term-missing -s") 

然后chmod +x文件并手动运行:./test_something.py