Metadata-Version: 2.1
Name: pytest-automock
Version: 0.1.1
Summary: Jticker core
Home-page: https://github.com/pohmelie/pytest-automock
License: UNKNOWN
Platform: UNKNOWN
Requires-Python: >= 3.7
Description-Content-Type: text/markdown
Provides-Extra: dev
Requires-Dist: pytest ; extra == 'dev'
Requires-Dist: pytest-cov ; extra == 'dev'
Requires-Dist: pytest-asyncio ; extra == 'dev'
Requires-Dist: flake8 ; extra == 'dev'

# pytest-automock
![Travis status for master branch](https://travis-ci.com/pohmelie/pytest-automock.svg?branch=master)
![Codecov coverage for master branch](https://codecov.io/gh/pohmelie/pytest-automock/branch/master/graph/badge.svg)
![Pypi version](https://img.shields.io/pypi/v/pytest-automock.svg)
![Pypi downloads count](https://img.shields.io/pypi/dm/pytest-automock)

Automock fixtures for pytest.

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

Lets say you have some module `mymod.py`:
``` python
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`:
``` python
import pytest
import mymod

@pytest.fixture(autouse=True)
def _mocks(automock):
    with automock((mymod, "T")):
        yield
```
`test_t.py`:
``` python
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:
``` bash
$ 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`
``` bash
$ 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).
``` bash
$ 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.


