Exercise 1 (software testing) #5
Description
In this exercise, you will be given a few lines of code that compute the overlap between two time ranges and a unit test that checks that this overlap is computed correctly.
Your task will be to write a further unit test that checks whether the overlap computation is performed correctly on a new set of two time ranges.
Setup
- Fork this repository to your personal GitHub account using the
Fork
button on the top right of this page. - Clone your forked copy of this repository to your laptop
git clone https://github.com/<your github username here>/ipls-workshop.git
. - Inside your local copy of the repository, checkout the
exercises
branch:git checkout exercises
- Execute
ls
in the repository - you should see 2 files:times.py
andtest_times.py
.
Exercise 1
Unit tests in Python
As we've discussed in class, unit tests are a way to check that small, modular parts of your code behave as intended.
The de-facto standard framework for unit tests in Python is pytest
.
pytest
automatically detects files that start with test_
and functions therein that start with test_
and runs those.
Comparisons can be done using the assert
statement. The typical structure of a pytest
unit test is as follows:
def test_given_input():
...
result = ...
expected = ...
assert result == expected
Further information about pytest
can be found in its documentation.
Understanding the code
- Spend some time reading both files, and see whether you understand what's going on.
- Do you think the
compute_overlap
function ofoverlap.py
computes the time overlaps correctly? - Do you understand the code in
test_times.py
?
Running a test
- The
test_times.py
file contains a test forcompute_overlap
. Run the test by typingpytest
and then hittingEnter
on your command line in the local repository. Does the test pass?
Adding another test
Now it's your turn to create a test.
- In
test_times.py
, add an additional unit test namedtest_workshop_morning_times
(there is already a skeleton implementation there, you need to fill in the test function body). You should - create a time range that goes from 2021-04-30 10:00:00 - 2021-04-30 13:00:00 (the timings of this morning's git lesson)
- create a time range that starts at 10:05 and ends at 12:55 and has two 10-minute breaks, also on 30 April 2021.
- include a line that checks that the time overlap between the first and the second time range is what you expect
- check that the test passes by running
pytest
.
Commit your changes and push
Once you've written this new test, commit it to your repository with a meaningful commit message that also links to this issue.
> git add test_times.py
> git commit -m "Add workshop timing test. Answers UCL-RITS/ipls-workshop#5"
and push it to your fork.
> git push
Well done, you've gotten to the end of the first exercise 🎉
If you've finished this exercise with time to spare, feel free to explore an additional exercise, that involves writing a test for no overlap test between time ranges: #6 .