Skip to content Skip to sidebar Skip to footer

Raise Exception If Script Fails

I have a python script, tutorial.py. I want to run this script from a file test_tutorial.py, which is within my python test suite. If tutorial.py executes without any exceptions, I

Solution 1:

If there is no error s will be 0:

from os import system
s=system("python tutorial.py")
assert  s == 0

Or use subprocess:

from subprocess import PIPE,Popen

s = Popen(["python" ,"tutorial.py"],stderr=PIPE)

_,err = s.communicate() # err  will be empty string if the program runs okassertnot err

Your try/except is catching nothing from the tutorial file, you can move everything outside the it and it will behave the same:

from os import system
test_passes = False

s = system("python tutorial.py")
test_passes = Trueassert test_passes

Post a Comment for "Raise Exception If Script Fails"