Skip to main content

Jticker core

Project description

pytest-automock

Travis status for master branch Codecov coverage for master branch Pypi version Pypi downloads count

Automock fixtures for pytest.

automock

Autogenerated method mocks for objects. Supports both sync/async methods.

Lets say you have some module mymod.py:

import time

class T:

    def do_job(self, x, y):
        s = x + y
        time.sleep(s)
        return s

And you want to create mocks for your tests, but you are too lazy to write them... conftest.py:

import pytest
import mymod

@pytest.fixture(autouse=True)
def _mocks(automock):
    with automock((mymod, "T")):
        yield

test_t.py:

import mymod

def test_job():
    t = mymod.T()
    assert t.do_job(1, 2) == 3
    assert t.do_job(2, 3) == 5

If you run pytest on this setup, then you will see fail:

$ pytest -x
...
E           RuntimeError: Mock is locked, but '__init__' wanted

automock can work in two modes: locked and unlocked. Locked mode is default, real methods calls of mocked objects are not allowed in this mode. So, above error says that we can't call __init__ of our T. In locked mode there are no mock-files updates also.

To allow real calls and mocks generation automock provides extra cli argument to pytest call --automock-unlocked

$ pytest -x --automock-unlocked
...
test_t.py .
...
1 passed in 8.08s

After that you can see that tests/mocks/test_job/T file was created. This is mock for your test sequence. Now you can rerun tests and see what happens (you can omit --automock-unlocked key for ensurance, that real object will not be touched).

$ pytest -x
...
test_t.py .
1 passed in 0.06s

API

automocker(*pairs, storage="tests/mocks", unlocked=None)

  • pairs: pair/tuple of object/module and attribute name (str)
  • storage (str or Path): root path for storing mocks
  • unlocked (bool): mode selector (if omited, selected by --automock-unlocked)

Pros

  • Easy to use
  • Speed up tests
  • Mock any object
  • Mock functions

Cons

  • No support for dunder methods (can be partly solved)
  • No support for sync/async generators/contexts
  • No black and white lists of methods for mocking (can be solved)
  • Races will break tests, since order counts

automock_unlocked

Fixture with default mode from cli parameter.

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-automock-0.1.1.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

pytest_automock-0.1.1-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file pytest-automock-0.1.1.tar.gz.

File metadata

  • Download URL: pytest-automock-0.1.1.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.20.1 setuptools/41.2.0 requests-toolbelt/0.8.0 tqdm/4.36.1 CPython/3.7.4

File hashes

Hashes for pytest-automock-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d129fdd38a133a1635058e2b4dcb48e90a34b3aeaa88e1db898d5191f97bca51
MD5 6ef80515d457940f44c19fe2a68c8da9
BLAKE2b-256 c9aeb60baa36ad81c1c2b96ad2b7813068e2e33c4109507115628cd7b7736988

See more details on using hashes here.

File details

Details for the file pytest_automock-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pytest_automock-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.20.1 setuptools/41.2.0 requests-toolbelt/0.8.0 tqdm/4.36.1 CPython/3.7.4

File hashes

Hashes for pytest_automock-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3225a5aabd282061a42933bbb342df6e2a6853f27b15ec7d2e887dd9f1134b30
MD5 608e30cffdb5b220b051aa4757d880cc
BLAKE2b-256 47937062dc69678729654c95ae372e7ab29fa6a8660fd138428177d4bfae84f1

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