相关疑难解决方法(0)

你如何编写python模块的argparse部分的测试?

我有一个使用argparse库的Python模块.如何为代码库的该部分编写测试?

python unit-testing argparse

137
推荐指数
7
解决办法
4万
查看次数

在Python unittest框架中修改全局变量

我正在使用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")
Run Code Online (Sandbox Code Playgroud)

谢谢.

编辑:使示例代码更明确.

python unit-testing global-variables

38
推荐指数
2
解决办法
4万
查看次数

Pytest:如何使用输入调用测试函数?

我有一个用Python编写的控制台程序.它使用以下命令询问用户问题:

some_input = input('Answer the question:', ...)
Run Code Online (Sandbox Code Playgroud)

我将如何测试中,含有一个呼叫的功能input使用pytest?我不想强迫测试人员多次输入文本只完成一次测试运行.

python testing unit-testing input pytest

38
推荐指数
5
解决办法
2万
查看次数

测试交互式python程序

我想知道python的哪些测试工具支持交互式程序的测试.例如,我有一个应用程序启动:

$ python dummy_program.py 

>> Hi whats your name? Joseph
Run Code Online (Sandbox Code Playgroud)

我想要工具,Joseph所以我可以模仿这种互动行为.

python testing

10
推荐指数
1
解决办法
2228
查看次数

如何在Python中显示重定向的stdin?

我正在开始一个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)
Run Code Online (Sandbox Code Playgroud)

问题是,代码运行后,将显示以下内容:

输入内容:输入:Hello

有没有办法修改我的代码,以便显示以下内容?

输入内容:你好

你输入了:你好

我一直在寻找高低,但尚未找到答案.如果有人有想法我真的很感激.谢谢!

python redirect stdin

7
推荐指数
1
解决办法
3636
查看次数

从外壳输入到python脚本?

我正在尝试编写一个shell脚本,该脚本将运行需要一些原始输入的python文件。python脚本基本上遵循以下原则:

def main():
    name=raw_input("What's your name?")
    print "Hello, "+name
main()
Run Code Online (Sandbox Code Playgroud)

我希望shell脚本运行脚本并自动将输入输入到其中。我已经看到了很多方法来从python函数返回的内容中获取shell输入,或者如何从带有输入的python中运行shell,但是这种方法并非如此。基本上,我只想要能做到的事情:

python hello.py
#  give the python script some input here
#  then continue on with the shell script.
Run Code Online (Sandbox Code Playgroud)

python shell

1
推荐指数
1
解决办法
1万
查看次数