Frequently Asked Question
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
Hello and welcome to our third lesson on Pytest.
In this lesson, we will parametrize a test using Python.
To do so, we will use only the test_greater_than_2 function in
file1 from the root. All the other files and folders can be deleted from our project.
In the first example, we will upgrade the test from one with a fixed value of x to a
test parametrized with several values. To achieve it, we can use the pytest parametrize decorator.
The first argument is the name of the variable, as a string, and the second one
is the collection of variable values. In this example, we would like x to vary from 1 to 5:
To link the parametrized variable with a function, we must include the variable's
name as an argument of the test function.So, let's execute Pytest in the command prompt.
We ve now run tests for all the cases from 1 through 5. In the test report,
we can see that the first two tests failed and the last three passed, as expected,
since 1 and 2 are not greater than 2.Further, it is also possible to parametrize
several variables. For example, let s use the same parametrize decorator for two variables, x and
y . To do this, let s set the first argument to be the string x, y . Then, lets convert the second
argument values into a list of tuples. Lastly, lets add y as a parameter of the test function:
Also, let s update the assert function to include the second variable,
now checking if x*y is greater than two.
Now, only the first test is failing, because 1*1 is the only combination not greater than
two and all the other conditions are passing.It is also possible to parametrize the variables
independently, where all combinations will be tested. Let s do a matrix combination of x and y,
both from one to five. This will test all the twenty-five combinations:
Here is the result of the twenty-five tests, where three of them are failing.
The conditions were 1*1 , 1*2 , and 2*1 .It is important to note that the order
of the decorators will change the order in which tests are executed.
To better visualize that, let s multiply the Y values by 10 and collect the tests.
Here you can see that for each value of Y, all values of X are tested.
If you want to change that order, you can change the decorator s order:
Now we changed the test order, testing all values of Y for each value of X.
In this lesson, we learned to parametrize tests. On the next one, we explore Fixtures
and how to use them.
Thank you for watching!