UCL-COMP0233-2023-2024 / RSE-Classwork

3 stars 65 forks source link

Avoid code repetition with parametrize #21

Open dpshelio opened 11 months ago

dpshelio commented 11 months ago

Now that you have written four different positive tests for times.py, take a step back and look at your code: There is a lot of repetition, almost every test (apart from the negative test) essentially does the same (albeit with different data), which makes our test code harder to change in the future.

We can use pytest.mark.parametrize to get our tests DRY (Don't Repeat Yourself).

You will need the test function to accept parameters, for example time_range_1,time_range_2 and expected, let the parametrize decorator know about it as its first argument and pass a list of tuples of length 3 with the values for each test.

Commit your solution including Answers UCL-COMP0233-23-24/RSE-Classwork#21 in the message, and push it to GitHub.

What are the advantages and disadvantages of using parametrize in this case?


Sample solution