vendredi 11 décembre 2020

Why can this test methods only run in separate classes?

There is very simple thing to test in file named gui.py:

    def getInputs():
        a = int(input("Enter Liters : "))
        b = int(input("Enter Persons: "))

Typical things like positive numbers, no characters etc. Now there are test cases that run, e.g.:

import gui      # The code to test
import unittest   # The test framework
from unittest.mock import patch

class Test_guiInputs1(unittest.TestCase):
       
    @patch('builtins.input')
    
    def test_getInputs1(self, mock_input):
        mock_input.side_effect = [20, 2]
        tuple = (20, 2)
        self.assertEqual(gui.getInputs(), tuple)

class Test_guiInputs2(unittest.TestCase):
    
    @patch('builtins.input')
    
    def test_getInputs(self, mock_input):
        mock_input.side_effect = [-20, 2]
        tuple = (-20, 2)
        self.assertEqual(gui.getInputs(), tuple)

Now there are 2 classed with 1 method, since I don't get managed to enter the second method in the first class. I wonder if the mock function need some reinitialization or else. All approaches like this ...

class Test_guiInputs3(unittest.TestCase):
       
    @patch('builtins.input')
    
    def test_getInputs1(self, mock_input):
        mock_input.side_effect = [-20, 2]
        tuple = (-20, 2)
        self.assertEqual(gui.getsome(), tuple)

    def test_getInputs2(self, mock_input2):
        mock_input2.side_effect = [-20, 2]
        tuple2 = (-20, 2)
        self.assertEqual(gui.getsome(), tuple2)

... lead to failing test result at the 2nd method. The first one gets a green flag in VSC (as expected). The second one - even with the same values from the first test case. Has anyone an idea what is going wrong? (Python 3.7.1, VSC, Windows 10 Pro in case that is important for any known version specific bugs.)

Aucun commentaire:

Enregistrer un commentaire