diff_decorator
is a Python package that provides a decorator for unittests, enhancing the visualization of differences when assertions fail. It allows easy comparison of expected and actual results using a diff tool of your choice.
pip install diff_decorator
To use diff_decorator
, import the diff_with
decorator in your test files and apply it to your test methods.
from diff_decorator import diff_with
import unittest
class MyTestCase(unittest.TestCase):
@diff_with
def test_example(self):
expected = "expected string"
actual = "actual string"
self.assertEqual(expected, actual)
By default, diff_with
uses diff
as the diff tool. That could be customized by setting the DIFF_DECORATOR_TOOL
environment variable or by specifying a tool in the decorator (which has higher precedence).
To modify the default diff tool set the DIFF_DECORATOR_TOOL
environment variable to the preferred diff tool:
export DIFF_DECORATOR_TOOL=meld
@diff_with(tool='meld')
def test_example_with_custom_tool(self):
# ...
- The decorator only runs when the test shows differences (e.g., if it fails).
- Python 3.x
- External diff tool (e.g., vimdiff, meld, diff)
For development, clone the repository and install the package using pip:
git clone https://github.com/weshouman/diff_decorator.git
cd diff_decorator
pip install .
To publish the project
pip install setuptools
python setup.py sdist bdist_wheel
pip install twine
twine upload dist/*
This project is licensed under the MIT License while being prohibited for use in anything that is Haram.