pytest-vtestify

Downloads

pytest-vtestify Documentation

pytest-vtestify is a pytest plugin designed for visual assertion and image comparison, utilizing Structural Similarity Index (SSIM). This package is perfect for validating UI changes or detecting regressions in web and software interfaces.

Features

  • Compare two images using SSIM.
  • Visualize the differences between images.
  • Automatically generate and save the diff image with bounding boxes.
  • Integrate seamlessly into pytest framework for automated testing workflows.

Installation

To install the package, simply run:

pip install pytest-vtestify

Usage

Basic Usage

You can integrate pytest-vtestify into your test suite by using the visual_assertion fixture. Below is an example of how to compare two images and generate a diff image.

from visual_testify import visual_assertion

def test_visual_comparison(visual_assertion):
    image1 = "path/to/expected_image.png"
    image2 = "path/to/actual_image.png"
    visual_assertion(image1, image2, threshold=0.95)

Web Automation Testing with Selenium

pytest-vtestify can also be used to perform visual regression testing in web automation projects, especially when working with dynamic web content. Here's an example using Selenium:

  1. Capture screenshots of the expected and actual web pages.
  2. Use visual_assertion to compare them.

Example using Selenium:

from selenium import webdriver
from visual_testify import VisualAssertion

def test_webpage_visual_comparison():
    driver = webdriver.Chrome()

    # Navigate to the webpage and capture a screenshot
    driver.get('https://example.com')
    driver.save_screenshot('actual_page.png')

    # Assume 'expected_page.png' is the expected screenshot
    visual_assert = VisualAssertion(threshold=1)

    visual_assert.assert_images('actual_page.png','expected_page.png')

    driver.quit()

In this example:

Selenium's webdriver.Chrome() is used to open a web page. The actual web page screenshot is compared to an expected screenshot using pytest-vtestify. The threshold value specifies how similar the images should be to pass the test.

Mobile Automation Testing with Appium

Example using Appium (Android Automation):

from appium import webdriver
from appium.options.android.uiautomator2.base import UiAutomator2Options
from appium.webdriver.common.appiumby import AppiumBy

from visual_testify import VisualAssertion

@pytest.fixture
def setup():

    options = UiAutomator2Options()

    options.app_package = "com.package"
    options.app_activity = "com.package.MainActivity"
    options.platform_name = "Android"
    options.udid = "5554"

    driver = webdriver.Remote('http://127.0.0.1:4723', options=options)
    driver.implicitly_wait(10)
    yield driver
    driver.quit()


def test_visual_comparison(setup):
    screenshot_path = 'screenshot.png'
    setup.get_screenshot_as_file(screenshot_path)
    visual_assert.assert_images('default.png', 'screenshot.png')    

Diff Image Output

When the comparison fails, a diff image will be automatically generated and saved in report/images/diff.png. This diff image contains:

  • The expected image.
  • The actual image.
  • The diff image with bounding boxes highlighting the differences.

Example using Selenium: alt text

Folder Structure

If the report/images folder doesn't exist, the plugin will create it during the test execution and place the diff image inside it.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Instructions for Using pytest-vtestify with Selenium

In the example provided for web automation testing with Selenium:

  • Selenium takes a screenshot of a web page during a test.
  • You compare the screenshot to an "expected" image using the visual_assertion fixture from pytest-vtestify.
  • The threshold parameter determines how closely the two images should match.

This approach is commonly used in visual regression testing for web automation, helping to catch unintended UI changes or regressions.

Demo

PYTEST-VTESTIFY

Note: This Webinar run in Bahasa Indonesia

-

paypal