Navigation

If possible everything in our codebase, each and every function. But it depends as a choice
of the developers. You can skip it if it is not practical to write a robust test. As Nick Coghlan
said in a guest session – … with a solid test suite, you can make big changes, confident that the externally visible behavior will remain the same

A method by which individual units of source code. Wikipedia says
In computer programming, unit testing is a method by which individual units of source code,
sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures, are tested to determine if they are fit for use.

importunittestfromfactorialimportfactclassTestFactorial(unittest.TestCase):""" Our basic test class """deftest_fact(self):""" The actual test. Any method which starts with ``test_`` will considered as a test case. """res=fact(5)self.assertEqual(res,120)if__name__=='__main__':unittest.main()

If we call div(0) in factorial.py , we can see if raises an exception.

We can also test these exceptions, like:

self.assertRaises(ZeroDivisionError,div,0)

Full code

importunittestfromfactorialimportfact,divclassTestFactorial(unittest.TestCase):""" Our basic test class """deftest_fact(self):""" The actual test. Any method which starts with ``test_`` will considered as a test case. """res=fact(5)self.assertEqual(res,120)deftest_error(self):""" To test exception raise due to run time error """self.assertRaises(ZeroDivisionError,div,0)if__name__=='__main__':unittest.main()