PyTest – Conftest.py

In Previous section, we have discussed about the class scope and Parameters in PyTest Fixtures. In this tutorial we will discuss about uses of PyTest-Conftest.py. Addition to this we can discuss how to run PyTest Fixtures using Conftest.py file.

Objective:

  • What is Conftest.py
  • Use of Conftest.py file
  • Fixtures with Conftest.py
  • Example
PyTest - Conftest.py

What is Conftest.py?

Fixtures are the most powerful feature of PyTest. It helps user to define or setup the pre condition or to define some configuration to your script file like data base connection, handling data from other sources.

Fixtures can be implemented in individual file with class scope and parameters. In this case it will the individual file specific. But if we want to share the Fixtures to multiple files then we need to use Conftest.py file. If we define Conftest.py file on our scripts then all of out tests classed and modules in our directory can access the Fixtures file and share all configuration so that we can optimize our script.

Use of Conftest.py

Plugin is actually defined in our project or may be in other module. Through Conftest.py file these external plugins or modules can be imported. Addition to these we can also load the plugins to our project by defining the Conftest.py file.

For Example:

@pytest.fixture()
def fixture():
    return "some stuff"

Fixtures with Conftest.py:

Conftest.py file is called as configuration file. You can create one or multiple fixtures over here. Use the same fixtures method name in your test class which will be running successfully. This is called global precondition with the help of configuration test file which is Conftest.py. This is amazing fixtures in PyTest framework.

Let’s create the Conftest file in our directory.

conftest.py:

from selenium import webdriver
from webdriver_manager.chromeimport ChromeDriverManager
import pytest
from webdriver_manager.firefoximport GeckoDriverManager


@pytest.fixture(params=["chrome", "firefox"], scope='class')
def init_driver(request):
    if request.param == "chrome":
        driver = webdriver.Chrome(ChromeDriverManager().install())
        request.cls.driver = driver
    elif request.param == "firefox":
        driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
        request.cls.driver = driver

    request.cls.driver = driver
    yield
    driver.quit()

Let’s create our test file.

test_Fixtures_params.py:

fromselenium import webdriver
from webdriver_manager.chromeimport ChromeDriverManager
import pytest
from webdriver_manager.firefoximport GeckoDriverManager


@pytest.mark.usefixtures("init_driver")
class Base_Test:
    pass



class Test_Google(Base_Test):
    def test_title(self):
        self.driver.get("http://www.google.com")
        assert self.driver.title == "Google"

Summary:

  • Fixtures are the most powerful feature of PyTest.
  • It helps user to define or setup the pre condition or to define some configuration to your script file like data base connection, handling data from other sources.
  • If we define Conftest.py file on our scripts then all of out tests classed and modules in our directory can access the Fixtures file. It shares all configuration so that we can optimize our script.
  • We can load the plugins to our project by defining the Conftest.py file.
  • You can create one or multiple fixtures in Confest.py file.

Leave a Reply

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