Skip to content

Python Unit Tests

Clerik edited this page Apr 9, 2018 · 1 revision

Python Unit Tests work like this:

There needs to be a testing class, which can expand other testing classes. Inside this class is where you will write your tests. The test class will import unittest.TestCase, like this:

`class TestClass(unittest.TestCase):`

If you like, you can define a setUp function and a tearDown function. These are for lines of code common to each test. For example, if you need to establish a connection with the database, and close it afterwards. They run before and after each test function.

`def setUp(self):`
`def tearDown(self):`

Each test function should start with "test_", it should import "self", and it should end with an assertion. Like this:

`def test_example(self):`
    `self.assertTrue(True)`

In order to run the tests, this should be at the end of the file:

`if __name__ == '__main__':`
    `unittest.main()`
Clone this wiki locally