Unit Testing Guide - WheatonCS/Lexos GitHub Wiki
Lexos Unit Testing Guide
A unit test is a way of testing a unit - the smallest piece of code that can be logically isolated in a system.
For Lexos, these units are our functions.
This testing helps us isolate what is broken in our application in order to fix it faster.
assert handle_gutenberg(text="") == ""
This is a simple example of a line in our unit tests that checks the expected output of a function when we pass it an empty string.
For this function to be behaving as we intend it, we expect the output of this case to also be an empty string.
If a programmer edits the handle_gutenberg function to return a False
instead of the empty string, the unit tests would fail as other functions in Lexos rely on handle_gutenberg to return a string.
This would then alert the programmers that the functionality of handle_gutenberg was modified.
Checking if your branch is passing the unit tests
After navigating to your branch on the GitHub, you should see either a :x:, or a :heavy_check_mark:
In this case, we have a red x, meaning we are failing at least one of our continuous integration tests.
When you click on the symbol shown, you will open a menu showing various tests:
The first option, Travis CI, houses our unit tests. The other options have to do with code coverage, which is a measurement of how many lines of code our unit tests are actually testing.
NOTE: If you have a green check mark next to Travis CI, then your unit tests are passing.
If you'd like to learn more about code coverage see Repo Administration Guide.
Click on the details section of Travis CI and you will be presented with a table that looks like this:
The first row shows our Python continuous integration test, you'll want to click the blue value in the first column.
You'll be presented a log file containing lots of useful information for Repo Administration.
Scroll down until you find the section labeled test session starts
You can see visually, that every unit test passed except for 1 red F in our unit tests for testing general_functions.py
.
At the bottom half of the image, we can see the specific test that failed, test_image
in the TestExtractDocxContent
unit testing class.
NOTE: Your Travis CI may have failed because of flake8 or other tests failing, if that is the case, there will be no
FAILURES
section.
Fixing our Unit Tests
You will want to familiarize yourself with what function has been modified.
In our case, we can see the unit test is failing when we are calling the extract_docx_content
function.
When unit tests are failing, the functionality of your function has been modified.
Figure out why it is failing, and make sure to check everything that is dependent on your function to be working as intended.
You can do this by highlighting a function's name, right clicking and navigating to find usages
.
NOTE: Make sure to communicate with the team/individual who pushed the commit that broke the unit tests.
It is up to you and the programmer how to proceed, figure out if the programmer broke or fixed certain functionalities, then update the unit tests / fix the feature accordingly.