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