What is a fixture in PyTest framework?

The fixture in PyTest framework is used to run a precondition and a postcondition to a test method similar to a setup and tear down method. We have to use the expression @pytest.fixture() followed by the fixture test method to introduce a fixture. Moreover, we have to include the import pytest package in our code.

Now to connect a test method to a fixture, we have to pass the name of the test method of the fixture as a parameter to that test method. To execute the postcondition statement we have used the yield keyword. So first the fixture test method shall execute, then the actual test method and finally statements after the yield keyword shall execute.

Now let us see implementation of test_fixture.py file with fixture.

import pytest

# fixture text method

@pytest.fixture()

def setup():

    print("Precondition statement")

# postcondition statement   

    yield

    print("Postcondition")

# actual test method

def test_fixture(setup):

    print("Actual test scenario")

Output:

The output shows that at first the Precondition statement got printed in the console since it is part of the fixture setup method. Then Actual test scenario got printed in the console as it is a part of the actual test method test_fixture. Finally Postcondition got printed as it appears after the yield keyword.

Please note the fixture test method setup is passed as a parameter to the test_fixture method.

Conclusion: Thus we have seen how to use fixtures in Pytest. For more details, you can refer to the link:

https://courses.rahulshettyacademy.com/courses/learn-selenium-automation-in-easy-python-language/lectures/13248486

In the next section we shall discuss how to use conftest file in Pytest.

How to use conftest file in PyTest

There may be scenarios in which multiple Pytest files having test methods inside a package are having the same precondition and postcondition similar to a setup and tear down method. Instead of repeating the same fixture several times, Pytest recommends to create a conftest file which shall contain the generalized prerequisites and post requisites to the test methods.

Thus a conftest file can be shared by all Pytest files inside the same package and we can have an optimized code. We have to use the expression @pytest.fixture() followed by the fixture test method. Moreover, we have to include the import pytest package in our code.

Now to connect test methods to the conftest fixture, we have to pass the name of the fixture test method as a parameter to the actual test method. To execute the postcondition statement we shall use the yield keyword. So first the fixture test method shall execute, then the actual test methods and finally statements after the yield keyword shall execute.

Let us see the implementation of conftest.py file.

import pytest

# fixture test method

@pytest.fixture()

def setup():

    print("Precondition statement")

# post condition statement

    yield

    print("Postcondition")

Implementation of actual test scenario in test_fixture.py

import pytest

# test method with fixture method name in conftest file as parameter

def test_fixture(setup):

    print("Actual test scenario")

Output:

The output shows that at first Precondition statement got printed in the console since it is part of the fixture setup method. Then Actual test scenario got printed in the console as it is a part of the actual test method test_fixture. Finally Postcondition got printed as it appears after the yield keyword.

Please note the fixture test method setup is passed as a parameter to the test_fixture method.

Conclusion: Thus we have seen how to use conftest file in Pytest. For more details, you can refer to the link:

https://courses.rahulshettyacademy.com/courses/learn-selenium-automation-in-easy-python-language/lectures/13248489

In the next section we shall discuss generic fixtures in Pytest.

How to use generic fixtures in Pytest

Let us discuss a scenario in which multiple test methods have to execute the same precondition and postcondition. If a single fixture is to be repeated several times in a Pytest file, we have to use the expression @pytest.mark.usefixtures("<fixture test method name>") at the class level in the Pytest file.

 Implementation of actual test scenarios in test_fixture.py file.

import pytest

            # fixture method setup declared at the class level

@pytest.mark.usefixtures("setup")

class TestExample:

# test method 1 with setup fixture method for pre and post condition

    def test_fixture(self):

        print("Actual test scenario1")

# test method 2 with setup fixture method for pre and post condition

    def test_fixture1(self):

        print("Actual test scenario2")

Let us see the implementation of conftest.py file

import pytest

# fixture method

@pytest.fixture()

def setup():

    print("Precondition statement")

# post condition statement

    yield

    print("Postcondition")

Output:

The output shows that the Precondition statement got printed twice in the console before test methods test_fixture and test_fixture1 and Postcondition got printed twice in the console after test methods test_fixture and test_fixture1 since they are a part of the fixture setup method. Actual test scenario1 and Actual test scenario2 got printed in the console as they are a part of the actual test methods test_fixture and test_fixture1.

Please note the fixture test method setup is passed as a parameter to the @pytest.mark.usefixtures("setup") and the same precondition and postcondition being executed by two different test methods.

Conclusion: Thus we have discussed the generic fixtures in Pytest. For more details, you can refer to the link:

https://courses.rahulshettyacademy.com/courses/learn-selenium-automation-in-easy-python-language/lectures/13248489

In the next section we shall discuss the scope of fixtures in Pytest.

How to use scope in fixtures in Pytest

Let us discuss another scenario in which we need to run a precondition method only once before all the test methods in the Pytest file and run a postcondition method only once after all the test methods in the Pytest file.

Inside the conftest file we have to add an expression @pytest.fixture(scope="class") followed by the fixture test method.

Let us see the implementation of conftest.py file with scope.

import pytest

# fixture method with scope set to class and executed by all test methods

@pytest.fixture(scope="class")

def setup():

    print("Precondition statement")

# post condition statement

    yield

    print("Postcondition")

Implementation of actual test scenarios in test_fixture.py file

   import pytest

            # fixture method setup to be executed before class initiation

@pytest.mark.usefixtures("setup")

class TestExample:

# test method 1

    def test_fixture(self):

        print("Actual test scenario1")

# test method 2

    def test_fixture1(self):

        print("Actual test scenario2")

Output:

The output shows that the Precondition statement got printed once in the console before the TestExample class initiation. Hence the fixture test method setup got executed before the test methods test_fixture and test_fixture1. Then the test methods test_fixture and test_fixture1 got executed. Finally Postcondition got printed once in the console for executing the line after the yield in setup fixture method.

Please note the fixture test method setup is passed as a parameter to the @pytest.mark.usefixtures("setup") and scope is passed as a parameter to the @pytest.fixture(scope="class") in the conftest file.

Conclusion: Thus we have discussed the scope of fixtures in Pytest. For more details, you can refer to the link:

https://courses.rahulshettyacademy.com/courses/learn-selenium-automation-in-easy-python-language/lectures/13248489

In the next section we shall discuss data driven fixtures in Pytest.

How to use data driven fixtures in PyTest

Let us discuss another scenario in which we will introduce data to our conftest file and bring that data at run time to the test methods of the Pytest file. We have to use the expression @pytest.fixture() followed by the fixture test method containing the data.

In the Pytest file, we have to use the expression @pytest.mark.usefixtures("<fixture test method name>") at the class level. Also, since the fixture method is returning data in tuple format to the actual test method, we have to pass the name of the fixture as a parameter to the test method.

 Let us see the implementation of conftest.py file with data driven fixture.

import pytest

# fixture method

@pytest.fixture()

def dataLoad():

    print("user profile data is being created")

# data being returned in tuple format  

    return ["Rahul","Shetty","Academy"]

Implementation of actual test scenarios in test_fixture.py.

import pytest

            # test method with fixture method name as parameter

@pytest.mark.usefixtures("dataLoad")

class TestExample:

# fixture method passed as parameter as data is returned from fixture

    def test_editprofile(self, dataLoad):

        print(dataLoad)

Output:

The output shows that user profile data is being created and ["Rahul", "Shetty","Academy"] got printed once in the console.

Please note the fixture test method dataLoad is passed as a parameter to the @pytest.mark.usefixtures("dataLoad") at the class level and also as a parameter to the actual test method test_editprofile(self, dataLoad) and ["Rahul", "Shetty","Academy"] are passed from the conftest file.

Conclusion: Thus we have discussed the data driven fixtures in Pytest. For more details, you can refer to the link:

https://courses.rahulshettyacademy.com/courses/learn-selenium-automation-in-easy-python-language/lectures/13248491

In the next section we shall discuss multiple data parameterization with fixtures in Pytest.

How to use multiple data sets for parameterization in fixtures in Pytest

Let us discuss another scenario in which we deal with multiple data sets for parameterization in fixtures. We have to use the expression @pytest.fixture(params=["<data set1>", "<data set2>"] followed by the fixture test method. The number of times the actual test method shall execute will depend on the number of the data sets passed with the params parameter.

To send the multiple data sets to the fixture test method we have to use the request instance which belongs to the fixture. Finally the data is returned with the request.param statements to the actual test method in tuple format.

Let us see the implementation of conftest.py file with fixtures with parameters.

import pytest

# fixture method passing two parameters

@pytest.fixture(params=["chrome","firefox"])

def crossBrowser(request):

# data returned to actual test method

    return request.param

Implementation of actual test scenario in test_fixture.py

import pytest

# test method 1 with fixture method name as parameter

           def test_crossBrowser(crossBrowser):

                    print(crossBrowser)

Output:

The output shows that the chrome and firefox got printed in the console. Also, the number of test cases executed is shown as two as the same test case got executed two times with the different data sets passed with params in the fixture- @pytest.fixture(params=["chrome","firefox"]).

Please note the name of the fixture test method crossBrowser is passed as a parameter to the actual test method test_crossBrower.

Conclusion

Thus we have discussed multiple data parameterization with fixtures in Pytest. For more details, you can refer to the link:

https://courses.rahulshettyacademy.com/courses/learn-selenium-automation-in-easy-python-language/lectures/13248496

In the next post, we shall discuss how to generate HTML reports with Pytest.


Tags


You may also like

API Basics and Architecture

API Basics and Architecture
Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}