JuanPablo
JuanPablo

Reputation: 24764

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 ?

Upvotes: 2

Views: 121

Answers (3)

user966588
user966588

Reputation:

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.

Upvotes: 0

pradyunsg
pradyunsg

Reputation: 19406

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!

Upvotes: 1

You can write an except script to automate this.

Upvotes: 0

Related Questions