In the previous article for abstraction and inheritance I wrote some classes, showing the basics of these two programming concepts. In this one, I will write unit tests for one of the classes, written before.
Software testing is an important part of the software creation process. (This is a fantastic sentence, being able to create such means tha finally all these year of MBA studies have paid off 🙂) Without being tested, you cannot be completely sure that the software runs as expected. Even with testing and full code coverage you still cannot be sure that it is ok. But, testing is good, to give you some average security that the software you have written works as expected. Thus, once you decide to change it, you will not lose sleep, asking yourself the question – “Would it run again?” – but you will simply run the old tests and make sure that it works. Or fix the code up to the moment, when these tests are passing correctly.
If you have never used unit tests, probably you would consider them useless. And you will never think of them as of something good, up to the moment, when a well-written unittest saves you from deploying “broken” code. Then, probably you would understand the importance of those and you would rethink your values in life. Honestly, seeing a broken unittest after a change in the code, due to maintenance is like your old you (or the person who wrote the unittest) is giving you a high-five from the past. And the feeling is nice 🙂
Ok, now about the video and what I wrote there. Pretty much, I tested the class Book, with quite a few methods. There could be more tests to be written, still I wrote quite enough for a 30 minute video. The code below is, also available in GitHub here.
There are quite a few interesting methods, that I would like to point out separately:
- setUp – it executes itself before each test
- tearDown – it executes itself after each test, a bit useless in the current example, but can be quite important in general
- test_adding_string_for_change_price – it checks for TypeError and its message.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
import unittest from papers import Book class TestBook(unittest.TestCase): def setUp(self): self.my_book = Book(10, 20.0, "How to get 1K YouTube subscribers", "Joe") def tearDown(self): self.my_book = None print("tearDown() method has been called.") def test_init_book_and_check_author(self): actual = self.my_book.author expected = "Joe" self.assertEqual(expected, actual) # self.assertEqual(my_book.author, "Joe") def test_init_book_and_check_whether_reviews_are_empty_list(self): actual = self.my_book.reviews expected = [] self.assertEqual(expected, actual) def test_change_price(self): self.my_book.change_price(100) actual = self.my_book.price expected = 101.2 self.assertEqual(expected, actual) def test_change_title_small(self): self.my_book.change_title("VBA") actual = self.my_book.title expected = "Special Edition:VBA" self.assertEqual(expected, actual) def test_change_title_big(self): self.my_book.change_title("Some large title here") actual = self.my_book.title expected = "Some large title here" self.assertEqual(expected, actual) def test_whether_review_is_added(self): self.my_book.add_review("Great book", "Peter") actual = self.my_book.reviews[0][0] expected = "Great book" self.assertEqual(expected, actual) def test_whether_author_is_added(self): self.my_book.add_review("Great book", "Peter") self.my_book.add_review("Really great book", "Gosho") actual = self.my_book.reviews[1][1] expected = "Gosho" self.assertEqual(expected, actual) def test_review_count(self): self.my_book.add_review("Great book", "Peter") self.my_book.add_review("Really great book", "Gosho") self.my_book.add_review("Really great book", "Gosho") self.my_book.add_review("Really great book", "Gosho") self.my_book.add_review("Really great book", "Gosho") actual = self.my_book.reviews_count() expected = 5 self.assertEqual(expected, actual) def test_adding_string_for_change_price(self): with self.assertRaises(TypeError) as cm: self.my_book.change_price("three fifty") self.assertEqual('can only concatenate str (not "float") to str', str(cm.exception)) if __name__ == "__main__": unittest.main() |
Yup, this is all!
Thank you for reading so far! 🙂