避免将pytest参数作为命令行参数传递

时间:2020-10-21 11:16:57

标签: python pytest

一个简单的问题。当我跑步时:

pytest test_file.py

我的python应用返回错误,因为test_file.py被识别为无效参数。

如何避免这种情况发生?换句话说,如何阻止我的应用接受pytest自变量作为参数?

谢谢您的提示!

罗兰

1 个答案:

答案 0 :(得分:1)

您要将整个应用程序导入测试代码吗?您应该将逻辑移到其自己的模块中,并仅将其导入到测试代码中。例如:

文件main.py

import sys
from my_logic import do_stuff

if __name__ == "__main__":
    expected_cmd_arg = sys.argv[1]
    do_stuff(expected_cmd_arg)

文件my_logic.py:

def do_stuff(expected_cmd_arg):
    pass

文件test_my_logic.py:

import pytest
from my_logic import do_stuff

def test_my_logic():
     do_stuff("hello")

这样,您就永远不会将读取sys.argv的文件导入到测试中。

相关问题