In this tutorial, we will parameterize the tests using fixtures. To know what is fixture, please refer to this tutorial – What is Fixture in PyTest?.
Table of Contents
- What is the parameterized test?
- Parameterization using Fixtures
- How to pass multiple parameters in a method using Fixtures
- @pytest.mark.parametrize: Parametrize test functions
What is the parameterized test?
Parameterized tests allow a developer to run the same test over and over again on different values. For example, we want to test the login page or process, with multiple input values for e.g. say 10 different username and password conditions.
Parameterization using Fixtures
It allows us to run the same test function with different sets of inputs, often by defining those inputs as fixtures.
In the below example, let us create a conftest.py file.
@pytest.fixture()
def dataload():
print("User Profile data")
return ["Vibha","Singh","qaautiomation.expert"]
Now, let us create a new file – test_demo1.py.
import pytest
@pytest.mark.usefixtures("dataload")
class TestExample:
def test_edit_profile(self, dataload):
print(dataload)
print("First Name :", dataload[0])
print("Surname :", dataload[1])
print("Website :", dataload[2])
The output of the above program is
In the above example, the test_edit_profile function is parameterized using @pytest.mark.usefixtures(“dataload”). It allows you to run the same test with different input values by providing a list of tuples specifying the parameters and expected values. In this case, the test will be executed three times, each time with different values for Vibha, Singh, and qaautomation.expert.
In the example below, Chrome, Firefox and edge are used as parameters to the fixture functions with the scope as class i.e. @pytest.fixture(params=[“chrome”, “firefox”,”edge”])
In the below example, let us create a fixture in conftest.py file.
import pytest
@pytest.fixture(params=["chrome", "firefox", "edge"])
def cross_browser(request):
if request.param == "chrome":
print ("Browser selected is chrome")
elif request.param == "firefox":
print("Browser selected is firefox")
elif request.param == "edge":
print("Browser selected is edge")
return request.param
Now, let us create a method in a new file – test_demo2.py.
def test_cross_browser(cross_browser):
print(cross_browser)
The output of the above program is
How to pass multiple parameters in a method using Fixtures
In the below example, we will pass multiple parameters for a run. We can see that for the first run, the parameters used are “chrome”, “Vibha”, “Singh”, the parameters used for the second run are “firefox”, “qaautomation.expert” whereas, for the third run, it is “edge”, “QA”.
import pytest
@pytest.fixture(params=[("chrome","Vibha","Singh"), ("firefox","qaautomation.expert"), ("edge","QA")])
def multiple_parameters(request):
return request.param
Now, let us create a method in a new file – test_demo3.py.
def test_multiple_parameters(multiple_parameters):
print(multiple_parameters)
print("First parameter :", multiple_parameters[0])
print("Second parameter :", multiple_parameters[1])
The output of the above program is
@pytest.mark.parametrize: Parametrize test functions
@pytest.mark.parametrize allows one to define multiple sets of arguments and fixtures for the test function or class.
import pytest
@pytest.fixture
def arithmetic_fixture(request):
factor = request.param
yield factor
@pytest.mark.parametrize("arithmetic_fixture", [6, 7, 9], indirect=True)
def test_arithmetic(arithmetic_fixture):
result1 = arithmetic_fixture * 5
result2 = arithmetic_fixture + 11
result3 = arithmetic_fixture - 3
print(arithmetic_fixture)
print("Multiplication :", result1)
print("Addition :", result2)
print("Subtraction :", result3)
The output of the above program is
Here, the @parameterize decorator defines three different (arithmetic_fixture) tuples so that the test_arithmetic function will run three times using them in turn.