Skip to main content

pytest plugin for comparing call arguments.

Project description

pytest-compare

PyPI Latest Release License Documentation DOI Downloads Code style: black

What is it?

The pytest-compare helps validate method call arguments when testing python code.

pytest-compare is designed to work with assert methods. While python native variables can be easily compared, a more complicated structures sometimes do not. For example validating a pd.DataFrame will raise an exception. This is where pytest-compare comes in. It allows this kind of structures to be easily compared.

How to install

To install pytest-compare from PyPi, run the command:

pip install pytest-compare

Optional dependencies

Some compare modules may require additional packages to be installed.

Pandas

To compare pandas module, add the pandas option to the installation:

pip install pytest-compare[pandas]

How to use

The comparation modules are design to be used with assert methods. When validating patched method call arguments

df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})

with patch.object(ProductionClass, 'method', return_value=None) as mock_method:
    thing = ProductionClass()
    thing.method(df)
# will raise an exception
mock_method.assert_called_once_with(df)

# the correct way
mock_method.assert_called_once_with(CompareDataFrame(df))

Multiple arguments in a call

When a method is called using multiple arguments, all of them must be addressed in the test. while python native variables can be easily compared, pytest-compare is designed to compare a more complicated structures and do custom compares.

with patch.object(ProductionClass, 'method', return_value=None) as mock_method:
    thing = ProductionClass()
    thing.method(1, "str", df1, df2)
    
# the correct way
mock_method.assert_called_once_with(1, "str", CompareDataFrame(df1), CompareDataFrame(df2))

Args and Kwargs

When validating the call, the expected values must be passed in the exact same mix of args and kwargs as when they were called.

with patch.object(ProductionClass, 'method', return_value=None) as mock_method:
    thing = ProductionClass()
    thing.method(df1, dataframe=df2)
    
# the correct way
mock_method.assert_called_once_with(CompareDataFrame(df1), dataframe=CompareDataFrame(df2))

Actual and Expected convention

  • actual: The values that the method was originally called with.
  • expected: Test values to see if the method was called with.

For example here, arg_actual is the actual value while arg_expected is the expected value.

mock_method = Mock()

mock_method(arg_actual)
mock_method.assert_called_once_with(arg_expected)

If arg_actual is not equal to arg_expected, an exception will be raised.

Development

Setup

Virtual Environment

Create a virtual environment and install the dependencies:

$ python3 -m venv venv
$ source venv/bin/activate
$ pip install -e ".[pandas]" -r requirements-dev.txt -r requirements-quality.txt

Pre-commit Hooks

Start by installing the pre-commit hooks which will run black, mypy, flake8, and codespell on every commit.:

$ hooks/autohook.sh install

Creating tests

Running tests

To test the code, run the following command:

$ pytest

Writing tests

Tests are written using the pytest framework. To create a test for a new Compare module, create two files in the tests directory: conftest.py and the test file witch must start with test_. The conftest.py file must implement the following fixtures:

@pytest.fixture
def actual_call_args() -> Tuple[Any]:
    raise NotImplementedError("`actual_call_args` must be implemented")


@pytest.fixture
def actual_call_kwargs() -> Dict[str, Any]:
    raise NotImplementedError("`actual_call_kwargs` must be implemented")


@pytest.fixture
def expected_call_args() -> Tuple[Any]:
    raise NotImplementedError("`expected_call_args` must be implemented")


@pytest.fixture
def expected_call_kwargs() -> Dict[str, CompareBase]:
    raise NotImplementedError("`expected_call_kwargs` must be implemented")

The test file must implement a test class that inherits from BaseTest.

That way two base tests will be run for each Compare module that wil test the args and kwargs of the method call.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_compare-0.2.4.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_compare-0.2.4-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file pytest_compare-0.2.4.tar.gz.

File metadata

  • Download URL: pytest_compare-0.2.4.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for pytest_compare-0.2.4.tar.gz
Algorithm Hash digest
SHA256 a7ac957d71d0723cee1acf2735f6d9c429796fbb384cb2ccbdeb3bfddd87e88d
MD5 25db2ead320c63dfe687c8de3515d68f
BLAKE2b-256 e7fe1a2af887c5e1767afc8ea41baa12e227affe76b7865568b761c601e47b9b

See more details on using hashes here.

File details

Details for the file pytest_compare-0.2.4-py3-none-any.whl.

File metadata

  • Download URL: pytest_compare-0.2.4-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for pytest_compare-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e5201bc3e52e14617bac5446ae0829763e3c36c29f913c3cb4901c4208666d31
MD5 abd697e6c3bccd348ce7061f7b26cf1d
BLAKE2b-256 15ceb578877f6ead7a193f316ed2e963f700aec776392785658b5e2b369e1dd1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page