Interview Questions, Answers and Tutorials

Unit testing classes and methods

Unit testing classes and methods

What is Unit Testing?

Imagine you have a magic wand (computer program) that can do amazing tricks (perform tasks). You want to make sure this wand works perfectly before showing it off. So, you test it! Unit testing is like testing each part of the wand to ensure it works just right. In programming, it means testing small parts of your code, like functions or classes, to check if they work as expected.

Why Unit Test?

Unit testing helps us catch mistakes early. It’s like making sure every piece of a puzzle fits perfectly before completing the whole picture. It saves time because finding mistakes early is easier than fixing them later when everything is put together.

How to Unit Test Classes and Methods in Python

In Python, classes are like blueprints for creating objects, and methods are like actions objects can perform. Let’s say we have a simple Calculator class with methods to add and subtract numbers. Here’s how we can write unit tests for it.

Example: Calculator Class

# calculator.py

class Calculator:
    def add(self, a, b):
        return a + b
    
    def subtract(self, a, b):
        return a - b




Writing Unit Tests

Now, we’ll write tests to check if our Calculator class works correctly:

# test_calculator.py

import unittest
from calculator import Calculator

class TestCalculator(unittest.TestCase):
    
    def test_add(self):
        calc = Calculator()
        result = calc.add(3, 5)
        self.assertEqual(result, 8)  # Check if 3 + 5 equals 8
    
    def test_subtract(self):
        calc = Calculator()
        result = calc.subtract(10, 4)
        self.assertEqual(result, 6)  # Check if 10 - 4 equals 6

if __name__ == '__main__':
    unittest.main()

Explanation of the Code

  • Importing: We import the unittest module, which helps us write and run tests.
  • Test Class: TestCalculator inherits from unittest.TestCase, which makes it a test case.
  • Test Methods: Each method inside TestCalculator that starts with test_ is a test. We create a Calculator object (calc) and use its methods (add and subtract) to perform calculations.
  • Assertions: We use self.assertEqual(result, expected_value) to check if the result of each calculation matches the expected value.

Running the Tests

To run these tests:

  1. Save calculator.py and test_calculator.py in the same directory.
  2. Open a terminal or command prompt.
  3. Navigate to the directory where your files are saved.
  4. Type python test_calculator.py and press Enter.

You should see an output that tells you if your tests passed or failed. If everything works as expected, you’ll see something like:

..
----------------------------------------------------------------------
Ran 2 tests in 0.001s

OK

Practice Questions:

  1. Modify the Calculator Class: Add a method multiply to the Calculator class. Write a test case in test_calculator.py to check if calc.multiply(4, 5) equals 20.
  2. Edge Case Testing: Write a test case to check if calc.subtract(5, 10) equals -5.

Solutions:

  1. Solution for multiply method addition:

# calculator.py

class Calculator:
    def add(self, a, b):
        return a + b
    
    def subtract(self, a, b):
        return a - b
    
    def multiply(self, a, b):
        return a * b




Update test_calculator.py:

# test_calculator.py

import unittest
from calculator import Calculator

class TestCalculator(unittest.TestCase):
    
    def test_multiply(self):
        calc = Calculator()
        result = calc.multiply(4, 5)
        self.assertEqual(result, 20)  # Check if 4 * 5 equals 20

    # Existing test methods...

  1. Solution for edge case testing:

# test_calculator.py

import unittest
from calculator import Calculator

class TestCalculator(unittest.TestCase):
    
    # Existing test methods...

    def test_subtract_edge_case(self):
        calc = Calculator()
        result = calc.subtract(5, 10)
        self.assertEqual(result, -5)  # Check if 5 - 10 equals -5

if __name__ == '__main__':
    unittest.main()




Unit testing helps us ensure that each part of our code works correctly before putting everything together. It’s like checking each piece of a puzzle to make sure they fit perfectly. By writing and running tests, we can catch mistakes early and build more reliable programs.