In this exercise, you will be given a few lines of code that perform a certain task (that you will have to understand) and then write an automated test that checks whether that task is performed correctly.
Make sure you have read the note chapters on Testing basics, The Fields of Saskatchewan and Test frameworks.
If you haven't already, fork this repository and clone it on your computer.
- Spend some time reading the code, try to run it and see whether you understand what's going on.
- Have you seen
datetime
before? - Play using your favourite tool (notebook, terminal, scripts) with the functions and objects used in
times.py
.
- Create a new file called
test_times.py
in the same directory wheretimes.py
is. - Make the
overlap_time
function accessible to that file. (Hint: You need toimport
the file). - Move the content from the
if __name__ ...
block fromtimes.py
to a function calledtest_given_input
intotest_times.py
and fill the gaps forresult
andexpected
. (For now, you can copy the output of the program as the expected value)
def test_given_input():
...
result = ...
expected = ...
assert result == expected
- run
pytest
on that directory and see whether the test is picked up bypytest
and whether it passes. If the test doesn't pass, see if you can find what is going wrong.
- When you are happy with your solution (or want some feedback!):
- Push your new code to your own fork.
- On GitHub, open a pull request from your fork to the original repository.
- In the description, include the text "Answers UCL-MPHY0021-21-22/RSE-Classwork#16". This will list your PR to this issue.
- On the PR text, comment on what you found difficult or interesting, or something you learned.
- Continue with the remaining steps (7. - 9.) on Classwork issue #16