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.
If you haven't already, fork this repository and clone it on your computer, and use uv to create a new environment for it (with pytest):
uv init
uv add --dev pytestTip
You can find useful information on the following chapters of our notes on Testing basics, The Fields of Saskatchewan and Test frameworks.
- Spend some time reading the code, try to run it and see whether you understand what's going on.
- Have you seen
datetimebefore? - Play using your favourite tool (notebook, terminal, scripts) with the functions and objects used in
times.py.
Tip
Imagine we have two instruments. The first one takes measurements for 5 minutes, stops for 1, and takes measurements again for another 5 minutes. The other one measures for 1 minute, and has a gap of 2 minutes between intervals. If we start our measurments with the first one at t=0 and the second one starts at t=2, then we would have a pattern of observations like:
block-beta
columns 13
a["obs 1 [5min]"]:5 space:1 c["obs 1 [5min]"]:5 space:2
space:2 d["obs 2 [1min]"]:1 space:2 e["obs 2 [1min]"]:1 space:2 f["obs 2 [1min]"]:1 space:2 g["obs 2 [1min]"]:1 space:1
h["00:00"]:1 j["01:00"]:1 k["02:00"]:1 l["03:00"]:1 m["04:00"]:1 n["05:00"]:1 o["06:00"]:1 p["07:00"]:1 q["08:00"]:1 r["09:00"]:1 s["10:00"]:1 t["11:00"]:1 u["12:00"]:1
The time_range function generates the observation blocks for a given start and end time, a number of intervals and a duration for the gaps. For the first row above, this would be:
"2025-01-01 00:00:00", "2025-01-01 00:11:00", 2, 60
and for the second:
"2025-01-01 00:02:00", "2025-01-01 00:12:00", 4, 120.
And the compute_overlap_time should provide:
[('2025-01-01 00:02:00', '2025-01-01 00:03:00'), # first interval
('2025-01-01 00:08:00', '2025-01-01 00:09:00')] # second interval - Create a new file called
test_times.pyin the same directory wheretimes.pyis. - Make the
overlap_timefunction accessible to that file. (Hint: You need toimportthe file). - Move the content from the
if __name__ ...block fromtimes.pyto a function calledtest_given_inputintotest_times.pyand fill the gaps forresultandexpected. (For now, you can copy the output of the program as the expected value, as if being a regression test)
def test_given_input():
...
result = ...
expected = ...
assert result == expected- run
pyteston that directory and see whether the test is picked up bypytestand whether it passes. If the test doesn't pass, see if you can find what is going wrong.
Note
If you have created the environment using uv and installed pytest with it then you need to run uv run pytest or use the VS Code testing menu.
- 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 as required in the issue for this exercise. This will link 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 the following issue in the Classwork repository.