---
title: Visual Regression Testing with Python | Testing Resources
description: 'How to add visual regression testing to a Python suite: creating a baseline,
  comparing against it, and keeping screenshots stable over time.'
source_url:
  html: https://testingbot.com/resources/articles/python-visual-testing
  md: https://testingbot.com/resources/articles/python-visual-testing.md
---

![Visual Regression Testing with Python](https://testingbot.com/assets/resources/articles/20.webp)

# Python Visual UI Testing

How to add visual regression testing to a Python suite: creating a baseline, comparing against it, and keeping screenshots stable over time.

By [Jochen D.](https://testingbot.com/about/jochen-d)2023-03-21Updated 2026-08-31

 Share on Facebook 

 Share on Twitter 

 Post on Reddit 

 Share link 

Visual regression testing is a specific type of regression testing, which verifies the aesthetic accuracy of what end-users of the product see.

When you deploy new code, you'll want to make sure your website still looks the same. Most of the time, you would compare two screenshots.

The first screenshot should have been created as a baseline. A baseline is a state of what you want your UI to look like.

Once you run the regression test, you will take another screenshot of the same UI and detect if there are any visual differences.

By using a combination of Python tests and the power of TestingBot's browser and device grid, you can make sure that your UI looks perfect across a large combination of end user devices.

## Table of Contents

- [Why should I use Automated Visual Testing?](https://testingbot.com#why)
- [Why use Python to do Visual Regression Testing?](https://testingbot.com#python)
- [Create your first Python Visual Test](https://testingbot.com#example)
- [Best practices when creating visual regression testcases](https://testingbot.com#best)

## Why should I use Automated Visual Testing?

Using Visual Testing as part of your automated test suite is a good idea, for both website based testing as well as mobile app testing.

Here are some of the advantages of using automated UI testing over manual testing:

- **Improved accuracy and test coverage** : 

Automation tests are more accurate than manual tests. Automation will consistently check each part of the UI, every single time.   
 Manual testers might miss visual defects, due to human error, fatigue or any other number of factors.

- **Improved repeatability** : 

When integrated in a CI/CD system, you can run these automated UI tests at a consistent interval. For example after every commit or deploy of new code.

- **Increased speed** : 

It might take time to set up, configure and program your visual test cases in Python. But once you have these in place, they will prove to be more efficient and faster than going through the UI with a manual test.

- **Pixel perfect** : 

Because the UI is compared by an algorithm, it will make sure your UI looks perfect. The automated test will ensure each pixel looks good, by using a visual diff algorithm.

## Why use Python to do Visual Regression Testing?

[Python](https://www.python.org/) is a very popular programming language among many web and mobile app developers. It has grown in popularity in recent years because of its readability and simplicity to create high performance code. Python is cross platform, which means it can be used on Windows, Linux, macOS and other operating systems.

One popular Python test framework is called PyTest, which has plugins that you can install. One PyTest plugin is called [pytest-needle](https://github.com/jlane9/pytest-needle) which allows you to take and compare screenshots from a remote browser, in a PyTest script.

You can use a [Selenium](https://testingbot.com/support/web-automate/selenium), [Playwright](https://testingbot.com/support/web-automate/playwright) or Puppeteer driver together with pytest-needle to take a screenshot. The plugin will then use Needle's pixel comparison to go over all pixels of both the base image and the screenshot to detect any differences.

## Create your first Python Visual Test

To get started, you can choose to either use [PyTest with the needle plugin](https://docs.pytest.org/) or use [Needle](https://github.com/python-needle/needle) together with Nose.

You can then write your first test script, where you will check if the screenshot you take from TestingBot is exactly the same as your golden image.

To use Needle with PyTest, you will need to install these packages

```bash
pip install selenium pytest pytest-needle
```

```python
@pytest.mark.element
def test_example_testingbot(needle):
    needle.driver.get('https://testingbot.com')

    # Take an element screen diff
    needle.assert_screenshot('html', (By.Name, 'html'))
```

You can then run the test on a TestingBot browser, thanks to the [built-in support for TestingBot](https://pytest-selenium.readthedocs.io/en/latest/user_guide.html#testingbot):

```bash
pytest --driver TestingBot --capability browserName chrome --capability version latest --capability platform WIN10
```

To use Needle with Nose, you will need to install these packages

```bash
pip install selenium nose needle
```

```python
from needle.cases import NeedleTestCase
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

class TestingBotTest(NeedleTestCase):
    @classmethod
    def get_web_driver(cls):
        options = Options()
        options.browser_version = "latest"
        options.platform_name = "WIN10"
        return NeedleRemote(webdriver.Remote(
            command_executor='https://key:secret@hub.testingbot.com/wd/hub',
            options=options))

    def test_page(self):
        self.driver.get('https://testingbot.com')
        self.assertScreenshot('html', 'html')
```

You can then run the test with Nose:

```bash
nosetests test_testingbot.py --with-save-baseline
```

## Best practices when creating visual regression testcases

Here are some tips to make sure you make the most efficient use of Visual Automated Testing.

1. **Compare components instead of pages** :   

It is faster and more efficient to compare multiple DOM components, instead of entire viewports/pages.   
 This is a more modular approach, with clearer error messages. If a test fails, you'll know exactly which component is failing.

2. **Integrate visual testing with other types of testing** :   

Visual testing should be used in combination with other types of tests, such as functional tests. Depending on pixel diff algorithms alone is not a very good test strategy.

You should make sure you have clear functional tests implemented as well. These test the behaviour of your product, instead of the visual aspect.

## Related reading

- [Visual testing with Playwright](https://testingbot.com/resources/articles/playwright-visual-regression-testing)
- [Visual regression testing an Angular app with Cypress](https://testingbot.com/resources/articles/angular-ui-testing)

Topics [Visual Testing](https://testingbot.com/resources/articles/topic/visual-testing) 

## Sidebar

### TestingBot Cloud Testing

Run automated, manual and visual tests on remote browsers and devices. Sign up for a free trial.

[Free Trial](https://testingbot.com/users/sign_up)

### Latest articles

[![Angular UI Testing](https://testingbot.com/assets/resources/articles/21-15af461a46c449ef26dfb952ed9d0fc327c97d9ca815f24f8fe9ee60b9aa64a5.webp)](https://testingbot.com/resources/articles/angular-ui-testing)

#### [Angular UI Testing](https://testingbot.com/resources/articles/angular-ui-testing)

How to add visual regression testing to an Angular app wi...

[Read article →](https://testingbot.com/resources/articles/angular-ui-testing)

[![Visual Testing with Playwright](https://testingbot.com/assets/resources/articles/14-f3f2e8954c8a47cf0bb29059b44f4d2f40998442e22db6f8acdccb8e5b34d8c1.webp)](https://testingbot.com/resources/articles/playwright-visual-regression-testing)

#### [Visual Testing with Playwright](https://testingbot.com/resources/articles/playwright-visual-regression-testing)

Playwright provides automated browser testing

[Read article →](https://testingbot.com/resources/articles/playwright-visual-regression-testing)

[![Parallel Testing: What Actually Scales](https://testingbot.com/assets/resources/articles/parallel-testing-32f51791d748c85621c4bf431293e46349cf0d81ec1a3262450b33f0edead4ef.webp)](https://testingbot.com/resources/articles/parallel-testing)

#### [Parallel Testing: What Actually Scales](https://testingbot.com/resources/articles/parallel-testing)

Why adding workers stops helping, how to shard so they ar...

[Read article →](https://testingbot.com/resources/articles/parallel-testing)

## Other Articles

[![Automated Regression Testing in CI](https://testingbot.com/assets/resources/articles/automated-regression-testing-e283d0b6a88c787633d29f0cfb35cbb90a48136dace54720252843ebc4a36063.webp)](https://testingbot.com/resources/articles/automated-regression-testing "Automated Regression Testing in CI")

### [Automated Regression Testing in CI](https://testingbot.com/resources/articles/automated-regression-testing)

How to build a regression suite that stays fast and trustworthy in CI, what to leave out of it, and when to run which part.

[Read article →](https://testingbot.com/resources/articles/automated-regression-testing)

[![Mobile App Test Automation at Scale](https://testingbot.com/assets/resources/articles/mobile-app-test-automation-12d7e7edc7c551f05282cd03ccd0de42da8e4c740d754286777972249471a36e.webp)](https://testingbot.com/resources/articles/mobile-app-test-automation "Mobile App Test Automation at Scale")

### [Mobile App Test Automation at Scale](https://testingbot.com/resources/articles/mobile-app-test-automation)

How to choose between Appium, Espresso, XCUITest and Maestro, and what changes when you run the suite on real devices at scale.

[Read article →](https://testingbot.com/resources/articles/mobile-app-test-automation)

[![AI in Software Testing: What Works](https://testingbot.com/assets/resources/articles/ai-in-software-testing-ab8885a15af9f64731cdb97574153660eb0f409067995f0a089094cac76431e2.webp)](https://testingbot.com/resources/articles/ai-in-software-testing "AI in Software Testing: What Works")

### [AI in Software Testing: What Works](https://testingbot.com/resources/articles/ai-in-software-testing)

A category-by-category look at where AI genuinely helps a test suite, where it does not, and how to evaluate a tool before you buy it.

[Read article →](https://testingbot.com/resources/articles/ai-in-software-testing)

[![Playwright MCP for Test Automation](https://testingbot.com/assets/resources/articles/playwright-mcp-da3126b2a5a5b0fe528ad6311ccb79e7a2943f7a3b637a5ae5e748087a59f39a.webp)](https://testingbot.com/resources/articles/playwright-mcp "Playwright MCP for Test Automation")

### [Playwright MCP for Test Automation](https://testingbot.com/resources/articles/playwright-mcp)

What Playwright MCP does, how driving the accessibility tree differs from screenshot agents, and where it fits in a real test workflow.

[Read article →](https://testingbot.com/resources/articles/playwright-mcp)

## Ready to start testing?
[Start a free trial](https://testingbot.com/users/sign_up)
