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