Running tests in parallel
When we have a lot of test cases in our framework, it takes a lot
of time to execute all the test cases.Instead of running tests sequentially, or
one after the other, parallel testing allows us to execute multiple tests at
the same and decrease the time taken. Parallel testing helps to faster testing
and has a quicker turn around in release process.
To run tests in parallel using pytest, we need pytest-xdist plugin
which extends pytest with some unique test execution modes.
Let's first install pytest-xdist plug-in by running below command
pip install pytest-xdist
after installing pytest-xdist module, you can execute your scripts parallely.
Let’s look at the example below.
First create conftest.py file like below
import pytest
from selenium import webdriver
@pytest.fixture
def WebDriver():
base_url = "http://automationpractice.com/"
chromePath = "C:\\executables\\chromedriver.exe"
driver = webdriver.Chrome(executable_path=chromePath)
driver.get(base_url)
return driver
from selenium import webdriver
@pytest.fixture
def WebDriver():
base_url = "http://automationpractice.com/"
chromePath = "C:\\executables\\chromedriver.exe"
driver = webdriver.Chrome(executable_path=chromePath)
driver.get(base_url)
return driver
Now create test file test_parallel.py and add below code here we
are using two test methods.
import pytest
import time
def test_one(WebDriver):
WebDriver.find_element_by_xpath("//a[@title='Log in to your customer account']").click()
WebDriver.find_element_by_xpath("//input[@id='email']").send_keys("abc111@gmail.com")
WebDriver.find_element_by_xpath("//input[@name='passwd']").send_keys("abcd1234")
WebDriver.find_element_by_xpath("//button[@type='submit' and @id='SubmitLogin']").click()
def test_two(WebDriver):
WebDriver.find_element_by_xpath("//a[@title='Log in to your customer account']").click() WebDriver.find_element_by_xpath("//input[@id='email']").send_keys("abc111@gmail.com")
WebDriver.find_element_by_xpath("//input[@name='passwd']").send_keys("abcd12341")
WebDriver.find_element_by_xpath("//button[@type='submit' and @id='SubmitLogin']").click()
errormsg= WebDriver.find_element_by_xpath("//div[@class='alert alert-danger']//li").text
assert (errormsg=="Authentication failed.")
import time
def test_one(WebDriver):
WebDriver.find_element_by_xpath("//a[@title='Log in to your customer account']").click()
WebDriver.find_element_by_xpath("//input[@id='email']").send_keys("abc111@gmail.com")
WebDriver.find_element_by_xpath("//input[@name='passwd']").send_keys("abcd1234")
WebDriver.find_element_by_xpath("//button[@type='submit' and @id='SubmitLogin']").click()
def test_two(WebDriver):
WebDriver.find_element_by_xpath("//a[@title='Log in to your customer account']").click() WebDriver.find_element_by_xpath("//input[@id='email']").send_keys("abc111@gmail.com")
WebDriver.find_element_by_xpath("//input[@name='passwd']").send_keys("abcd12341")
WebDriver.find_element_by_xpath("//button[@type='submit' and @id='SubmitLogin']").click()
errormsg= WebDriver.find_element_by_xpath("//div[@class='alert alert-danger']//li").text
assert (errormsg=="Authentication failed.")
Now execute the tests. In PyTest, to execute tests just type
pytest in the terminal in the project root folder / tests folder. This will
execute all tests.
Pytest test_parallel.py -v -n 2
-n <num> runs the tests by using multiple workers, here it
is 2.
Comments
Post a Comment