pytest - step by step tutorial for beginners

Pytest is a testing framework based on Python. Python is very flexible to test any kind of services like web, database, API, cloud, etc. This tutorial gives a basic idea about pytest, write test scripts and integration with other services. This tutorial intended for beginners and they will get the basic idea to start the high-level tasks. This package contains 5 steps and try to complete it in a series manner. Selenium integration also included in the tutorial. Happy learning !!!


Step 1 - pytest installation

Step 2 - Create the first pytest script

Step 3 - pytest rules and features

Step 4 - Creating the first script using Selenium

Step 5 - Validating the first test 

Selenium with Pytest - Validating the first test

In the previous post, you've created the first script. Let's see how we can validate a test. Please see below test case to automate it

1. Launch the https://www.testodev.com/
2. Make sure that testodev loaded successfully
3. Ensure that "Privacy Policy" present in the header

Using selenium, we can identify the web objects using different method such as id, xpath, classname etc. We can use any convenient method. As mentioned in the pytest rules, all test must have an assert to validate the output. The script is given below
import pytest
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('start-maximized')
driver = webdriver.Chrome(
    options=options, executable_path=r".\\chromedriver\\chromedriver.exe")
driver.get("https://www.testodev.com/")

#Test 1 - Validating the testodev launch
def test_ValidateTestodev():
    assert "https://www.testodev.com/" in driver.current_url, "testodev failed to load"

#Test 2 - Validating the privacy policy text
def test_ValidatePrivacyPolicy():
    txtValidate = driver.find_element_by_class_name(
        'privacy').get_attribute("innerHTML")
    assert txtValidate == "Privacy Policy", "Privacy Policy text validation failed"

Creating the first script using Selenium with Pytest

Selenium is one of the topmost tool used for web automation. You can download the selenium package and interact with the applications using web drivers. Selenium package available here and can be download using pip command

pip install selenium  

Selenium requires a driver to interface with the chosen browser. Chrome, for example, requires chromedriver, which needs to be installed before the below examples can be run. Make sure it’s in your PATH

Yes, you've completed the required installation. Now we can write a simple script to launch the testodev.
import pytest
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('start-maximized')
driver = webdriver.Chrome(
    options=options, executable_path=r".\\chromedriver\\chromedriver.exe")
driver.get("https://www.testodev.com/")
You run the script via command line using the command pytest <script name> 
The script launches the testodev via chromedriver. 

Pytest - Rules and Features

In the previous post, we've learned about creating the first script using pytest framework. This post will discuss the rules and features

1. Header - All pytest script should include import pytest at header of the script 
2. File naming - pytest only identifies the file names starting with test_ or ending with _test as the test files
3. Method naming - All pytest methods should start with test_
4. Assertions in PyTest - Assertions are used for validating the test conditions. Assertions are checks that return either True or False status. if an assertion fails in a test method, then that method execution is stopped there. The remaining code in that test method is not executed, and pytest will continue with the next test method.
assert "tree" == "tree" is a successful assertion.
assert 10==3 is a assertion failure
assert True is a successful assertion
assert False is an assertion failure.
assert "orange" == "apple" is a assertion failure.
These are basic rules to validate the application with pytest framework. pytest also supports parallel execution, annotation etc, which testodev will cover in the later posts 

Create the first script using pytest

Hope you're installed pytest package as mentioned in the post. In this post, we're going to create a sample pytest.

As a first step, create a file named test_first.py and copy the below code snippet to it
import pytest
def test_test1():
 a=5
 b=5
 assert x == y,"test failed"
def test_test2():
 x="Cat"
 y="Lion"
 assert x == y,"test failed"
As a second step, using command prompt navigate to the script path and execute below command to run the script
pytest -vv test_first.py -ss
Yes!!!, you've executed the first script and its results are displayed.