Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中检查输入和预期输出的简单单元测试_Python_Unit Testing - Fatal编程技术网

Python中检查输入和预期输出的简单单元测试

Python中检查输入和预期输出的简单单元测试,python,unit-testing,Python,Unit Testing,我有一个简单的Python程序(hello.py): 就这么简单,没有类,没有函数,什么都没有,只有一个输入和相应的输出 我的目标只是创建test\u hello.py,一个用于hello.py的测试文件。我花了几个小时在互联网上搜索,但我只找到了函数或方法的单元测试,而没有找到简单的输入和输出程序。有线索吗?提前非常感谢 注意:堆栈溢出表明有一个答案: 但是这两个问题非常不同,我的代码中没有函数,而是有一个input()。在“答案”中,建议代码位于函数内部,并且没有input()我确实建议使用

我有一个简单的Python程序(
hello.py
):

就这么简单,没有类,没有函数,什么都没有,只有一个输入和相应的输出

我的目标只是创建
test\u hello.py
,一个用于
hello.py
的测试文件。我花了几个小时在互联网上搜索,但我只找到了函数或方法的单元测试,而没有找到简单的输入和输出程序。有线索吗?提前非常感谢

注意:堆栈溢出表明有一个答案:
但是这两个问题非常不同,我的代码中没有函数,而是有一个
input()
。在“答案”中,建议代码位于函数内部,并且没有
input()

我确实建议使用类或函数,但是如果您坚持,那么您可以尝试在子流程中使用它并捕获输出


最后,我想出了解决方案:

import unittest
import os
import subprocess

class TestHello(unittest.TestCase):

    def test_case1(self):
        input = "Alan"
        expected_output = "Hello Alan"
        with os.popen("echo '" + input + "' | python hello.py") as o:
            output = o.read()
        output = output.strip() # Remove leading spaces and LFs
        self.assertEqual(output, expected_output)

    def test_case2(self):
        input = "John"
        expected_output = "Hello John"
        with os.popen("echo '" + input + "' | python hello.py") as o:
            output = o.read()
        output = output.strip() # Remove leading spaces and LFs
        self.assertEqual(output, expected_output)

if __name__ == '__main__':
    unittest.main()
我有两个测试用例:“Alan”必须打印“Hello Alan”,John必须打印“Hello John”


谢谢你们的线索

有没有具体的原因?您将如何检查预期输出?例如,它应该返回一些内容或将输出写入文件。为此编写一个bash脚本可能更好。我只是不知道如何导入或调用它以在测试中运行,因为它不是函数,您必须编写
hello.py
以在测试代码中运行,这是您可以使用函数执行的预测结果,但是你不想使用函数,谢谢你的回答。是的,我坚持。如何在子流程中使用它并捕获输出?此链接可能会有所帮助此链接可能更好
import unittest
import os
import subprocess

class TestHello(unittest.TestCase):

    def test_case1(self):
        input = "Alan"
        expected_output = "Hello Alan"
        with os.popen("echo '" + input + "' | python hello.py") as o:
            output = o.read()
        output = output.strip() # Remove leading spaces and LFs
        self.assertEqual(output, expected_output)

    def test_case2(self):
        input = "John"
        expected_output = "Hello John"
        with os.popen("echo '" + input + "' | python hello.py") as o:
            output = o.read()
        output = output.strip() # Remove leading spaces and LFs
        self.assertEqual(output, expected_output)

if __name__ == '__main__':
    unittest.main()