Frequently Asked Question

Video: 6.3.3 Test parametrization
Last Updated 3 years ago


Press "Ctrl + F" to find the keyword of your interest.

If you wish to have a direct link access to the video timestamps, please follow these instructions.

Found this video helpful? Why not take the whole HIL Specialist course? A Certificate is waiting for you for free at HIL Academy.

Would you or your organization benefit from having these videos narrated in your native language? Contact us and let us know if you wish to contribute.

TRANSCRIPT

00:00:02

Hello and welcome to our third lesson on Pytest.  

00:00:05

In this lesson, we will parametrize a test using Python. 

00:00:09

To do so, we will use only the test_greater_than_2 function in  

00:00:12

file1 from the root. All the other files and folders can be deleted from our project. 

00:00:33

In the first example, we will upgrade the test from one with a fixed value of x to a  

00:00:38

test parametrized with several values. To achieve it, we can use the pytest parametrize decorator.  

00:00:44

The first argument is the name of the variable, as a string, and the second one  

00:00:48

is the collection of variable values. In this example, we would like x to vary from 1 to 5: 

00:00:55

To link the parametrized variable with a function, we must include the variable's  

00:01:00

name as an argument of the test function.So, let's execute Pytest in the command prompt.

00:01:16

We ve now run tests for all the cases from 1 through 5. In the test report,  

00:01:21

we can see that the first two tests failed and the last three passed, as expected,  

00:01:25

since 1 and 2 are not greater than 2.Further, it is also possible to parametrize  

00:01:31

several variables. For example, let s use the same parametrize decorator for two variables, x and  

00:01:37

y . To do this, let s set the first argument to be the string x, y . Then, lets convert the second  

00:01:43

argument values into a list of tuples. Lastly, lets add y as a parameter of the test function: 

00:01:50

Also, let s update the assert function to include the second variable,  

00:01:55

now checking if x*y is greater than two. 

00:02:04

Now, only the first test is failing, because 1*1 is the only combination not greater than  

00:02:09

two and all the other conditions are passing.It is also possible to parametrize the variables  

00:02:15

independently, where all combinations will be tested. Let s do a matrix combination of x and y,  

00:02:21

both from one to five. This will test all the twenty-five combinations: 

00:02:30

Here is the result of the twenty-five tests, where three of them are failing.  

00:02:34

The conditions were 1*1 , 1*2 , and 2*1 .It is important to note that the order  

00:02:42

of the decorators will change the order in which tests are executed.  

00:02:46

To better visualize that, let s multiply the Y values by 10 and collect the tests. 

00:03:00

Here you can see that for each value of Y, all values of X are tested.  

00:03:08

If you want to change that order, you can change the decorator s order: 

00:03:19

Now we changed the test order, testing all values of Y for each value of X. 

00:03:26

In this lesson, we learned to parametrize tests. On the next one, we explore Fixtures  

00:03:32

and how to use them.

00:03:34

Thank you for watching!

Please Wait!

Please wait... it will take a second!