mercredi 24 octobre 2018

Patching decorator in setUp or setUpClass in TestCases does not work

I am trying to patch some functions during either the setUp or setUpClass methods of a unittest.TestCase subclass.

The following script produces more output that I would expect.

import unittest
from unittest.mock import patch


@patch('sys.stdout.write', lambda x: None)
class TestStringMethods(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        print('I am the setupclass.')

    def setUp(self):
        print('I am the setup.')

    def test_print(self):
        print('I am the test')


if __name__ == '__main__':
    unittest.main()

With the patch the output is

I am the setupclass.
I am the setup.
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

Without the patch the output is

I am the setupclass.
I am the setup.
I am the test
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

Where I would expect no output whatsoever if the patch worked in setUp and setUpClass.

How can I get the mock patch to be applied in these methods?

Aucun commentaire:

Enregistrer un commentaire