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.

xxx() missing 1 required positional argument: 'mocker'

See original GitHub issue

I’m using pytest 5.3.0 with pytest-mock 1.12.1, and in a pytest class I want to temporarily mock a function of an object returned by some other module function. When I use test_calls_operation(mocker) outside a test class, then the mocker arg gets filled in by pytest-mock as expected. However, when I declare the test as shown below, I get test_calls_operation() missing 1 required positional argument: 'mocker'. What am I doing wrong? I sifted through the documentation and especially StackOverflow, but I seem to miss the knack of getting the mocker arg` automatically set as needed.

class KeysModuleTests(TestCase):
    def test_calls_operation(self, mocker):
        keys = mnt.keys.end()
        mocker.spy(keys, 'op_end')
        # ...

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

17reactions
nicoddemuscommented, Dec 7, 2019

@TheDiveO

pytest fixtures cannot be used in unittest.TestCase test methods. The unittest integration in pytest uses the unittest Runner to actually run the tests, and the unittest runner doesn’t know about fixtures (this applies to any fixture, not only mocker).

One workaround is to use an autouse fixture (which work un unittest.TestCase subclasses) to obtain the fixture you are interested in, then use it later. Example:


class KeysModuleTests(TestCase):

    @pytest.fixture(autouse=True)
    def __inject_fixtures(self, mocker):
        self.mocker = mocker

    def test_calls_operation(self):
        keys = mnt.keys.end()
        self.mocker.spy(keys, 'op_end')
        # ...

Closing for now but feel free to follow up with further questions.

4reactions
alterEgo123commented, Aug 28, 2020

It returns missing 1 required positional argument 😔

Read more comments on GitHub >

github_iconTop Results From Across the Web

Missing 1 required positional argument: 'its' - Stack Overflow
when executing this code, an error occurs: TypeError: j1() missing 1 required positional argument: 'its' elif id == 526891231 and "/remove" ...
Read more >
9 Testing - Data Pipelines with Apache ... - liveBook · Manning
Mimicking a DAG run to test tasks which apply templating; Faking requests and responses to external systems with mocking to keep tests self-contained;...
Read more >
Cheat Sheet of Python Mock - Ines Panker
Level 1: Creating a new Mock object · Level 2: Missing attributes · Level 3: The attribute with the very unfortunate name -...
Read more >
Introduction | The Function of Humour in Roman Verse Satire
Humour may lead the eye away from a weak point in the argument, ... Those who adopt the latter position, a more modern...
Read more >
pact-python
2.6.1 - Fix issue where mocked `urlopen` didn't handle the correct number of positional arguments ```. bethskurrie. 2018-12-20 23:15.
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