unit testing - Python unittest call function when assertion passes -
i can't find way do_something() when assertion in test passes. example:
def test_one(self): self.assertequal(1,1, "did not match")
that test print "did not match" if assertion fail, in case not, i'm trying call function or print when self.assertequal() successful, please ideas ?
thanks
if want print when passes, there couple of options. however, please don't use noeld's answer. not wrong, it's don't want clutter test bunch of print
messages when unittest
provides better ways doing this.
verbosity
if want print result of every single test_
function, set verbosity of test runner. can in couple of ways:
from command line, use verbose option:
python -m unittest discover -v
programmatically calling
unittest.main
, passing verbosity argumentif __name__ == "__main__": unittest.main(verbosity=2)
running tests "manually"
programmatically building testsuite , calling testrunner verbosity argument.
suite = unittest.testloader().loadtestsfrommodule(testmodulename) results = unittest.texttestrunner(verbosity=2).run(suite)
creating subclass of testresult object, contains addsuccess method called whenever test passes.
you can pass testresult object test suite's run method.
suite = unittest.testloader().loadtestsfrommodule(testmodulename) suite.run(mytestresult)
third party runners
take @ twisted's trial. contains many different test runners might useful. default, runs treereporter
looks like:
Comments
Post a Comment