PyTest Fixtures with Class Scope & Parameters

In Previous section, we have discussed about the PyTest Fixtures fundamentals and with scope module. Now we will see how we will implement PyTest Fixtures with Class Scope & Parameters.

Objective:

  • Run Fixtures in class scope
  • Example
  • PyTest Fixtures with Parameters
PyTest Fixtures with Class Scope

How to run Fixtures in Class Scope?

In last section we have used the module in scope for Fixtures in PyTest. Now instead of module we will run the Fixtures with class level. For this we need to mention the scope as class in Fixtures annotation as below.

@pytest.fixture(scope= 'class')

For this we need to create 2 class as below.

class Base_Chrome_Test:
    pass


class Test_Google_Chrome(Base_Chrome_Test):
    def test_google_title_chrome(self):
        self.driver.get("http://www.google.com")
        assert self.driver.title == "Google"
class Base_Firefox_Test:
    pass


class Test_Firefox_Chrome(Base_Firefox_Test):
    def test_google_title_firefox(self):
        self.driver.get("http://www.google.com")
        assert self.driver.title == "Google"

Here we have created 2 classes. One is for Chrome and other is for Firefox. For these 2 classes we need 2 fixtures. One is for Chrome and other is for Firefox as below.

@pytest.fixture(scope= 'class')
def init_Chrome_driver(request):
    chrome_driver = webdriver.Chrome(ChromeDriverManager().install())
    request.cls.driver = chrome_driver
    
    yield
    chrome_driver.quit()

@pytest.fixture(scope= 'class')
def init_Firefox_driver(request):
    firefox_driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
    request.cls.driver = firefox_driver
    
    yield
    firefox_driver.quit()

Now we will this fixtures name before every class to make the class for Fixtures enable as below.

@pytest.mark.usefixtures("init_Chrome_driver")
@pytest.mark.usefixtures("init_Firefox_driver")

Now we are done. Have a look into the final script below.

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


@pytest.fixture(scope= 'class')
def init_Chrome_driver(request):
    chrome_driver = webdriver.Chrome(ChromeDriverManager().install())
    request.cls.driver = chrome_driver
    
    yield
    chrome_driver.quit()

@pytest.fixture(scope= 'class')
def init_Firefox_driver(request):
    firefox_driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
    request.cls.driver = firefox_driver
    
    yield
    firefox_driver.quit()

@pytest.mark.usefixtures("init_Chrome_driver")
class Base_Chrome_Test:
    pass


class Test_Google_Chrome(Base_Chrome_Test):
def test_google_title_chrome(self):
    self.driver.get("http://www.google.com")
    assert self.driver.title == "Google"



@pytest.mark.usefixtures("init_Firefox_driver")
class Base_Firefox_Test:
    pass


class Test_Firefox_Chrome(Base_Firefox_Test):
def test_google_title_firefox(self):
    self.driver.get("http://www.google.com")
    assert self.driver.title == "Google"

Let’s run the above script with the help of below command.

pytest PyTest_Session/test_Fixture_with_class.py -v -s --html=Fixtures _class.html

Here first it test case will be executed with the Chrome browser and 2nd will be executed by the Firefox Browser.

PyTest Fixtures with Parameters:

In last section we have seen that with the class scope we are creating 2 fixtures for Chrome and Firefox and pass to the class fixtures annotations. But with the help of Parameters we can create one Class and one Fixture and based on the parameters it will execute the test case for Chrome and Firefox.

Let’s have a look on below example.

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()

@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"

Output:

Summary:

  • Instead of module we can run the Fixtures with class level.
  • We can create fixtures with class scope with the command : @pytest.fixture(scope= ‘class’)
  • With the help of Parameters we can create one Class and one Fixture and based on the parameters it will execute the test case for Chrome and Firefox

Now we have understood the PyTest Fixtures with Class Scope & & Parameters in details. In next section we will understand more on PyTest Fixtures with Confest

Leave a Reply

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