Bypassing a decorator for unit testing
<p>I have a decorator <code>@auth</code> which basically checks a database to ensure a user can access a given REST call. I would like to write some unit tests for these calls. My initial idea was to simply monkeypatch the decorator into a pass through that does nothing. (My initial idea failed, so I may just monkeypatch some function inside of <code>@auth</code> so that it always passes, but I'm still curious if I can bypass a decorator completely)</p>

<p>I threw together a quick sample of what I was hoping to accomplish.</p>

<p>example.py</p>

<pre><code># example.py
from __future__ import print_function

def sample_decorator(func):
    def decorated(*args, **kwargs):
        print("Start Calculation")
        ans = func(*args, **kwargs) + 3
        print(ans)
        print("Finished")
        return ans
    return decorated

@sample_decorator
def add(a, b):
    return a + b
</code></pre>

<p>test_example.py</p>

<pre><code># test_example.py
from __future__ import print_function
import pytest

import example

def test_add_with_decorator():
    assert example.add(1, 1) == 5

def testadd_with_monkeypatch_out_decorator(monkeypatch):
    monkeypatch.setattr(example, 'sample_decorator', lambda func: func)
    assert example.add(1, 1) == 2  # this fails, but is the behaviour I want
</code></pre>

<p>Is there some straight forward way to accomplish this?</p>