Modern Python Projects Transcripts
Chapter: Testing code
Lecture: Parametrized tests

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Another useful feature of by testes parameterization.
0:04 You can use it when you want to test some code with different inputs.
0:08 Let's continue our example with the online shop.
0:11 You want to test that adding different numbers of item to the cart will result in
0:15 the correct cart size. So we have a first test where we add one booked
0:20 the cart and we want to make sure that the card size is equal to one
0:24 Then we try to add 10 books,
0:26 and then we would expect to have 10 items in the cart.
0:30 And then we also want to check that when we have a malicious user that tries
0:34 to add minus one item to the cart.
0:37 Our cart is empty, so all those three test looks very similar.
0:41 The only difference is the number of items and expected cart size.
0:45 We can use pytest parameterization to extract those two variables into a fixture and
0:51 turn those three tests into one test.
0:54 So, let's do that. First we have to create parametrize Fixture.
1:02 The first parameter is the number of arguments that we will use in the function.
1:06 I will explain that a bit later, so we have two variable things,
1:12 the number of items in the order and the cart size.
1:20 Then we pass a list of tupples.
1:23 Those tupples will be assigned to those parameters that we just specified.
1:27 So in one test we want to see that adding one item to the cart results
1:32 in a expected card size of one, the same for 10 items and adding -1
1:40 results in the current size of 0.
1:46 So now let's just modify this test and let's call it add_items_to_cart and
1:51 it should accept those two parameters as arguments and now we can take them and use
1:58 them in our function. If we run pytest on this file,
2:09 pytest will create three different tests and each time,
2:12 It will replace the number of items and expected cart size with the values from the
2:18 tupples in the list above. Since I don't have the code for cart written,
2:23 I made another simple example with the calculator.
2:26 So here we are, just making sure that adding numbers in python works correctly.
2:31 So we have three arguments left,
2:33 right and output, and first we want to check that adding 1 + 1 = 2
2:37 that adding 10 + 100 is 110.
2:40 and so on. So this file,
2:43 we can actually run with pytest.
2:49 And as you can see, we have four tests.
2:52 The output from pytest looks different than before because we created this pyproject.toml there
2:58 We have specified that we want to have a different output using this -array
3:02 and -q parameters. So let's see what happens if one of those parameters is
3:10 incorrect. As you can see,
3:16 we have three tests that passed and one that failed,
3:19 and pytest is actually adding the values from the parametrize fixture in the square brackets here
3:26 so we can see that we have a failing test pytest other numbers with parameters
3:31 -10 --10 --10.
3:34 So we know it's this guy that's failing.