Introduction to Testing in Python
Alexander Levin
Data Scientist
.setUp()
- a method called to prepare the test fixture before the actual test.tearDown()
- a method called after the test method to clean the environmentimport unittest
class TestLi(unittest.TestCase):
# Fixture setup method
def setUp(self):
self.li = [i for i in range(100)]
# Fixture teardown method
def tearDown(self):
self.li.clear()
# Test method
def test_your_list(self):
self.assertIn(99, self.li)
self.assertNotIn(100, self.li)
The correct syntax: setUp
with capital U and tearDown
with capital D.
class TestLi(unittest.TestCase):
# Fixture setup method
def setUp(self):
self.li = [i for i in range(100)]
# Fixture teardown method
def tearDown(self):
self.li.clear()
The command: python3 -m unittest test_in_list.py
Output of a run with a .setUp()
and .tearDown()
:
Output of a run with a .set_up()
:
.setUp()
method.tearDown()
method.setUp()
- a method called to prepare the test fixture before the actual test..tearDown()
- a method called after the test method to clean the environment.Introduction to Testing in Python