question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow use of custom mock implementations

See original GitHub issue

With the growing use of asyncio, we need ways to mock Coroutines.

Currently, mock and unittest.mock do not support creating specs for coroutines (AsyncMock is being added in python 3.8).

As a work around, we’re using asynctest.mock, which has the same interface as unittest.mock, however this requires us to completely re-implement the __init__ method of MockFixture, and removes the “hide traceback” functionality (though we don’t care too much about this). This allows us to have both mocker and async_mocker fixtures.

TLDR To ease this usecase (and potentially others), Can support for defining a mock modules path from the config be added please?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
antdkingcommented, Jun 28, 2019

agree with your points on this. I think it’s probably best to wait until 3.8 for this library.

In terms of creating a new plugin, it might be better to try and get the fixtures into the asynctest library instead. Will close this issue for now.

The hack we have in place works well enough for us until that time, and for our specific usecase, it will be an easy migration.

If anyone else stumbles on this issue, here is the hack:

import pytest

import pytest_mock
import asynctest.mock


@pytest.fixture
def async_mocker(pytestconfig):
    # This is a straight copy + paste from pytest_mock, but with our patched MockFixture
    result = AsyncMockFixture(pytestconfig)
    yield result
    result.stopall()


class AsyncMockFixture(pytest_mock.MockFixture):
    def __init__(self, config):
        # This is a straight copy + paste from pytest_mock
        # TODO: contribute a way to use arbitary mock libraries upstream

        self._patches = []  # list of mock._patch objects
        self._mocks = []  # list of MagicMock objects

        # CHANGED: hard coding the asynctest.mock
        self.mock_module = mock_module = asynctest.mock

        self.patch = self._Patcher(self._patches, self._mocks, mock_module)
        # aliases for convenience
        self.Mock = mock_module.Mock
        self.MagicMock = mock_module.MagicMock
        self.NonCallableMock = mock_module.NonCallableMock
        self.PropertyMock = mock_module.PropertyMock
        self.call = mock_module.call
        self.ANY = mock_module.ANY
        self.DEFAULT = mock_module.DEFAULT
        self.create_autospec = mock_module.create_autospec
        self.sentinel = mock_module.sentinel
        self.mock_open = mock_module.mock_open

        # CoroutineMock is from asynctest
        # AsyncMock is being added in python 3.8
        # Please use AsyncMock.
        self.CoroutineMock = self.AsyncMock = mock_module.CoroutineMock
2reactions
tirkarthicommented, Jul 26, 2020

FWIW, AsyncMock is now available as part of mocker from https://github.com/pytest-dev/pytest-mock/commit/449d3d038e076fcdbb860815d306d4a59db44141 with rich assert diff for assert helpers associated with AsyncMock

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mock Functions - Jest
Mock functions allow you to test the links between code by erasing the actual implementation of a function, capturing calls to the function ......
Read more >
How to change mock implementation on a per single test basis?
Use mockFn.mockImplementation(fn). import { funcToMock } from './somewhere'; jest.mock('./somewhere'); beforeEach(() => { funcToMock.
Read more >
How to Mock a Single Function from a Module with Jest
Create a mock for the entire module. Tell that mock to use actual (non-mocked) implementations for everything. Mock the desired function of the ......
Read more >
Stubbing and Mocking with Mockito and JUnit - Semaphore CI
This unit test is based on the doAnswer/when directives. Mockito allows us to override the answer to any method by implementing the Answer ......
Read more >
Mocking different values for the same module using Jest
Learn how to use Jest mocking utilities to setup tests with different mock values. ... They allow you to isolate the code under...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found