0

I am trying to test my method that simply checks if a letter is in either of the lists of letters. If it is in list a, then I assign myletter to an imported object called yes; Otherwise, I assign myletter to an imported object called no.

I tried to write a testing file using MagicMock for it, but since I am new to the concept of testing using mock and magic mock, I am not sure how to further continue. I would appreciate any tips or any solutions on how to go with it and further continue.

Should I create mock object for each of the lists and another mock object for myletter to check it agains those two mock objects or how else I should continue on doing it?

My code:

from mydir.newdir import yes
from mydir.newdir import no

 
def yes_no_det(check_letter: str):
    
    first_list = ['a', 'b', 'c']
    
    second_list = ['d', 'e', 'f']
    
    myletter = None

    if check_letter:    
        if check_letter in first_list:
            myletter = yes.att
        elif check_letter in second_list:
            myletter = no.att

    return myletter

My testing so far:

import unittest
from mock import MagicMock, patch

class letter_check_test(TestCase):
    with patch('mydir.newdir.check_letter_py.yes') as mock_yes,\
    patch('mydir.newdir.check_letter_py.no') as mock_no:
        from mydir.newdir.check_letter_py import yes_no_det
        mock_check_letter = MagicMock()

        mock_yes = mock_yes.att
        mock_no = mock_no.att

    def test_check_letter(self):
        # I am not sure how further to continue or what to check here

Updated possibly right testing code:

class letter_check_test(TestCase):
    def test_yes_det():
        assert yes_no_det('a') == mydir.newdir.yes
    def test_no_det():
        assert yes_no_det('f') == mydir.newdir.no

    def test_neither_det():
        assert yes_no_det('x') == None

Thank you for any answers. Please let me know if I need to change/delete this post or anything without downgrading or reporting it. I would really appreciate that!

r_e
  • 242
  • 4
  • 14
  • I was trying to mock it since there could be many different cases for yes_no_det. From your code though, why would we do ```yes_no_det('a') == mydir.newdir.yes ```? The lists have the names of first_list and second_list. Why are we comparing ```yes_no_det('a')``` with ```mydir.newdir.yes``` instead of checking it against both lists? The reason I have the imported yes and no is because I use their attribute of ```att``` within the if statement. I am little bit confused about that. – r_e Jun 25 '21 at 14:09

1 Answers1

0

In this case, it was simply use of assert or assertequal code. For example:

class letter_check_testYES(Testing, TestingsCases):
    def test_letter_check(self):
        with patch('mydir.newdir.check_letter_py.yes') as YES:
            from mydir.newdir.check_letter_py import yes_no_det
            assert yes_no_det('a') == YES

class letter_check_testNO(Testing, TestingsCases):
    def test_letter_check(self):
        with patch('mydir.newdir.check_letter_py.no') as NO:
            from mydir.newdir.check_letter_py import yes_no_det
            assert yes_no_det('f') == YES

class letter_check_test(Testing, TestingsCases):
        from mydir.newdir.check_letter_py import yes_no_det
        assert yes_no_det('f') == NONE
r_e
  • 242
  • 4
  • 14