#100DaysOfCode in Python Transcripts
Chapter: Days 10-12: Testing your code with pytest
Lecture: Testing a program's stdout with capfd

Login or purchase this course to watch this video and the rest of the course contents.
0:00 The last video I forgot to run my coverage report, so let's do that now. So I'm in test, and we see an improvement. We went from 24% to 56%,
0:13 and it still shows the missing lines, which are going further down into the script. So let's continue. Next up, validate guess.
0:22 And the doc string says: verify if guess is correct? If so print, guess is correct, otherwise it's too low. Whoops.
0:32 Guess is too high or too low, depending what it is. And it returns a boolean. So let's write: def test, validate guess.
0:43 And I'm going to show you another feature of pytest, which I will explain you in a bit, which is capfd. And that will capture the standard output
0:53 of the program and execution. Very useful because for this method, I not only want to check for the boolean return value
1:00 but I also want to see if the actual output by the function to print, that the function does because we made this a game, prints to the console
1:09 and I want to have accurate information for the user. So let's make a game. I set the answer. So let's validate that one is not a winning number.
1:24 Validate guess. So I call this with one. One goes into this method. It checks against answer. If it's answer, true, if it's not answer, false.
1:36 And to say false in pytest, you can just say, assert not this method is truthy. So this return false, not false is true. So let's run this.
1:49 And that one passes as well. Then of course it's easy to do the same for higher an assertion. So if it's three, it's still not good,
2:02 and if it's two, it's good because that's the answer defined. And now back to capfd. If I actually want to see what the print is printing
2:12 to the console, because that's what you see before, if we run the game. It's printing these kind of feedbacks to the user.
2:22 So I want to test if these are what I'm expecting. And one very useful trick is to redirect the output, standard output, the error I just throw away
2:32 and I use capfd, which I passed into the function, and I'm calling its readout error: method. And let's just see what that gives me.
2:43 I'm not going to do this for now. If you want to print inside your test, one way with pytest to show that to the console is to add the -s.
2:52 And that actually stands for: shortcut for capture equals no. So it's not capturing the output, meaning in this scenario it prints it to the console.
3:04 So when it's been intermingled into these dots but this is the actual print statement, there's also a new line. So one is too low.
3:12 I captured that in the output variable, which I printed to the console. So capfd is very cool to capture output printed by your program.
3:20 And now it can make assertions on that, as on any other thing. So I can say, out, and that's strip of the new line, right strip equals one is too low.
3:37 Save, control z, pytest, and it worked. And if I would say too high, it would fail. And look at that nice output.
3:48 So that works and I can do the same for the other two. So let's just copy this. Too high and two is the right answer. So in that case, two is correct.
4:02 Let me just not do it 100% correct I you'll see. Oh, one is too high. Oh sorry, this is three. So you'll be targeting a lot between running tests
4:16 and writing test code, that's something you need to make a habit of. Still something bad: assert not true. Wait a second. Ah!
4:26 Typical copy and past error. Ah. And here, it's nice how pytest shows that, not only the diff, but also, the actual character that's missing.
4:37 So that's now very easy to spot and fix. There you go, we are green again, and let me run comma T. Wow we have 68% coverage now.

Talk Python's Mastodon Michael Kennedy's Mastodon