February 2022
Intermediate to advanced
274 pages
6h 28m
English
We’ve looked at how any exception can cause a test to fail. But what if a bit of code you are testing is supposed to raise an exception? How do you test for that?
You use pytest.raises() to test for expected exceptions.
As an example, the cards API has a CardsDB class that requires a path argument. What happens if we don’t pass in a path? Let’s try it:
| | import cards |
| | |
| | |
| | def test_no_path_fail(): |
| | cards.CardsDB() |
| | $ pytest --tb=short test_experiment.py |
| | ===================== test session starts ====================== |
| | collected 1 item |
| | |
| | test_experiment.py F [100%] |
| | |
| | =========================== FAILURES =========================== |
| | ______________________ test_no_path_fail _______________________ ... |
Read now
Unlock full access