How to Capture Screenshot on Test Case Failure With Pytest?

2 minutes read

To capture a screenshot on test case failure with pytest, you can utilize the pytest-screenshot plugin. This plugin allows you to take screenshots automatically whenever a test case fails. Simply install the pytest-screenshot plugin using pip, and then add a couple of lines of code to your pytest configuration file. Make sure to specify the folder where you want the screenshots to be saved. This way, whenever a test case fails, the plugin will automatically capture a screenshot and store it in the specified folder for further analysis.


What tool can be used to capture screenshots in pytest?

The "pytest-screenshot" plugin can be used to capture screenshots in Pytest. This plugin allows you to automatically capture screenshots of failing tests during test execution.


What is the purpose of capturing a screenshot on test case failure with pytest?

Capturing a screenshot on test case failure with pytest helps in providing additional information about the state of the application at the time of the failure. It can help in debugging and identifying the root cause of the failure by visually showing what the application was displaying when the test case failed. The screenshot can also be used for documenting and reporting the issue to the development team.


How to capture screenshots using pytest hooks on test case failure?

To capture screenshots using pytest hooks on test case failure, you can use the pytest fixture request to access test session information and capture a screenshot when a test case fails. Here is a step-by-step guide on how to achieve this:


Step 1: Create a custom pytest fixture to capture screenshots on test failure

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# conftest.py

import pytest
from selenium import webdriver

@pytest.fixture(scope='function')
def driver(request):
    options = webdriver.ChromeOptions()
    options.add_argument("--start-maximized")
    
    driver = webdriver.Chrome(options=options)
    
    yield driver
    
    # Perform cleanup actions
    if request.node.rep_call.failed:
        driver.save_screenshot(f"failure_{request.node.name}.png")
    
    driver.quit()


Step 2: Use the custom fixture in your test cases

1
2
3
4
5
6
7
8
9
# test_example.py

def test_example(driver):
    driver.get("https://www.example.com")
    assert driver.title == "Example Domain"

def test_another_example(driver):
    driver.get("https://www.example.com")
    assert driver.title == "Wrong Title"


Step 3: Run your test cases using pytest


When you run your test cases using pytest, the custom fixture will capture screenshots when a test case fails. The screenshots will be saved in the same directory where the test script is located with the format failure_test_name.png.


That's it! You have now successfully implemented capturing screenshots using pytest hooks on test case failure.

Facebook Twitter LinkedIn Telegram Whatsapp

Related Posts:

In pytest, you can apply multiple tags to a test case by using the pytest.mark decorator along with the pytest.mark. syntax. You can define multiple tags for a test case by simply adding multiple pytest.mark.tagname decorators above the test function. For exam...
To count test cases written with pytest, you can use the following command in the terminal: pytest --collect-only | grep "collected" This command will display the number of test cases collected by pytest while running the test suite. It will give you a...
To create an HTML report for pytest, you can use the pytest-html plugin. This plugin generates a detailed HTML report of your test results, including test failures, errors, and statistics.To use pytest-html, you first need to install the plugin using pip: pip ...
To raise an exception in pytest, you can use the pytest.raises context manager. This context manager allows you to check that a specific exception is raised during the execution of a test.Here is an example of how to raise an exception in pytest using the pyte...
To run a script as a pytest test, you need to create a Python file containing your test functions with names starting with "test_" and then use the pytest command in your terminal to execute the file. Pytest will automatically discover and run any func...