In our previous few blog posts, we have learned how to write selenium tests using python, we will extend our learning to use pytest as our unit testing framework and write a simple selenium test using pytest and learn more about this library.
Prerequisite –
Configure & run python on PyCharm
Write first selenium test using python
Pytest features –
- Easy to write tests
- Increase readability of tests
- Out of box assert feature to validate
- Run multiple tests
- Categorise tests
- Pytest fixtures help to share common tests & testdata across classes, modules, packages or session
Pytest installation –
Create a new python project or open an existing project
In terminal, type
pip install -U pytest
Verify the installation
pytest --version
Rules to write tests in pytest
File name
File name should start or end with “test”
test_*.py or *_test.py
Test method name
Method name should start with “test”
test_*
Pytest considers above 2 rules to run the tests, else it can’t pick the tests.
Let’s run a sample selenium test using pytest
Create a new file – test_samplePytest.py
Note – Male sure you download latest version of browser driver with respect to your browser version to run the test.
# author - qavalidation.com # test_samplePytest.py import pytest from selenium import webdriver import time from selenium.webdriver.common.by import By def test_sampleNav(): driver = webdriver.Chrome("/Users/[userName]/sel/chromedriver") # driver.maximize_window() driver.implicitly_wait(30) driver.set_page_load_timeout(50) driver.get("https://qavbox.github.io/demo") assert "QAVBOX" in driver.title driver.find_element(By.LINK_TEXT, "SignUp Form").click() # driver.find_element_by_link_text("SignUp Form").click() driver.save_screenshot("/Users/skpatro/sel/test.png") time.sleep(3) driver.quit()
Explanation –
We have created test_samplePytest.py
with test as prefix to file name
and also created a test method with test_sampleNav()
this above test will launch the browser with the specified url
and will click on the singup form button
and will navigate to the registration form page
To run the test
In your project terminal type
pytest -q test_samplePytest.py
Output –
(venv) userName SampleSelPython % pytest test_sampleSel.py
. [100%]
1 passed in 8.84s
We have used the parameter -q to brief the output
To know more pytest options, you can type
pytest --help
Another thing, we can have multiple test methods in side a test file.
let’s see that in action –
To share the driver object across multiple test methods, we have to create a global or static driver object, so let’s create a class and class variable and use across multiple test methods
# author - qavalidation.com # test_samplePytest.py import pytest from selenium import webdriver import time from selenium.webdriver.common.by import By class Test_sampleSel1: driver = None def test_sampleNav(self): print("1") Test_sampleSel1.driver = webdriver.Chrome("/Users/skpatro/sel/chromedriver") # driver.maximize_window() Test_sampleSel1.driver.implicitly_wait(30) Test_sampleSel1.driver.set_page_load_timeout(50) Test_sampleSel1.driver.get("https://qavbox.github.io/demo") assert "QAVBOX" in Test_sampleSel1.driver.title time.sleep(3) def test_navReg(self): Test_sampleSel1.driver.find_element(By.LINK_TEXT, "SignUp Form").click() # driver.find_element_by_link_text("SignUp Form").click() Test_sampleSel1.driver.save_screenshot("/Users/skpatro/sel/test.png") time.sleep(3) Test_sampleSel1.driver.quit()
We have created a class – Test_sampleSel1 & a class variable – driver
now this class variable we can use as classname.variable
i.e – Test_sampleSel1.driver
1st test method will launch the browser and verify the browser title
2nd test method will continue the driver instance, click on the sign up button and take a screenshot
to run the test –
pytest -q test_samplePytest.py
Hope this helps!