2

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

bastelflp
10.2k7 gold badges36 silver badges68 bronze badges
asked Jan 7, 2016 at 22:36

2 Answers 2

2

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()
answered Jan 7, 2016 at 22:53
Sign up to request clarification or add additional context in comments.

Comments

1

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)
answered Jan 8, 2016 at 13:28

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.