w3resource

What are assertions in Unit tests and how are they used?

How assertions verify code behavior?

To run unit tests with unittest, you can use the framework's built-in test runner. The test runner is a command-line tool that discovers and executes the test cases and test methods defined in your Python test modules. Here's how to execute unit tests using the unittest test runner:Unit tests use assertions to verify whether a certain condition is true or false. They serve as checkpoints during test execution, allowing developers to verify whether the actual output matches the expected behavior. If the condition specified in an assertion is true, the test passes successfully. If the condition is false, the assertion fails, indicating that the code may have a problem.

The Purpose of Assertions in Unit Tests:

Unit tests ensure that the code under test behaves correctly and produces the expected results. Assertions play a critical role in validating the code's correctness by comparing actual values against expected values. They help identify bugs, regressions, and unexpected behaviors in the codebase.

How assertions are used in Unit tests:

Here's how assertions are typically used in unit tests:

  • Define Test Cases: In a unit test, developers create one or more test cases that target specific functionalities or components of the code.
  • Executing the Code: Within each test case, the code under test (i.e., the function or method being tested) is executed with specific input data.
  • Obtaining Results: After executing the code, the test case obtains the actual output or result produced by the code.
  • Writing Assertions: In the test case, developers use assertion methods provided by the testing framework (e.g., unittest.TestCase in unittest) to check whether the actual result matches the expected result.
  • Pass or Fail: If the assertion evaluates to true (actual matches expected), the test case passes. In the event the assertion fails (actual doesn't match expected), the test case fails.
  • Testing Multiple Scenarios: Test cases may include multiple assertions to cover different scenarios, edge cases, and inputs.

Example of Assertions: Let's consider an example of a simple test case using unittest:

Code:

import unittest
def add_numbers(x, y, z):
    return x + y + z
class TestAddNumbers(unittest.TestCase):
    def test_addition_with_positive_numbers(self):
        result = add_numbers(5, 6, 7)
        self.assertEqual(result, 18)
    def test_addition_with_negative_numbers(self):
        result = add_numbers(-1, 0, -111)
        self.assertEqual(result, -112)
if __name__ == '__main__':
    unittest.main()

Output:

..
----------------------------------------------------------------------
Ran 2 tests in 0.002s
OK

In the example above, we have two test methods, each representing a test case. In each test method, we use self.assertEqual() to assert that the actual result of the "add_numbers()" function matches the expected result. The test passes if the addition is correct; otherwise, it fails.



Follow us on Facebook and Twitter for latest update.