I am giving an example which throws an error in ipython/jupyter notebook, but runs fine as an individual script.
import unittest
class Samples(unittest.TestCase
@Hari Baskar 's answer can be made generic by defining a decorator:
def run_test(tcls):
"""
Runs unit tests from a test class
:param tcls: A class, derived from unittest.TestCase
"""
suite = unittest.TestLoader().loadTestsFromTestCase(tcls)
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suite)
Run the test by applying the @run_test
decorator to the class to be tested:
@run_test
class Samples(unittest.TestCase):
def testToPow(self):
pow3 = 3**3
# use the tools the unittest module gives you...
self.assertEqual(pow3, 27)