python_examples/patch_example.py

24 lines
741 B
Python
Raw Normal View History

2023-08-23 11:11:51 -04:00
#!/usr/bin/env python3
import unittest
from io import StringIO
from unittest.mock import patch
2023-09-15 12:55:39 -04:00
import print_things
2023-08-23 11:11:51 -04:00
2023-08-23 12:02:52 -04:00
class TestPrintFunction(unittest.TestCase):
2023-08-23 11:11:51 -04:00
def test_print_example(self):
# The expected output of the print statement
expected_output = "the world\n"
# Using a context manager to temporarily replace sys.stdout with a StringIO object
2023-08-23 12:02:52 -04:00
with patch("sys.stdout", new=StringIO()) as captured_output:
print_things.print_the_world() # Call the function that has the print statement
2023-08-23 11:11:51 -04:00
# Assert the captured output is equal to the expected output
self.assertEqual(captured_output.getvalue(), expected_output)
2023-08-23 12:02:52 -04:00
if __name__ == "__main__":
2023-08-23 11:11:51 -04:00
unittest.main()