0

I'm starting a small project in Python and I want to create a unittest with py.test. I need to patch a pymongo module or a MongoClient class. So I tried something like this:

@pytest.fixture(scope='module')
def mock_pymongo(monkeypatch):
    monkeypatch.setattr('pymongo', mongomock)

or somthing like this :

@pytest.fixture(scope='class')
def mock_pymongo(monkeypatch):
    monkeypatch.setattr('pymongo.mongo_client.MongoClient', mongomock.mongo_client.MongoClient)

now in both test I am doing I get a scope mismatch error

scopeMismatch: You tried to access the 'function' scoped fixture 'monkeypatch' with a 'module' scoped request object, involved factories
tests/test_1.py:17:  def mock_pymongo(monkeypatch)

Maybe I'm using the right tool but is there any way using monkeypatch in order to mock class and module in my unit test?

Matthias
  • 4,481
  • 12
  • 45
  • 84
aj.styles
  • 1
  • 2

1 Answers1

0

The scope argument doesn't specifiy what to patch - it specifies the lifetime of the fixture. With the default scope (module), the patching will happen before each test, and be reverted after the test. That's generally the behavior you want.

The Compiler
  • 11,126
  • 4
  • 40
  • 54