This lab aims to familiarize with the concepts and practices of unit testing and code coverage in Python, using the Siena MLS as a case study.
Unit testing involves testing individual components or functions of a program to ensure they work as expected. Each test case is designed to test a specific part of the code in isolation.
Code coverage measures the amount of code exercised by a test suite. It helps in identifying untested parts of a codebase. There are different types of coverage, including:
- Line Coverage: Measures if each line of code has been executed.
- Branch Coverage: Measures if both the True and False branches of conditional statements have been executed.
unittestin Python: A unit testing framework. We will be running it viapytestcoverage.py: A tool for measuring code coverage in Python. We will be usingpy-covbut again, should not matter at this stage:)
- Ensure Python is installed on your system.
- Clone the
Siena_MLSrepository https://github.com/Introduction-To-CS-Siena-College/Siena-MLS-py - Change directory to the newly created directory
- We need to learn on how to read ReadMe files in opensource projects, Follow the instructions listed in that project to Install depdendies, build the project and finally test.
- Todo: you should be able to run the command to test this project.
- Do not remove these checkboxes! Add your answer below
Check the Example of a unit test for makePicture function:
import unittest
from siena_mls import makePicture
class TestMakePicture(unittest.TestCase):
def test_valid_image(self):
picture = makePicture("tests/assets/siena-small-logo.png")
self.assertIsNotNone(picture)
if __name__ == '__main__':
unittest.main()Many ways to measure coverage, or the proportion of system being executed by the test suite. One metric : Line Coverage Covered lines, or exercised lines -> The lines that have atleast been executed by some test case in your test suite.
- LineCoverage_percentage = 100 *
$covered \over {(covered + uncovered)} $
Another Metric : Branch Coverage
We will see next semester, but can you research?
Check the auto generated report.

Check the htmlcov directory and open the index.html
- Add the screenshot depicting the current coverage for
- What is the
line coveragefor the module?
Additional commands & comments
The basic commands to get code coverage are:coverage run -m unittest discover
coverage reportThese commands are usually automated in projects as you can see in the siena_mls project.
Check the makePicture function inside the html report. Notice some part of this function is covered, while the other is not. Some are marked in green others in red.
Can you see what part of the function was not traveresed by the current testsuite and why this might be the case?
- Todo Add your answer here.
Any idea how you could go about writing a unit test for these uncovered ( marked in red ) lines? Atleast specify a simple test case defination, input & output.
- Todo Add your answer here.
Without using Generative models, Write unit tests for the following functions in JES Media Functions:
duplicatePicturemakeSound- (optional)
getSampleValueAtandsetSampleValueAtModify the tests we have already seen to accomplish this. Feel free to create new test files if that's your fancy! but the idea is to get these functions tested.
After writing the unit tests, use coverage.py to measure the code coverage of your tests.
- Add the screenshot depicting the current coverage for
- What is the
line coveragefor the module?
Finish all the ToDos in this document Submit the following:
- Your unit test code.
- A screenshot or text file of the coverage report.
- Python
unittestdocumentation: https://docs.python.org/3/library/unittest.html - Coverage.py documentation: https://coverage.readthedocs.io/
- UBC.ca - Lecture 4 Debugging...