self assertequal(self stats mean() def test_median(self)self assertequal(self stats median() self stats append( self assertequal(self stats median() def test_mode(self)self assertequal(self stats mode()[ , ]self stats remove( self assertequal(self stats mode()[ ]if __name__ ="__main__"unittest main(if we run this exampleit indicates that all tests pass notice first that the setup method is never explicitly called inside the three test_methods the test suite does this on our behalf more importantly notice how test_median alters the listby adding an additional to ityet when test_mode is calledthe list has returned to the values specified in setup (if it had notthere would be two fours in the listand the mode method would have returned three valuesthis shows that setup is called individually before each testto ensure the test class starts with clean slate tests can be executed in any orderand the results of one test should not depend on any other tests in addition to the setup methodtestcase offers no-argument teardown methodwhich can be used for cleaning up after each and every test on the class has run this is useful if cleanup requires anything other than letting an object be garbage collected for exampleif we are testing code that does file /oour tests may create new files as side effect of testingthe teardown method can remove these files and ensure the system is in the same state it was before the tests ran test cases should never have side effects in generalwe group test methods into separate testcase subclasses depending on what setup code they have in common several tests that require the same or similar setup will be placed in one classwhile tests that require unrelated setup go in another class organizing and running tests it doesn' take long for collection of unit tests to grow very large and unwieldy it quickly becomes complicated to load and run all the tests at once this is primary goal of unit testingit should be trivial to run all tests on our program and get quick "yes or noanswer to the question"did my recent changes break any existing tests?