Вот код:
Код: Выделить всё
import unittest
from unittest.mock import patch
import io
def greet_user(name):
print(f"Hello, {name}!")
class TestGreetUser(unittest.TestCase):
@patch('sys.stdout', new_callable=io.StringIO)
def test_with_patch(self, mock_stdout):
greet_user("Alice")
print("OUTPUT WITH PATCH:")
print(mock_stdout.getvalue()) #trying to print what was on the console
def test_without_patch(self):
print("OUTPUT WITHOUT PATCH:")
greet_user("Alice")
if __name__ == '__main__':
unittest.main()
Код: Выделить всё
.OUTPUT WITHOUT PATCH:
Hello, Alice!
.
----------------------------------------------------------------------
Ran 2 tests in 0.011s
OK
Подробнее здесь: https://stackoverflow.com/questions/792 ... t-not-work
Мобильная версия