Let's recall from lecture that a "unit" test validates a small component of code. Now, we will look at our first example of a unit test using the native Python library, unittest
.
import sys import os import unittest """ This function takes as argument a list, `color`, which consists of three integer values for each of the three colors in the RGB model, and which represents a color. The RGB model is used in real life to represent hues in digital devices: computer monitors, cameras, screens, and the like. You may assume that `color[0]` is an integer corresponding with red, `color[1]` is an int for green, and `color[2]` is an int for blue. The function changes the colors to IWU green and returns the original list. """ def make_titan_green(color): RED = 46 GREEN = 100 BLUE = 73 color[0] = RED color[1] = GREEN color[2] = BLUE return color
Now, let's test out our function using the unittest
module.
What are some things that we can test for?
class BasicTest(unittest.TestCase): def test_correct_colors(self): color = [10, 10, 10] expected = [46, 100, 73] actual = make_titan_green(color) self.assertEqual(expected, actual) if __name__ == '__main__': unittest.main()
and we expect to see:
test_correct_colors (__main__.BasicTest) ... ok ---------------------------------------------------------------------- Ran 1 test in 0.005s OK