Browse Courses

Test Assertion

This document explains the purpose of assertions in testing, describes common PyUnit assertions, and discusses the role of happy and sad paths in test case design.

This document explores the purpose and use of assertions in Python testing, highlights common PyUnit assertions, and explains the importance of testing both happy and sad paths for robust code validation.


Introduction

Assertions are statements in test cases that check if a condition is true or false. They are essential for determining whether code behaves as expected. If an assertion fails, the test fails, helping identify issues early.


Using Assertions in Python

Assertions can be made using Python’s built-in assert statement or with specialized assertion methods provided by test frameworks like PyUnit (unittest). These methods make tests more readable and expressive.

Example: Using assert

1assert sum(2, 3) == 5

Example: Using PyUnit Assertions

1from unittest import TestCase
2class TestSum(TestCase):
3    def test_sum(self):
4        self.assertEqual(sum(2, 3), 5)

Common PyUnit Assertions

Assertion MethodPurpose
assertEqual(a, b)Check that a is equal to b
assertNotEqual(a, b)Check that a is not equal to b
assertIn(a, b)Check that a is in b
assertNotIn(a, b)Check that a is not in b
assertTrue(x)Check that x is True
assertFalse(x)Check that x is False
assertIsInstance(a, T)Check that a is an instance of type T
assertRaises(E, f, …)Check that function f raises exception E
assertAlmostEqual(a, b)Check that a and b are almost equal (floats)

Happy Paths and Sad Paths in Testing

  • Happy paths are scenarios where everything works as expected. Tests for happy paths confirm that the code produces correct results for valid input.
  • Sad paths are scenarios where something goes wrong, such as invalid input or errors. Tests for sad paths ensure that the code handles errors gracefully and raises appropriate exceptions.

Example: Testing area_of_a_triangle

 1from unittest import TestCase
 2from triangle import area_of_a_triangle
 3
 4class TestAreaOfTriangle(TestCase):
 5    def test_happy_paths(self):
 6        self.assertAlmostEqual(area_of_a_triangle(3.4556, 8.3567), 14.43870626)
 7        self.assertEqual(area_of_a_triangle(2, 5), 5.0)
 8        self.assertEqual(area_of_a_triangle(0, 5), 0.0)
 9
10    def test_sad_paths(self):
11        with self.assertRaises(ValueError):
12            area_of_a_triangle(-2, 5)
13        with self.assertRaises(ValueError):
14            area_of_a_triangle(2, -5)
15        with self.assertRaises(TypeError):
16            area_of_a_triangle(True, 5)
17        with self.assertRaises(TypeError):
18            area_of_a_triangle(2, "height")

Conclusion

Assertions are the foundation of effective test cases. Using a variety of assertion methods and testing both happy and sad paths ensures code is robust, reliable, and handles errors as expected.


FAQs

Assertions check if code behaves as expected, marking tests as passed or failed based on the result.

  1. Happy paths test for expected, correct behavior; sad paths test for error handling and invalid input
  2. Happy paths are always skipped
  3. Sad paths are not important in testing
  4. Happy paths only test for exceptions
(1) Happy paths confirm correct results for valid input, while sad paths ensure errors are handled properly.

assertRaises checks that a function raises the expected exception, making it easy to test error conditions and handlers.

assertAlmostEqual allows for small rounding differences, making tests for floating point calculations more reliable.

Assertion MethodPurpose
A. assertEqual1. Check that a is in b
B. assertIn2. Check that a is equal to b
C. assertRaises3. Check that a function raises exception
D. assertIsInstance4. Check that a is an instance of a type
A-2, B-1, C-3, D-4.

assertTrue and assertFalse are used to check if a value is True or False in a test case.

True. These assertions verify boolean conditions in test cases.