samedi 2 janvier 2021

Why is Mock incorrectly adding side_effect?

I understand I am probably testing too much with each test, but I am learning. Also i have fixed the 1st test to not give the false input as the 1st side effect. Code works, so the "issue" is 'technically' fixed. I just thought it was peculiar quirk and I wanted to ask.

The quirk:
When each test was run individually (whether by commenting out or specifying on CLI) the test works correctly and passes fine. But what i did notice was that together, either None was received as the patched input on 2nd test or the value was given of the side_effect of the 1st test or a.

Test_email.py

class TestEmailer(unittest.TestCase):
    week = '0'
    test_sport = 'football'



    @patch('builtins.input', side_effect=['a', week])
    def test_for_correct_week_selection(self, mock_week):
        '''Verify input is accepted as the correct one '''
        value = mailer.week_for_email()
        self.assertEqual(self.week, value)

    @patch('builtins.input', side_effect=['1', week])
    def test_for_correctly_built_subject(self, mock_input):
        ''' Very subject is correctly formed '''
        test = (f'{self.test_sport.title()} games for Week {self.week}')
        subject = mailer.build_subject()
        self.assertEqual(test, subject)

email.py


def week_for_email():
    week = None
    if makefile.week is None:
        active=True
        while active:
            week = input('Please input week ')
            try:
                if not week.isnumeric():
                    raise AttributeError('Need a number')
            except AttributeError as e:
                print(e)
            else:
                break
  
            makefile.week = week 
    else:
        week = makefile.week
    return week

def select_sport():
    print('Which sport to send out email? ')
    game = SportPicker.get_sport() <-- This is also a while loop
    return game

def build_subject():
    sport = select_sport()
    week = week_for_email()
    return '{} games for Week {}'.format(sport.title(), week)

Sport Picker

class SportPicker:
    
    sports = ((1, 'football'),
        (2, 'soccer'),
        (3, 'basketball'),
        (4, 'hockey'))

    @classmethod
    def get_sport(cls):
        while True:
            for sport in cls.sports:
                print('{}: {}'.format(sport[0], sport[1]))
            val = input('Enter a number for sport: ')
            val_list = map(str, range(1,len(cls.sports)+1))
            if val not in val_list:
                print('Enter [{}]'.format(', '.join(val for val in val_list)))
            else:
                for sport in cls.sports:
                    val = int(val)
                    if val == sport[0]:
                        sport = sport[1]
                        return sport

The Error

FAIL: test_for_correctly_built_subject (bet_tastic.tests.test_email.TestEmailer)
Very subject is correctly formed
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python38\lib\unittest\mock.py", line 1325, in patched
    return func(*newargs, **newkeywargs)
  File "C:\Users\Antho\Desktop\projectz\bet_tastic\bet_tastic\tests\test_email.py", line 23, in test_for_correctly_built_subject
    self.assertEqual(test, subject)
AssertionError: 'Football games for Week 0' != 'Football games for Week a'
- Football games for Week 0
+ Football games for Week a
?                         ^


----------------------------------------------------------------------

Aucun commentaire:

Enregistrer un commentaire