#100DaysOfCode in Python Transcripts
Chapter: Days 10-12: Testing your code with pytest
Lecture: A TDD primer writing Fizz buzz

Login or purchase this course to watch this video and the rest of the course contents.
0:01 1 final thing is when to write your test. I think the motto "having tests is better than no tests" is the most important, but there is a whole
0:09 style of test driven development, which is to write your test before your actual code, and to drive your design by those tests.
0:17 Let's write the Fizz Buzz program, which is a children's game, and it basically is a sequence where numbers divisible by 3 and 5 return Fizz and Buzz,
0:29 and if they're both divisible by 3 and 5 it returns Fizz Buzz. So let's write that program, but do it in a TDD way, by writing the tests first.
0:38 And I'm going to use the repetitiveness of these tests to also show you a nice feature of pytest, which is parameterize.
0:52 So let's do it from the ground up. Test Fizz Buzz. So let's give it a number, and it should return Fizz, Buzz, or Fizz Buzz, or number, right?
1:05 So if I call it with 1, it should return 1. If I call it with 2, it should return 2. 3, Fizz. Actually let me stop there.
1:28 The TDD way would be to fill at the earliest possible way. And just start adding code in small increments.
1:45 Alright, so now it is recognized, but it takes zero positional arguments, but 1 was given. So here 1 was given, but I'm not accepting 1.
1:54 So let's just hard-code that here. And now the return's None. So let's just return 1 for now.
2:06 And the second test fails, so let's then decide to return N. And then if it's 3, it should return Fizz, okay,
2:19 so we need some sort of if statement, right? Alright, that works, okay, let's move on then, 4, 4.
2:47 That still works. 5, That's not working, okay. And let's accommodate that, return Buzz.
3:10 And we're green again, cool. And you already start to see a lot of repetition, right? Now there's a cool feature in pytest, called parameterize.
3:21 And let me just copy over the whole test. So I can give that a list of tuples of argument, where I call the function with, and the return value.
3:34 And then in my test, I can just do this 1 time, call Fizz Buzz with arg, and test it against return. And look at that, how I put all the parameters
3:45 in a decorator, and I avoid having to write assert, assert, Fizz Buzz, Fizz Buzz, Fizz Buzz, over and over again. So this is pretty neat.
3:55 I think you will find a use guys for this. I need to pass them into the function. And I see that Fizz does not assert Fizz Buzz,
4:05 and that this particular call, so if it's 15, it should do Fizz Buzz. There are various ways to solve this. Let's do here, then return, Fizz Buzz.
4:24 As these are returns, I don't need an elif, because these are like, or early return, or continue. So let's see if this works. And this works.
4:35 And notice that it's also nice that pytest gives a dot for every parameter test. If 1 would fail, how would that look? Ah, we already saw that, right?
4:49 We still get all the dots, and you see the actual position of the tuple that failed. And that's also, again, nicely indicated by the output.
4:57 Alright, so, this was a little bit of TDD, and also, a nice feature of pytest, parameterize, that you probably want to become familiar with.


Talk Python's Mastodon Michael Kennedy's Mastodon