4

I have a function adding subscription for my users. At the end of this function, an instruction check the subscriptions and do some stuff with patron if necessary. For my testing, I need to skip this last instruction ; so I tried to use mock.patch but despite all my tries, the code is always executed (and my tests failed)


mymodule/classes.py

class User:

    def add_subscription(self, start_date, end_date):
        subscriptions = self.get('subscriptions', [])
        subscriptions.append({'start': start_date, 'end': end_date})
        self['subscriptions'] = subscriptions
        check_subscriptions(self)  # <-- skip this instruction for unitesting


def check_subscriptions(user):
    print("Not need to print for unitesting")
    # next doing more stuff...
    ...

tests/user_unitest.py

import mock
from mymodule.classes import User
from datetime import datetime, timedelta

...

def test_subscriptions(user_with_no_subscription_fixture):
    user = user_with_no_subscription_fixture
    start = datetime.now()
    end = start + timedelta(days=10)
    user.add_subscription(start, end)
    ...

How can I use the @mock.patch (or other mock function) to don't enter into the User.check_subscriptions methods ?


What I tried (and didn't work)

@mock.patch('mymodule.classes.check_subscriptions')
def test_subscriptions(user_with_no_subscription_fixture):
    user = user_with_no_subscription_fixture
    start = datetime.now()
    end = start + timedelta(days=10)
    user.add_subscription(start, end)
def test_subscriptions(user_with_no_subscription_fixture):
    with mock.patch('mymodule.classes.check_subscriptions'):
        user = user_with_no_subscription_fixture
        start = datetime.now()
        end = start + timedelta(days=10)
        user.add_subscription(start, end)
@mock.patch('mymodule.classes.check_subscriptions', MagicMock())
def test_subscriptions(user_with_no_subscription_fixture):
    user = user_with_no_subscription_fixture
    start = datetime.now()
    end = start + timedelta(days=10)
    user.add_subscription(start, end)
@mock.patch('mymodule.classes.check_subscriptions', side_effect=...)
def test_subscriptions(user_with_no_subscription_fixture):
    user = user_with_no_subscription_fixture
    start = datetime.now()
    end = start + timedelta(days=10)
    user.add_subscription(start, end)

thanks for your always usefull help

Renaud Michotte
  • 389
  • 4
  • 17
  • What did you try? – chepner Apr 03 '20 at 12:38
  • `@mock.patch('mymodule.classes.User.check_subscriptions')`, with `side_effect`, `with mock.patch(...)` inside my test function, ... – Renaud Michotte Apr 03 '20 at 12:42
  • Please update the *question* with *exactly* how you tried. – chepner Apr 03 '20 at 12:54
  • There are a few inconsistencies in the code: you define `User` in `mymodule.user.py` (I guess you mean `mymodule/user.py`), but import it from `mymodule.classes`. Also `object` is incorrectly written (and incidentally not needed) and the `self` argument missing in the function - please copy and paste the relevant part of _the actual code_ here, otherwise it is difficult to see where the problem lies. – MrBean Bremen Apr 03 '20 at 17:36
  • @MrBeanBremen You're right. I tried to simplify my code to keep it understandable. My entire relevant code is more than 300 lines. Despite my previous copy errors (now corrected) the problem stay the same ;-) Thanks for your advices. – Renaud Michotte Apr 03 '20 at 21:11
  • Ok, better :) Now you patch `my module.user` instead of `mymodule.classes`. Is that also a typo? – MrBean Bremen Apr 03 '20 at 21:57

0 Answers0