Skip to main content

Fake objects designed with OOP in mind.

Project description

Primitives build pypi

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.

— ⭐ —

Download files

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

Source Distribution

primitives-1.8.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

primitives-1.8.0-py2.py3-none-any.whl (7.2 kB view details)

Uploaded Python 2 Python 3

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

Hashes for primitives-1.8.0.tar.gz
Algorithm Hash digest
SHA256 78d4fc8fd5ffa196df136441255c0aa4a8e4c1458a9b970752c20af6ea17e601
MD5 abdc1c85334161516125ba7edca1ab9c
BLAKE2b-256 c61a55abf0eacc19e922eb03a64cbb91513cf58c02bcd5b94216a8e3803589d2

See more details on using hashes here.

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

Hashes for primitives-1.8.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d06d78e867c89b5e2159052cc02118b0d4f40ba458fe115c895582d98d8702f8
MD5 d6576885e317e110a8667a8e109d30f5
BLAKE2b-256 ded175f9844f336de914d4b8e4004fad3d184624bbf223146a28cbd8a25a8cec

See more details on using hashes here.

Supported by

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