Create unittests under tests/
in project directory
Follow the package directory structure for tests
myproject/ # top-level directory myproject/ # package module_1/ file_a.py module_2/ file_b.py tests/ # unittests module_1/ test_file_a.py module_2/ test_file_b.py
Simple template for unittests that covers most use cases
from unittest import TestCase
class TestSomething(TestCase):
# Setup that happens once for all tests in this class
self.one_time_setup = one_time_setup()
def setUp(self):
# Setup that happens again for every test
self.something = set_up_something()
self.something_else = set_up_something_else()
def test_some_feature(self):
# Do stuff
output = self.something.do_feature(self.something_else)
# Equality assertion
self.assertEqual(output, some_value)
# Raises error or exception assertion
with self.assertRaise(ValueError):
self.something.illegal()
# Something is true
self.assertTrue(self.something.has_property())
pytest
inside project directorypytest