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:

  1. from command line, use verbose option:

    python -m unittest discover -v

  2. programmatically calling unittest.main , passing verbosity argument

    if __name__ == "__main__":     unittest.main(verbosity=2) 

running tests "manually"

  1. programmatically building testsuite , calling testrunner verbosity argument.

    suite = unittest.testloader().loadtestsfrommodule(testmodulename) results = unittest.texttestrunner(verbosity=2).run(suite) 
  2. 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:

trial reporting


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -