pytest-dev / pytest-bdd

BDD library for the py.test runner
https://pytest-bdd.readthedocs.io/en/latest/
MIT License
1.3k stars 219 forks source link

It's not possible to disable "argument is a fixture" behavior (pytest-bdd + unittest.mock) #330

Open psxvoid opened 5 years ago

psxvoid commented 5 years ago

Hi,

I'm trying to use pytest-bdd with unittest.mock - to be more precise - I have to patch a class (let's say 'MyClass') from a module_XXX in one of the steps. But the issues is that unittest.mock uses decorators to provide patched classes via method arguments. But pytest-bdd always considers arguments as fixtures and obviously cannot find them. Here is the code:

@then('it should call my class method_YYY')
@patch('module_XXX.MyClass')
def it_should_call_my_class_method_yyy(MyClassMock):
    module_XXX.__init__("__main__")
    assert MyClassMock.called

Is it possible to disable "argument is a fixture" pytest-bdd feature? Is pytest-bdd compatible with unittest.mock? It seems like, the documentation of pytest-bdd says nothing about such cases.

-Thanks

subhashb commented 4 years ago

I faced the same issue using patch() as a decorator, but you could use patch() as a Context Manager to circumvent the problem.

Refer to this article to understand how.