Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/samuelcolvin/dirty-equals
Doing dirty (but extremely useful) things with equals.
https://github.com/samuelcolvin/dirty-equals
pytest python testing-tools unit-testing
Last synced: 21 days ago
JSON representation
Doing dirty (but extremely useful) things with equals.
- Host: GitHub
- URL: https://github.com/samuelcolvin/dirty-equals
- Owner: samuelcolvin
- License: mit
- Created: 2022-01-26T16:10:05.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-09-14T09:43:14.000Z (about 2 months ago)
- Last Synced: 2024-10-07T18:01:42.875Z (about 1 month ago)
- Topics: pytest, python, testing-tools, unit-testing
- Language: Python
- Homepage: https://dirty-equals.helpmanual.io
- Size: 1.44 MB
- Stars: 797
- Watchers: 13
- Forks: 36
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- best-of-python - GitHub - 44% open · ⏱️ 15.11.2023): (Data Validation)
- awesome-python-testing - dirty-equals - A python library that (mis)uses the `__eq__` method to make python code (generally unit tests) more declarative and therefore easier to read and write. (Assertions)
README
Doing dirty (but extremely useful) things with equals.
---
**Documentation**: [dirty-equals.helpmanual.io](https://dirty-equals.helpmanual.io)
**Source Code**: [github.com/samuelcolvin/dirty-equals](https://github.com/samuelcolvin/dirty-equals)
---
**dirty-equals** is a python library that (mis)uses the `__eq__` method to make python code (generally unit tests)
more declarative and therefore easier to read and write.*dirty-equals* can be used in whatever context you like, but it comes into its own when writing unit tests for
applications where you're commonly checking the response to API calls and the contents of a database.## Usage
Here's a trivial example of what *dirty-equals* can do:
```py
from dirty_equals import IsPositiveassert 1 == IsPositive
assert -2 == IsPositive # this will fail!
```**That doesn't look very useful yet!**, but consider the following unit test code using *dirty-equals*:
```py title="More Powerful Usage"
from dirty_equals import IsJson, IsNow, IsPositiveInt, IsStr...
# user_data is a dict returned from a database or API which we want to test
assert user_data == {
# we want to check that id is a positive int
'id': IsPositiveInt,
# we know avatar_file should be a string, but we need a regex as we don't know whole value
'avatar_file': IsStr(regex=r'/[a-z0-9\-]{10}/example\.png'),
# settings_json is JSON, but it's more robust to compare the value it encodes, not strings
'settings_json': IsJson({'theme': 'dark', 'language': 'en'}),
# created_ts is datetime, we don't know the exact value, but we know it should be close to now
'created_ts': IsNow(delta=3),
}
```Without *dirty-equals*, you'd have to compare individual fields and/or modify some fields before comparison -
the test would not be declarative or as clear.*dirty-equals* can do so much more than that, for example:
* [`IsPartialDict`](https://dirty-equals.helpmanual.io/types/dict/#dirty_equals.IsPartialDict)
lets you compare a subset of a dictionary
* [`IsStrictDict`](https://dirty-equals.helpmanual.io/types/dict/#dirty_equals.IsStrictDict)
lets you confirm order in a dictionary
* [`IsList`](https://dirty-equals.helpmanual.io/types/sequence/#dirty_equals.IsList) and
[`IsTuple`](https://dirty-equals.helpmanual.io/types/sequence/#dirty_equals.IsTuple)
lets you compare partial lists and tuples, with or without order constraints
* nesting any of these types inside any others
* [`IsInstance`](https://dirty-equals.helpmanual.io/types/other/#dirty_equals.IsInstance)
lets you simply confirm the type of an object
* You can even use [boolean operators](https://dirty-equals.helpmanual.io/usage/#boolean-logic)
`|` and `&` to combine multiple conditions
* and much more...## Installation
Simply:
```bash
pip install dirty-equals
```**dirty-equals** requires **Python 3.8+**.