0% found this document useful (0 votes)
2 views

14. Understanding Unit Testing

The document outlines the importance of unit testing in Python, emphasizing its role in ensuring software reliability and maintenance by catching bugs early. It provides definitions, benefits, and key concepts of unit testing, including the use of assert statements and the significance of the setUp method in the unittest framework. Additionally, it includes practical activities for implementing unit tests for a simple Python class, MathOperations, and encourages the application of various testing methodologies.

Uploaded by

ayaan.saleheen
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

14. Understanding Unit Testing

The document outlines the importance of unit testing in Python, emphasizing its role in ensuring software reliability and maintenance by catching bugs early. It provides definitions, benefits, and key concepts of unit testing, including the use of assert statements and the significance of the setUp method in the unittest framework. Additionally, it includes practical activities for implementing unit tests for a simple Python class, MathOperations, and encourages the application of various testing methodologies.

Uploaded by

ayaan.saleheen
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Understanding Unit Testing in Python

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.

Part 1: Research Questions

1. What is Unit Testing?


o Define unit testing and its purpose in software development.

Testing code by using segments of code to test

2. Why is Unit Testing Important?


o Explain the benefits of unit testing in terms of software reliability and
maintenance.

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.

3. Key Concepts in Unit Testing:


o Describe the role of the assert statement in Python.

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.

assertTrue/False(x) checks that bool(x) is True/False

assertEqual(a, b) checks that a == b

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.

Part 2: Practical Activity

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:

1. Create a Simple Python Class:


o Define a simple class, MathOperations, with methods for addition,
subtraction, multiplication, and division.

class MathOperations:
def add(self, a, b):
return a + b

def subtract(self, a, b):


return a - b

def multiply(self, a, b):


return a * b

def divide(self, a, b):


if b == 0:
raise ValueError("Cannot divide by zero.")
return a / b

2. Write Unit Tests:


o Create unit tests for each method in the MathOperations class.
o Some sections below are missing that you are required to complete.

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()

3. Black Box, White Box, and Grey Box Testing:


o Perform black box testing by writing tests without looking at the internal code.
o Perform white box testing by considering the internal code structure.
o Combine both approaches for grey box testing.

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.

Key unittest Methods

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.

You might also like