简体   繁体   中英

python: testing the outputs ( … not unit testing ?)

I have a python script called script.py , this need a input A and generate a output B .

A -> script.py -> B

every time I add a new feature, I need to check that the program generate the same output.

how I can automated this tests type ?

what is the name of this tests type ?

exists some python module for this tests type ?

From what I understand,
You want to test if that piece of code (unit) is doing what it's supposed to. That's Unit Testing.

What you can do is make a test that gives script.py it's input ( A ), and gets the output produced. Then you can just check if the output matches.

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: That's how PLY tests it's code. And I'm fiddling with it!

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

You can use pytest for your testing. So what you can do is create a testing_suite.py and put your tests in it with name starting as test_ for ex: test_B, test_C etc

# 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:
          ....
          ....

then you can run as:

$> py.test

So this should execute all tests with test_ . Please check pytest doc here for test discovery rules .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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