繁体   English   中英

python:测试输出(…不是单元测试?)

[英]python: testing the outputs ( … not unit testing ?)

我有一个名为script.py的python脚本,它需要输入A并生成输出B

A -> script.py -> B

每次添加新功能时,都需要检查程序是否生成相同的输出。

我怎样才能使这种测试类型自动化?

此测试类型的名称是什么?

是否存在一些用于此测试类型的python模块?

据我了解,
您想测试那段代码(单元)是否按预期执行。 那是单元测试。

您可以做的是做一个测试,为script.py提供输入( A ),并生成输出。 然后,您可以只检查输出是否匹配。

class OutputTestCase(unittest.TestCase):

    def get_output(self, input):
       ...  # You haven't mentioned how "input" is taken or how output is taken.

    def test_script(self):
       input = ...
       expected = ...

       output = self.get_output(input)
       self.assertEqual(output, expected)

PS:这就是PLY测试其代码的方式。 而且我很喜欢它!

您可以编写一个例外脚本来自动执行此操作。

您可以使用pytest进行测试。 因此,您可以做的是创建一个testing_suite.py并将您的测试以test_开头的名称test_ ,例如:test_B,test_C等

# testing_suite.py
import pytest

def test_B:
          proc = subprocess.Popen(['python', 'script.py',  'arg1'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
          output = proc.communicate()[0]
          if output == 'B':
                     print "testB done" # or something else depends on you

def test_C:
          ....
          ....

那么您可以运行为:

$> py.test

因此,这应该使用test_执行所有测试。 在此处查看pytest doc以获得测试发现规则

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM