Python unittest, tests without input are skipped

58 views Asked by At

I have written tests for several functions, some of these functions require input from the user. I use mock.patch from the unittest library to mock user input.

class TestCodes(unittest.TestCase):

    @mock.patch('builtins.input', side_effect=[1])
    def testNames(self, mock_input):
        """
        Tests function 1
        """
        self.assertEqual(functionOne('input'), 1337)


    @mock.patch('builtins.input', side_effect=[-1])
    def testRepeater(self, mock_input):
        """
        Tests that the output is equal to the input
        """
        self.assertEqual(functionTwo('test1'), -1)

This all works fine and dandy (Ran 2 tests in 0.018s). However, when I add another test that doesn't use mock input, the unittest skips all tests that need input (see below)

class TestCodes(unittest.TestCase):

    @mock.patch('builtins.input', side_effect=[1])
    def testNames(self, mock_input):
        """
        Tests function 1
        """
        self.assertEqual(functionOne('input'), 1337)


    @mock.patch('builtins.input', side_effect=[-1])
    def testRepeater(self, mock_input):
        """
        Tests that the output is equal to the input
        """
        self.assertEqual(functionTwo('test1'), -1)


    def testFunniness(self):
       self.assertEqual(functionThree('test4'), 42)

Ran 1 test in 0.007s

0

There are 0 answers