Unit Tests
This tests specific methods and logic in the code. This is the most granular type of test. The goal is to verify the internal flow of the method, as well as to make sure edge cases are being handled.
def func():
return 1
def test_func():
assert func() == 1
For Python, use the unittest
library. Also learn from the book “The Hitchhiker’s Guide To Python”
import unittest
class TestStringMethods(unittest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2)
if __name__ == '__main__':
unittest.main()