Run Test case Parallel with Markers

In Previous section, we have discussed about some basics of PyTest and how to define the test cases, how to run all test case in one shot and run some specific test cases with respect to Keyword. Now in this session we will learn about how to Run Test case in Parallel with Markers and how to run test cases in groups. We can give markers to all the test case like group and and run based on the markers.

Objective:

  • What is Parallel Test case
  • Run Test case in parallel Mode

What is Parallel Test case:

Before going to deep on Run Test case Parallel with Markers, let’s discuss about what is Parallel test case.

Parallel Test case is the type of test case in which we can run multiple test case parallel at the same time. In Parallel test case we can run more number of test case in less time and we can run the same test cases in different OS, platform, browsers etc. at the same time which can save the resource, time and effort.

Unlike Selenium Java, in Selenium Python running test case in parallel is very easy. You do not need to provide any verbose command like parallel flag etc. Within one line you can run the test case parallel in Selenium Python.

Run Test case Parallel with Markers

Run Test case in Parallel Mode:

Run Test case in parallel mode means all the specified test case will be running parallel. It will be very useful when you want to perform cross browser testing at same time or run multiple test cases at the same time to run all test cases in less time.

In Selenium Python, to run the test cased in parallel manner, we need a package to be installed called xdist. This is not coming default. We need to install to our application so that we can use this to our application by importing. To install xdist , run below command in terminal.

pip install pytest-xdist

Now you can see xdist is installed completely to you application.

Now let’s have a look on below example.

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


def test_facebook():
    driver = webdriver.Chrome(ChromeDriverManager().install())
    driver.implicitly_wait(10)
    driver.get("http://www.facebook.com")
    assert driver.title == "Facebook - log in or sign up"
    print("Facebook Test case")
    driver.quit()


def test_google():
    driver = webdriver.Chrome(ChromeDriverManager().install())
    driver.implicitly_wait(10)
    driver.get("http://www.google.com")
    assert driver.title == "Google"
    print("Google Test case")
    driver.quit()


def test_instagram():
    driver = webdriver.Chrome(ChromeDriverManager().install())
    driver.implicitly_wait(10)
    driver.get("http://www.instagram.com")
    assert driver.title == "Instagram"
    print("Instagram Test case")
    driver.quit()


def test_gmail():
    driver = webdriver.Chrome(ChromeDriverManager().install())
    driver.implicitly_wait(10)
    driver.get("http://www.gmail.com")
    assert driver.title == "Gmail"
    print("Gmail Test case")
    driver.quit()

In the above example, we have created 4 methods which signifies 4 test case because all methods name starts with test_. Let’ run the test case by executing below command in terminal without parallel and observe the output.

py.test PyTest_Session/test_Webpage_Login.py

Here you can see each method will be running in alphabetical order one by one. You can see all test cases are running in alphabetical order.

Now let’s run all these 4 test cases parallel. To run test cases in parallel mode you need to add 3 arguments in above command and Enter.

py.test PyTest_Session/test_Webpage_Login.py -n 4

-n is signifying to run the test case parallel

4 is the number of test cases to be executed parallel.

In the above example you can see all 4 test methods are running parallel.

Summary:

  • Parallel Test case is the type of test case in which we can run multiple test case parallel at the same time.
  • Unlike Selenium Java, in Selenium Python running test case in parallel is very easy. You do not need to provide any verbose command like parallel flag etc. Within one line you can run the test case parallel in Selenium Python.
  • In Selenium Python, to run the test cased in parallel manner, we need a package to be installed called xdist.

Leave a Reply

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