我有一个使用argparse库的Python模块.如何为代码库的该部分编写测试?
我正在使用Python进行一系列单元测试,其中一些测试依赖于配置变量的值.这些变量存储在全局Python配置文件中,并在其他模块中使用.我想为配置变量的不同值编写单元测试,但尚未找到实现此目的的方法.
我没有可能重写我正在测试的方法的签名.
这就是我想要实现的目标:
from my_module import my_function_with_global_var
class TestSomething(self.unittest):
    def test_first_case(self):
         from config import MY_CONFIG_VARIABLE
         MY_CONFIG_VARIABLE = True
         self.assertEqual(my_function_with_global_var(), "First result")
    def test_second_case(self):
         from config import MY_CONFIG_VARIABLE
         MY_CONFIG_VARIABLE = False
         self.assertEqual(my_function_with_global_var(), "Second result")
谢谢.
编辑:使示例代码更明确.
我有一个用Python编写的控制台程序.它使用以下命令询问用户问题:
some_input = input('Answer the question:', ...)
我将如何测试中,含有一个呼叫的功能input使用pytest?我不想强迫测试人员多次输入文本只完成一次测试运行.
我想知道python的哪些测试工具支持交互式程序的测试.例如,我有一个应用程序启动:
$ python dummy_program.py 
>> Hi whats your name? Joseph
我想要工具,Joseph所以我可以模仿这种互动行为.
我正在开始一个Python项目,其中stdin重定向是必要的,使用类似于下面的代码:
import sys
import StringIO
s = StringIO.StringIO("Hello")
sys.stdin = s
a = raw_input("Type something: ")
sys.stdin = sys.__stdin__
print("You typed in: "+a)
问题是,代码运行后,将显示以下内容:
输入内容:输入:Hello
有没有办法修改我的代码,以便显示以下内容?
输入内容:你好
你输入了:你好
我一直在寻找高低,但尚未找到答案.如果有人有想法我真的很感激.谢谢!
我正在尝试编写一个shell脚本,该脚本将运行需要一些原始输入的python文件。python脚本基本上遵循以下原则:
def main():
    name=raw_input("What's your name?")
    print "Hello, "+name
main()
我希望shell脚本运行脚本并自动将输入输入到其中。我已经看到了很多方法来从python函数返回的内容中获取shell输入,或者如何从带有输入的python中运行shell,但是这种方法并非如此。基本上,我只想要能做到的事情:
python hello.py
#  give the python script some input here
#  then continue on with the shell script.