Fake objects designed with OOP in mind.
Project description
Primitives
Fake objects designed with OOP in mind.
Documentation | Source Code | Task Tracker
Mock objects makes your tests worst. Usage of mock objects is considered an anti-pattern by many experienced developers. Mock objects blindly respond to any interaction. Patch function is able to put such objects in any place in your code. It does not matter if that code was written in a way to be configured or not. This situation has several consequences.
First of all, your tests start making assumptions about implementation of tested code. This creates high coupling between tests and code. You no more could easily change your code because 25 tests are aware of the name of the function in the middle of the call stack.
The second unpleasant details about mocks is its fragile blind trust to the client code. Writing mocks of proper quality is extremely complicated. You need a ton of assert statements at the end of the test to check that only expected methods were called. In addition API of the mock library in python is an ugly procedural code. It requires a 3 lines just to define a dumb method returning predefined value on mock. This harms readability of tests dramatically.
I was upset with mock library for the long time. I decided to design a collection of strict composable objects without ability to put them at random place in code. Here is what I came with!
Pros
- Fake objects with strict behavior will highlight problems in your code earlier
- Nice composable API makes definition of complex objects short and concrete
- Force user to use composition instead of patch
Example
The primitives
library gives you a collection of objects with ability to
define expected behavior as set of short expressions. For example, you could
define a function returning None
like this:
>>> from primitives import Instance, Callable, Argument
>>> func = Callable()
>>> func()
Let's try to test a function below using primitives
fake objects and standard
unittest.mock
library for comparison.
>>> def greet_many(repo):
... for user in repo.users():
... print(user.greet('Hello'))
>>> greet_many(Instance(users=Callable([
... Instance(greet=Callable('Hello, John', Argument('Hello'))),
... Instance(greet=Callable('Hello, Kate', Argument('Hello'))),
... ])))
Hello, John
Hello, Kate
We would leave unittest.mock
implementation to the reader as a homework.
Questions
If you have any questions, feel free to create an issue in our Task Tracker. We have the question label exactly for this purpose.
Enterprise support
If you have an issue with any version of the library, you can apply for a paid enterprise support contract. This will guarantee you that no breaking changes will happen to you. No matter how old version you're using at the moment. All necessary features and bug fixes will be backported in a way that serves your needs.
Please contact proofit404@gmail.com if you're interested in it.
License
primitives
library is offered under the two clause BSD license.
— ⭐ —
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file primitives-1.8.0.tar.gz
.
File metadata
- Download URL: primitives-1.8.0.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.10.4 Linux/5.13.0-1023-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 78d4fc8fd5ffa196df136441255c0aa4a8e4c1458a9b970752c20af6ea17e601 |
|
MD5 | abdc1c85334161516125ba7edca1ab9c |
|
BLAKE2b-256 | c61a55abf0eacc19e922eb03a64cbb91513cf58c02bcd5b94216a8e3803589d2 |
File details
Details for the file primitives-1.8.0-py2.py3-none-any.whl
.
File metadata
- Download URL: primitives-1.8.0-py2.py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.10.4 Linux/5.13.0-1023-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d06d78e867c89b5e2159052cc02118b0d4f40ba458fe115c895582d98d8702f8 |
|
MD5 | d6576885e317e110a8667a8e109d30f5 |
|
BLAKE2b-256 | ded175f9844f336de914d4b8e4004fad3d184624bbf223146a28cbd8a25a8cec |