def test_print_when_expected(): """ Generator that creates tests that ensure that methods print For most logger classes <class>.log() and <class>.summary() should print something, for some classes <class>.start() should print. This doesn't try to check what they are printing, just that they are printing, since 1) the output is very implementation dependent, and 2) it is subject to change. """ # a list of tuples which element 1 is the name of the class and method # element 2 is a callable, and element 3 is a list of arguments to be # passed to that callable. it needs to work with the splat operator printing = [] # Test QuietLog quiet = log.QuietLog(TEST_STATE) printing.append(('QuietLog.log', quiet.log, ['pass'])) printing.append(('QuietLog.summary', quiet.summary, [])) # Test VerboseLog verbose = log.VerboseLog(TEST_STATE) printing.append(('VerboseLog.start', verbose.start, ['a test'])) printing.append(('VerboseLog.log', verbose.log, ['pass'])) printing.append(('VerboseLog.summary', verbose.summary, [])) for name, func, args in printing: check_for_output.description = "{} produces output".format(name) yield check_for_output, func, args
def test_log(self, log_state): # pylint: disable=redefined-outer-name """Test the output of the log method.""" quiet = log.QuietLog(log_state, threading.Lock()) quiet.start(None) quiet.log('pass') sys.stdout.seek(0) actual = sys.stdout.read() assert actual == b'[1/1] pass: 1 -\n'
def test_start(self, log_state): # pylint: disable=redefined-outer-name """Test that the start method doesn't have output.""" quiet = log.QuietLog(log_state, threading.Lock()) quiet.start(None) quiet.start('foo') sys.stdout.seek(0) actual = sys.stdout.read() assert actual == b''
def test_summary(self, log_state): # pylint: disable=redefined-outer-name """Test the output of the summary method.""" quiet = log.QuietLog(log_state, threading.Lock()) # Call log to set the total correctly, then truncate and remove the # values, so the we can test quiet.start(None) quiet.log('pass') sys.stdout.seek(0) sys.stdout.truncate() quiet.summary() sys.stdout.seek(0) # Because of the 'lastlength' mechanims there will likely be # trainling whitespace after the the output, it's not useful to # test that here, so just strip it. assert sys.stdout.read().rstrip() == b'[1/1] pass: 1'
def test_noprint_when_expected(): """ Generate tests for methods that shouldn't print some methods shouldn't print anything, ensure that they don't """ # a list of tuples which element 1 is the name of the class and method # element 2 is a callable, and element 3 is a list of arguments to be # passed to that callable. it needs to work with the splat operator printing = [] # Test QuietLog quiet = log.QuietLog(TEST_STATE) printing.append(('QuietLog.start', quiet.start, ['name'])) # Test DummyLog dummy = log.DummyLog(TEST_STATE) printing.append(('DummyLog.start', dummy.start, ['name'])) printing.append(('DummyLog.log', dummy.log, ['pass'])) printing.append(('DummyLog.summary', dummy.summary, [])) for name, func, args in printing: check_no_output.description = "{} produces no output".format(name) yield check_no_output, func, args