14. Understanding Unit Testing
14. Understanding Unit Testing
Objective: Research and understand the importance of unit testing in Python and how it
contributes to software quality assurance.
Instructions: Read the article from Dataquest on unit testing in Python – Dataquest Unit
Testing. Then, answer the following questions and complete the practical activity to
demonstrate your understanding.
they help to ensure that code works as intended and catch bugs early on. Also, testing is a
best practice that can save time and money by finding and fixing issues before they cause
major problems.
The assert statement is a built-in statement in Python used to, as the name says, assert if
a given condition is true or not. If the condition is true, nothing happens, but if it's not true,
an error is raised.
o List and explain three assert methods provided by the unittest module.
assertIn(a, b) inserts a in b
4. Setting Up Unit Tests:
o Explain the significance of the setUp method in the unittest framework.
The setUp method in the unittest framework runs before each test, initializing
objects and variables needed for the tests. This ensures each test runs in a
clean environment without repeating setup code, improving maintainability
and reducing errors.
Objective: Implement unit tests for a Python class and practice unit, subsystem, and system
testing, as well as black box, white box, and grey box testing methodologies.
Activity Steps:
class MathOperations:
def add(self, a, b):
return a + b
import unittest
from math_operations import MathOperations
class TestMathOperations(unittest.TestCase):
def setUp(self):
self.math_ops = MathOperations()
def test_add(self):
# Add unit tests for the add method
self.assertEqual(self.math_ops.add(3, 4), ____) # Fill in
expected value
self.assertEqual(self.math_ops.add(-1, 1), ____) # Fill in
expected value
def test_subtract(self):
# Add unit tests for the subtract method
self.assertEqual(self.math_ops.subtract(10, 5), ____) #
Fill in expected value
self.assertEqual(self.math_ops.subtract(0, 0), ____) # Fill
in expected value
def test_multiply(self):
# Add unit tests for the multiply method
self.assertEqual(self.math_ops.multiply(6, 7), ____) # Fill
in expected value
self.assertEqual(self.math_ops.multiply(-1, -1), ____) #
Fill in expected value
def test_divide(self):
# Add unit tests for the divide method
self.assertEqual(self.math_ops.divide(10, 2), ____) # Fill
in expected value
with self.assertRaises(ValueError):
self.math_ops.divide(10, ____) # Fill in value that
causes exception
if __name__ == '__main__':
unittest.main()
Submission
Submit your answers to the research questions and the completed Python code with unit tests.
Ensure that your code is well-commented to explain your testing approach.
Method Description
assertEqual(a, b) Checks if a is equal to b.
assertNotEqual(a, b) Checks if a is not equal to b.
assertTrue(x) Checks if x is true.
assertFalse(x) Checks if x is false.
Setting Up Tests
Method Purpose
setUp Initialises the test environment before each test method is run.