Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14215

Unittest Patch not altering decorator

$
0
0

I've been having a great deal of trouble regarding Unittest.mock.patch

After much research and crawling SO, I've broken down and decided to ask the people brighter than myself.

Below is a simple function to add two ints with a decorator, which in turn will add one more to the result of add. My goal is to mock the original decorator so I am able to test just the add function.

Thank you for any help!

File Structure

project_folder/│├── test_script.py└── add.py

add.py

####Decoratordef decor(func):    @functools.wraps(func)    def wrapper(*arg, **kwargs):        result = func(*arg, **kwargs)        return result + 1    return wrapper@decordef add(a,b):    return a+b#Result add(1,1) == 3

test_script.py

def mock_decorator(func):    @functools.wraps(func)    def wrapper(*args, **kwargs):        result = func(*args, **kwargs)        return result    return wrapperwith patch('add.decor', new=mock_decorator):    # import 'add' after patching    from add import add as patched_add    #pytest test function    def test_add():        result = patched_add(1, 1)        assert result == 2
--Result    def test_add():        result = patched_add(1, 1)>       assert result == 2E       assert 3 == 2

Viewing all articles
Browse latest Browse all 14215

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>