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


Talk Python's Mastodon Michael Kennedy's Mastodon