def test_skip_cleans_up(self): """ After a skipped test the global Eliot logging state is restored. """ # Save the logger that's active before we do anything so that we can # restore it later. Also install another logger so we can compare it # to the active logger later. expected = MemoryLogger() original = swap_logger(expected) # Restore it, whatever else happens. self.addCleanup(lambda: swap_logger(original)) class SkipProbe(SyncTestCase): @skip("It's a skip test.") def test_skipped(self): pass case = SkipProbe("test_skipped") case.run() # Retrieve the logger that's active now that the skipped test is done # so we can check it against the expected value. actual = swap_logger(MemoryLogger()) self.assertThat( actual, Is(expected), )
def run_with_logging(*args, **kwargs): validating_logger = _memory_logger() original = swap_logger(None) try: swap_logger(_TwoLoggers(original, validating_logger)) with RUN_TEST(name=test_id): try: return test_method(*args, **kwargs) finally: check_for_errors(validating_logger) finally: swap_logger(original)
def wrapper(self, *args, **kwargs): logger = kwargs["logger"] previous_logger = swap_logger(logger) def cleanup(): swap_logger(previous_logger) self.addCleanup(cleanup) return function(self, *args, **kwargs)
def test_validation_failure(self): """ If a test emits a log message that fails validation then an error is added to the result. """ # Make sure we preserve the original global Eliot state. original = swap_logger(MemoryLogger()) self.addCleanup(lambda: swap_logger(original)) class ValidationFailureProbe(SyncTestCase): def test_bad_message(self): # This message does not validate because "Hello" is not an # int. MSG = MessageType("test:eliotutil", fields(foo=int)) MSG(foo="Hello").write() result = TestResult() case = ValidationFailureProbe("test_bad_message") case.run(result) self.assertThat( result.errors, HasLength(1), )
def cleanup(): swap_logger(previous_logger)
def logger(): test_logger = MemoryLogger() swap_logger(test_logger) yield test_logger check_for_errors(test_logger)