PyTest Fixtures

In Previous section, we have discussed about the running test case in parallel mode. Now in this section, we will discuss more about the PyTest Fixtures in Selenium Python.

Objective:

  • What is Fixtures
  • Example with Practical Test Scenario

What is PyTest Fixtures?

Let’s think a scenario where you need to run number of test cases where one test case is considered as pre-requisite or pre-condition test for all other test cases. That pre-condition test case is generally known as setup method. Like in TestNG we have before method or before test, we need to perform the same in Selenium Python.

PyTest Fixtures

If you observer above diagram, you can see that there is a Setup method which requires to execute every test case and Tear down is the ending process. For example, Setup method is open the web page and login to the application whereas Teardown is the closing the browsers.

This can be performed with the Fixtures concept in Selenium Python. Let’s discuss an example for this scenario without Fixtures and then we will see how it is differ from Fixtures. This is very interesting concept.

For setup, we need to create a method with named “setup_modue(module)” and for Teardown “ teardown_module(module)”.

def setup_module(module):
def teardown_module(module):
from selenium import webdriver
#from selenium.webdriver.common.by import By
from webdriver_manager.chromeimport ChromeDriverManager
import pytest


driver = None

def setup_module(module):
    global driver
    driver = webdriver.Chrome(ChromeDriverManager().install())
    driver.implicitly_wait(10)
    driver.delete_all_cookies()
    driver.get("https://www.google.com")


def teardown_module(module):
    driver.quit()

def test_google_title():
    assert driver.title == "Google"


def test_Google_url():
    assert driver.url == "https://www.google.com"

To run above example, we need to execute below command in terminal.

pytest -v -s PyTest_Session/test_Google_test.py
PyTest Fixtures
PyTest Fixtures

 In PyTest you can generate the html report also. For this you need to install one module called pytest-html.

PyTest Fixtures

Now to run the test case and save the output in html format then we need to execute the below command.

pytest PyTest_Session/test_Google_test.py -v -s --html=goole_test_report.html

goole_test_report.html is the name of the file in which the html report will be saved.

PyTest Fixtures
PyTest Fixtures

After successful execution, html file will be generated and saved in root folder as above.

Let’s use Fixtures to run the same Test scenario.

To change the same scenario to Fixtures, we need to change our script little bit.

  • Need to add Decorator / Annotation to make the setup method as Fixtures.
@pytest.fixture(scope ='module')
  • Change the setup_module() method name to init_driver() and do not pass any module as parameter to init_driver() method.
def init_driver():
    global driver
print("------------------------- Setup--------------------------")
    driver = webdriver.Chrome(ChromeDriverManager().install())
    driver.implicitly_wait(10)
    driver.delete_all_cookies()
    driver.get("https://www.google.com")
  • Do not need tear down method. You can create a single method for setup and tear down. But  to specify teardown we need to add yield keyword instead of Teardown method.
yield
print("------------------------- Teardown--------------------------")
    driver.quit()

So our init_driver method should be look like as below.

@pytest.fixture(scope ='module')
def init_driver():
    global driver
    print("------------------------- Setup--------------------------")
    driver = webdriver.Chrome(ChromeDriverManager().install())
    driver.implicitly_wait(10)
    driver.delete_all_cookies()
    driver.get("https://www.google.com")
    
    yield
    print("------------------------- Teardown--------------------------")
    driver.quit()
  • Now we can add the init_driver  to other method / test cases to consider this as setup method and tear down method and run before every method.
def test_google_title(init_driver):
    assert driver.title == "Google"


def test_Google_url(init_driver):
    assert driver.current_url == "https://www.google.com/"

Now we are done. Here is our final script for Fixtures sample.

from selenium import webdriver
#from selenium.webdriver.common.by import By
from webdriver_manager.chromeimport ChromeDriverManager
import pytest


driver = None

@pytest.fixture(scope ='module')
def init_driver():
    global driver
    print("------------------------- Setup--------------------------")
    driver = webdriver.Chrome(ChromeDriverManager().install())
    driver.implicitly_wait(10)
    driver.delete_all_cookies()
    driver.get("https://www.google.com")
    
    yield
    print("------------------------- Teardown--------------------------")
    driver.quit()

def test_google_title(init_driver):
    assert driver.title == "Google"


def test_Google_url(init_driver):
    assert driver.current_url == "https://www.google.com/"

To run the above script in Fixtures mode we need to run below command.

pytest PyTest_Session/test_Fixtures.py -v -s --html=Fixtures.html

-v is to show more information in Terminal

–html is to generate HTML report after test case running

There is another way to declare fixtures name in the test case method. Rather defining inside the parameter to the test case method, we can create an annotation as below. This will consider the method as Fixtures.

@pytest.mark.usefixtures("init_driver")
from selenium import webdriver
#from selenium.webdriver.common.by import By
from webdriver_manager.chromeimport ChromeDriverManager
import pytest


driver = None

@pytest.fixture(scope ='module')
def init_driver():
    global driver
    print("------------------------- Setup--------------------------")
        driver = webdriver.Chrome(ChromeDriverManager().install())
    driver.implicitly_wait(10)
    driver.delete_all_cookies()
    driver.get("https://www.google.com")

yield
print("------------------------- Teardown--------------------------")
    driver.quit()

@pytest.mark.usefixtures("init_driver")
def test_google_title():
    assert driver.title == "Google"

@pytest.mark.usefixtures("init_driver")
def test_Google_url():
    assert driver.current_url == "https://www.google.com/"

Summary:

  • Setup method is open the web page and login to the application whereas Teardown is the closing the browsers.
  • For setup, we need to create a method with named “setup_modue(module)” and for Teardown “ teardown_module(module)”.
  • To run above example, we need to execute below command in terminal. –> pytest -v -s PyTest_Session/test_Google_test.py

Leave a Reply

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