Getting started with pytest Transcripts
Chapter: Test Functions
Lecture: Enhanced assert tracebacks
0:00 So all of our tests are passing, which is really cool, This is great. But I want to show you some of the differences between how pytest handles asserts
0:11 and how Python handles asserts. So python versus pytest with asserts. And it's kind of some magic stuff that happens behind the scenes.
0:19 It's not magic, it's engineering, but it's really great for us. And so let's take a look at it. We'll take one of these equality test,
0:28 equality type tests and just uh make it for our own uses. So we'll grab one of these and I'll create a new file test,
0:42 test card fail and we need to import card and I'm not going to do all of this. We just need a couple of things to be different.
0:58 So we'll do Brian and Okken. Those are different and something and maybe foo so those are different. So the assertion for equality should fail.
1:13 Test card fail. Yeah, there we go. We have quite a bit of going on here, this is actually awesome. We get this assertion error.
1:32 But and it says, it says a lot already, it tells us that the there's one omitting one identical items but there's differing attributes
1:43 of summary and owner. And for the summary it's telling us that summary something is not equal to foo definitely not. But it also says use -VV.
1:54 So let's try that for more information VV. And very cool. We get um it actually tells us that there's this is neat that the state is the same.
2:09 The state matches, but there there's two different 1 matching attributes and two different attributes
2:16 That's pretty neat. Um That's definitely coming from pytest and then for the different attributes it tells us what's different.
2:25 So for uh for the summary something in foo were not equal and for the owner Brian and Okken are not equal. That's really handy. What's the difference?
2:36 Why? How do I know that's coming from pytest and not python? Well you can definitely try exactly what python gives you right out of the box if
2:45 we just run it directly. So let's do an if name equals main black This will make it so that it doesn't run when pi test imports it,
2:57 but it only runs it when we run it directly from python and test equality fail So we'll just run it from python and it still fails.
3:16 We get that assertion error. But it doesn't tell us much information so I definitely like this much information better. Thanks Pytest.