Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pytest.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ It takes two arguments: a *tuple or string* with the *names* of the parameters y

15. It's time to add explicit exception handling to our functions. You will probably want to do `raise ValueError("array size mismatch")` for the case where arrays sizes are different, and `raise TypeError("arguments should be lists")` for when the arguments are not lists.

16. Now, we can add new test functions named `test_add_arrays_error()` and so on, where we check if errors are being raised correctly. That is done by wrapping our function call with `with pytest.raises(ValueError)`, for example. What happens when you run `pytest` now? Add checks for both possible errors we came up with. What other cases can happen in e.g. `divide_arrays()`?
16. Now, we can add new test functions named `test_add_arrays_error()` and so on, where we check if errors are being raised correctly. That is done by wrapping our function call with `with pytest.raises(ValueError)`, for example. What happens when you run `pytest` now? Add checks for both possible errors we came up with. Better yet, use the [`match` keyword argument](https://docs.pytest.org/en/stable/reference/reference.html#pytest-raises) to check the string and ensure *the right* ValueError was raised! What other cases can happen in e.g. `divide_arrays()`?

17. We have successfully found a way to separate the data to be tested from the code to be tested. `pytest` has an even better way to do that for more complex cases, for example, when you want multiple test functions using the same data. They are called [_fixtures_](https://docs.pytest.org/en/stable/explanation/fixtures.html#about-fixtures).
A _fixture_ is defined as a function that returns something we want to use repeatedly in our tests. `pytest` provides [some fixtures out of the box](https://docs.pytest.org/en/stable/reference/fixtures.html), like the very useful `tmp_path` fixture that gives you a unique temporary location.
Expand Down