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
2 Answers 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()
Sign up to request clarification or add additional context in comments.
Comments
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
Gang
2,8183 gold badges21 silver badges38 bronze badges
Comments
Explore related questions
See similar questions with these tags.
lang-py