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 _make_validator(logger=None): logger = MemoryLogger() try: yield logger finally: logger.validate() if logger.tracebackMessages: UnflushedTracebacks(logger.tracebackMessages)
def wrapper(self, *args, **kwargs): skipped = False kwargs["logger"] = logger = MemoryLogger() self.addCleanup(check_for_errors, logger) # TestCase runs cleanups in reverse order, and we want this to # run *before* tracebacks are checked: if assertion is not None: self.addCleanup( lambda: skipped or assertion(self, logger, *assertionArgs, **assertionKwargs) ) try: return function(self, *args, **kwargs) except SkipTest: skipped = True raise
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), )
logger = Logger() def connected(self, client): self.is_connected = True self.client = client def disconnected(self): self.is_disconnected = True self.client = None def cluster_updated(self, configuration, cluster_state): self.desired = configuration self.actual = cluster_state TEST_ACTION = start_action(MemoryLogger(), 'test:action') class AgentClientTests(TestCase): """ Tests for ``AgentAMP``. """ def setUp(self): super(AgentClientTests, self).setUp() self.agent = FakeAgent() self.reactor = Clock() self.client = AgentAMP(self.reactor, self.agent) self.client.makeConnection(StringTransportWithAbort()) # The server needs to send commands to the client, so it acts as # an AMP client in that regard. Due to https://tm.tl/7761 we need # to access the passed in locator directly.
) RUN_TEST = ActionType( u"run-test", [_NAME], [], u"A test is run.", ) # On Python 3, we want to use our custom JSON encoder when validating messages # can be encoded to JSON: if PY2: _memory_logger = MemoryLogger else: _memory_logger = lambda: MemoryLogger(encoder=AnyBytesJSONEncoder) @attr.s class EliotLoggedRunTest(object): """ A *RunTest* implementation which surrounds test invocation with an Eliot-based action. This *RunTest* composes with another for convenience. :ivar case: The test case to run. :ivar handlers: Pass-through for the wrapped *RunTest*. :ivar last_resort: Pass-through for the wrapped *RunTest*.
def logger(): test_logger = MemoryLogger() swap_logger(test_logger) yield test_logger check_for_errors(test_logger)