Getting started with pytest Transcripts
Chapter: Test Functions
Lecture: Using pytest.fail() and Exceptions
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Assertions aren't the only way we can cause a test to fail. We can also use really any exception.
0:06
And there's a special way within pytests to raise an exception and it's just called pytest fail. So let's try that. Let's do a new test card,
0:24
alt fail, and I'm gonna go ahead and copy this bit. And instead of assertions, let's just say if If C1 is not equal to C2
0:43
pytest fail, they are not equal since it's coming from pytest, we have to import pytest, and now let's try running that.
1:10
So we still got one test failed, so that worked, but it just doesn't give us very many very much information,
1:16
so I definitely would recommend using asserts when you can, but it's nice to have this around as a another way. Now we can also just do any exception,
1:30
so we can say let's do another one. Just raise an exception, and the second one also failed with just a raw exception
1:55
so really any exception any exception works, but that's just weird, I if I were to see this in a test, I would I would not understand what's going on.
2:06
However, a pytest fail makes a little bit more sense. Anyway, personal preference, but I prefer probably assert first,
2:16
then Pytest fail and then some other exception, but it is good to know that if any of your code raises an exception that
2:23
you're trying to test that you didn't expect, it will also fail a test. So that's good.