Peter Pan
Peter Pan

Reputation: 249

Python test whole script

I have a Python script. I use unittest for tests, but how can I test whole script.

My idea is something like this:

def test_script(self):
    output=runScript('test.py --a 5 --b 3')
    self.assertEqual(output, '8') 

test.py takes argument a and b and print a+b, in this case 8

Upvotes: 2

Views: 1416

Answers (2)

Gang
Gang

Reputation: 2768

Not sure this is what you wanted, use python unittest to wrap up the blackbox testing

import unittest # install and import

wrap your test in TestCase

class ScriptTest(unittest.TestCase):
    def test_script(self):
        output=runScript('test.py --a 5 --b 3')
        self.assertEqual(output, '8')

add TestCase to unittest

if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(ScriptTest)
    unittest.TextTestRunner(verbosity=2).run(suite)

Upvotes: 1

zunayed ali
zunayed ali

Reputation: 46

You can use the subprocess library to call a script and capture the output.

import subprocess

p = subprocess.Popen(
    ['./test.py', '--a', ...],
    stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT
)

print p.stdout.read()

Upvotes: 2

Related Questions