~/Blog

Brandon Rozek

Photo of Brandon Rozek

PhD Student @ RPI studying Automated Reasoning in AI and Linux Enthusiast.

Quick Python: Unit Testing

Published on

Updated on

Warning: This post has not been modified for over 2 years. For technical posts, make sure that it is still relevant.

Python has a great built-in unit test framework. This post will give a skeleton for how to format the files in your tests directory.

Example tests/test_basic.py

import unittest

class Tester(unittest.TestCase):
    def setUp(self):
        """To Run Before Every Test Case"""
        pass

    def tearDown(self):
        """To Run After Every Test Case"""
        pass

    def test_something(self):
        """A Test Case"""
        self.assertEqual(True, True)

if __name__ == '__main__':
    unittest.main()

To auto-discover and run your tests

python -m unittest discover -s tests
Reply via Email Buy me a Coffee
Was this useful? Feel free to share: Hacker News Reddit Twitter

Published a response to this? :