mirror of
https://github.com/GAM-team/GAM.git
synced 2026-06-24 08:01:36 +00:00
Requires use of a context manager within each test method for stdout/stderr patches, rather than the @patch.object decorator. Otherwise, the test runner hijacks the patched object before the method under test can use it. Note: The `--buffer` switch in the unittest module will suppress all output during the test, unless the test fails. However, with this implementation, anything that would have been printed to the streams within the context manager will not be reflected in the test runner's output, should the test fail. See more in jay0lee/GAM#1068
60 lines
2.2 KiB
Python
60 lines
2.2 KiB
Python
"""Tests for display."""
|
|
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
import display
|
|
from var import ERROR_PREFIX
|
|
from var import WARNING_PREFIX
|
|
|
|
|
|
class DisplayTest(unittest.TestCase):
|
|
|
|
def test_print_error_prints_to_stderr(self):
|
|
message = 'test error'
|
|
with patch.object(display.sys.stderr, 'write') as mock_write:
|
|
display.print_error(message)
|
|
printed_message = mock_write.call_args[0][0]
|
|
self.assertIn(message, printed_message)
|
|
|
|
def test_print_error_prints_error_prefix(self):
|
|
message = 'test error'
|
|
with patch.object(display.sys.stderr, 'write') as mock_write:
|
|
display.print_error(message)
|
|
printed_message = mock_write.call_args[0][0]
|
|
self.assertLess(
|
|
printed_message.find(ERROR_PREFIX), printed_message.find(message),
|
|
'The error prefix does not appear before the error message')
|
|
|
|
def test_print_error_ends_message_with_newline(self):
|
|
message = 'test error'
|
|
with patch.object(display.sys.stderr, 'write') as mock_write:
|
|
display.print_error(message)
|
|
printed_message = mock_write.call_args[0][0]
|
|
self.assertRegex(printed_message, '\n$',
|
|
'The error message does not end in a newline.')
|
|
|
|
def test_print_warning_prints_to_stderr(self):
|
|
message = 'test warning'
|
|
with patch.object(display.sys.stderr, 'write') as mock_write:
|
|
display.print_error(message)
|
|
printed_message = mock_write.call_args[0][0]
|
|
self.assertIn(message, printed_message)
|
|
|
|
def test_print_warning_prints_error_prefix(self):
|
|
message = 'test warning'
|
|
with patch.object(display.sys.stderr, 'write') as mock_write:
|
|
display.print_error(message)
|
|
printed_message = mock_write.call_args[0][0]
|
|
self.assertLess(
|
|
printed_message.find(WARNING_PREFIX), printed_message.find(message),
|
|
'The warning prefix does not appear before the error message')
|
|
|
|
def test_print_warning_ends_message_with_newline(self):
|
|
message = 'test warning'
|
|
with patch.object(display.sys.stderr, 'write') as mock_write:
|
|
display.print_error(message)
|
|
printed_message = mock_write.call_args[0][0]
|
|
self.assertRegex(printed_message, '\n$',
|
|
'The warning message does not end in a newline.')
|