Skip to main content
PyPI version Python versions See Build Status on Travis CI See Build Status on AppVeyor

A simple plugin to use with pytest


This pytest plugin was generated with Cookiecutter along with @hackebrot’s cookiecutter-pytest-plugin template.

Features

  • Readable diffs for failed tests

  • Customizable diffs for your classes

Requirements

  • pytest

  • deepdiff

  • pprintpp

Installation

You can install “pytest-diff” via pip from PyPI:

$ pip install pytest-diff

Usage

class Person:
    def __init__(self, name, age, favorites):
        self.name = name
        self.age = age
        self.favorites = favorites


def test_person():
    a = Person("Alice", age=21, favorites={"food": "spam", "movie": "Life of Brian"})
    b = Person("Alice", age=21, favorites={"food": "eggs", "movie": "Life of Brian"})
    assert a == b

Running pytest gives:

______________________________ test_person ______________________________

    def test_person():
        a = Person("Alice", age=21, favorites={'food': 'spam', 'movie': 'Life of Brian'})
        b = Person("Alice", age=21, favorites={'food': 'eggs', 'movie': 'Life of Brian'})
>       assert a == b
E       assert
E         <test_person.Person object at 0x7fa326d769e8>
E         ==
E         <test_person.Person object at 0x7fa326d76be0>
E         {'values_changed': {"root.favorites['food']": {'new_value': 'spam', 'old_value': 'eggs'}}}

examples/test_person.py:11: AssertionError

To customize diffs for a specific type, register it with the registry:

import pytest_diff


class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year


@pytest_diff.registry.register(Car)
def diff(x, y):
    return [
               f"{x.make} vs {y.make}",
               f"{x.model} vs {y.model}",
               f"{x.year} vs {y.year}",
           ]


def test_car():
    c1 = Car("Toyota", "Prius", 2010)
    c2 = Car("Honda", "Accord", 2009)
    assert c1 == c2

Then running pytest shows your custom diff:

    def test_car():
        c1 = Car("Toyota", "Prius", 2010)
        c2 = Car("Honda", "Accord", 2009)
>       assert c1 == c2
E       assert
E         <test_custom.Car object at 0x7f0e9b0ccd68>
E         ==
E         <test_custom.Car object at 0x7f0e9b0cceb8>
E         Toyota vs Honda
E         Prius vs Accord
E         2010 vs 2009

examples/test_custom.py:21: AssertionError

Contributing

Contributions are very welcome. Tests can be run with tox, please ensure the coverage at least stays the same before you submit a pull request.

License

Distributed under the terms of the MIT license, “pytest-diff” is free and open source software

Issues

If you encounter any problems, please file an issue along with a detailed description.

Release files for pytest-diff 0.1.14

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-diff 0.1.14
File Size Uploaded
pytest-diff-0.1.14.tar.gz 4.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-diff 0.1.14
File Interpreter ABI Platform
pytest_diff-0.1.14-py3-none-any.whl Python 3 none any Details

Total release size: 9.7 kB

Release files / pytest-diff-0.1.14.tar.gz

Download URL pytest-diff-0.1.14.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
f1a86070fa53c2d6f29f5e242aac78df29dcb24a0ccaabb9b354d099665bc0fc
BLAKE2b-256 checksum
How to use checksums
4fb684e3e8801f28347050a5472dec57031d8beea6a389daf8291c0c40164baf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7

Release files / pytest_diff-0.1.14-py3-none-any.whl

Download URL pytest_diff-0.1.14-py3-none-any.whl
Size 4.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bbed16b05f5a73d19575f293d6777cbd2b1de7e59df5e8a933574177bdd0552b
BLAKE2b-256 checksum
How to use checksums
d9c46f482146c5ae702a404bff24d6ed340fa63a039960aa867bc98a94e5b191
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7

Release history Release notifications | RSS feed

This release

0.1.14 This release

2 release files

0.1.13

2 release files

0.1.12

2 release files

0.1.11

2 release files

0.1.10

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

1 release file

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page