Index
- Initial Setup
2.1 Clone Project
2.2 Run Test
- Parameterized Tests
3.1 Implementation
3.2 Run Tests
- Parallel Tests
4.1 Conventions
4.2 Install pytest-xdist plugin
4.3 Running parallel tests
1. Introduction
This tutorial is the continuation of my other tutorial called “Configuring Multiple Browsers with Selenium and Python”.
Please take a look at it before starting this one.
This tutorial is based on the “Selenium WebDriver with Python” course, administered by Andrew Knight.
He also has a blog, click the link below to follow the news:
https://automationpanda.com/.
2. Initial Setup
As previously said, since this project continues the “Configuring Multiple Browsers with Selenium and Python” tutorial, you will have to clone its repo to continue your work.
2.1 Clone Project
Before you start, check if you are cloning the right branch:
“tutorial/multiple-browsers”.
Git Repo:
https://github.com/LuizGustavoR/intro-selenium-py/tree/tutorial/multiple-browsers
After cloning it check in the VSCode if you cloned the right branch:
2.2 Run Test
To make sure the cloned project is actually working,
open the “Command Prompt” inside the project folder.
Run the command below:
pipenv run python -m pytest
You should see this message:
3. Parameterized Tests
To parametrize tests in python you need to use code below before a test function:
@pytest.mark.parametrize('phrase', ['panda', 'python', 'polar bear'])
The first argument is a string with the variable name.
The second argument is a list of values or tuples.
3.1 Implementation
Pytest will run the test function once for each entry in the list, inserting each list item into the “phrase” variable and executing the test.
File “tests/test_search.py”:
"""
These tests cover DuckDuckGo searches.
"""
import pytest
from pages.search import DuckDuckGoSearchPage
from pages.result import DuckDuckGoResultPage
@pytest.mark.parametrize('phrase', ['panda', 'python', 'polar bear'])
def test_basic_duckduckgo_search(browser, phrase):
search_page = DuckDuckGoSearchPage(browser)
result_page = DuckDuckGoResultPage(browser)
# Given the DuckDuckGo home page is displayed
search_page.load()
# When the user searches for the phrase variable
search_page.search(phrase)
# And the search result query is the phrase variable
assert phrase == result_page.search_input_value()
# And the search result links pertain to the phrase variable
# for title in result_page.result_link_titles():
# assert PHRASE.lower() in title.lower()
# Then the search result title contains the phrase variable
assert phrase in result_page.title()
3.2 Run Tests
Run the command below:
pipenv run python -m pytest
As you can see.
Web UI tests are slow.
4. Parallel Tests
To add a parallel test to pytest use the “pytest-xdist” plugin.
“pytest-xdist” can control multiple test threads in one machine and can also distribute them across multiple machines.
4.1 Conventions
- Make sure all tests are truly independent, avoid making one test dependent upon the setup or outcome of another test.
- Should be able to run by itself without any other tests.
- Should be able to run at any order.
- Avoid test collisions, it could happen when tests access a shared state, like one test changing the user password while another test tries to do a login with the previous password and fails.
4.2 Install pytest-xdist plugin
To install the “pytest-xdist” plugin open the “Command Prompt” inside the project folder:
Then run the command below:
pipenv install pytest-xdist
You should see this message:
4.3 Running parallel tests
Now run the command below:
pipenv run python -m pytest -n 3
PS: “-n” means you want to run the tests in parallel and the number “3” means the number of threads you want.
5. Repository
I created a new branch called “tutorial/parallel-tests” for this tutorial, click the link below to see it:
https://github.com/LuizGustavoR/intro-selenium-py/tree/tutorial/parallel-tests
The end.
Olá Luíz! Bom dia.
Chamo-me António e saúdo de Portugal.
Estou com um problema a nível de execução paralela e estava a pesquisar soluções quando encontrei este teu blog.
Já tiveste problemas com concorrência a correr testes de Selenium paralelos com o xdist? Podes partilhar como resolveste?
Olá António.
Nunca tive problemas com o xdist, sempre funcionou normal.
Se tiver algum problema com ele para executar o projeto pode compartilhar a mensagem de erro aqui que vou ver o que se passa.