def test_success(self): """ A test run is still successful even if there are expected failures. """ self.result.addExpectedFailure(self.test, Failure(Exception()), makeTodo('todo!')) self.assertEqual(True, self.result.wasSuccessful())
def test_accumulation(self): """ L{reporter.Reporter} accumulates the expected failures that it is notified of. """ self.result.addExpectedFailure(self.test, Failure(Exception()), makeTodo('todo!')) self.assertEqual(self._getTodos(self.result), 1)
def test_booleanTodo(self): """ Booleans CAN'T be used as the value of a todo. Maybe this sucks. This is a test for current behavior, not a requirement. """ self.result.addExpectedFailure(self.test, Failure(Exception()), makeTodo(True)) self.assertRaises(Exception, self.result.done)
def test_unexpectedSuccess(self): """ A test which is marked as todo but succeeds will have an unexpected success reported to its result. A test run is still successful even when this happens. """ self.result.addUnexpectedSuccess(self.test, makeTodo("Heya!")) self.assertEqual(True, self.result.wasSuccessful()) self.assertEqual(self._getUnexpectedSuccesses(self.result), 1)
def test_basicErrors(self): """ The reporter's L{printErrors} method should include the value of the Todo. """ self.result.addExpectedFailure(self.test, Failure(Exception()), makeTodo('some reason')) self.result.done() output = self.stream.getvalue().splitlines()[4].strip() self.assertEqual(output, "Reason: 'some reason'")
def test_summary(self): """ The reporter's C{printSummary} method should print the number of expected failures that occured. """ self.result.addExpectedFailure(self.test, Failure(Exception()), makeTodo('some reason')) self.result.done() output = self.stream.getvalue().splitlines()[-1] prefix = 'PASSED ' self.failUnless(output.startswith(prefix)) self.assertEqual(output[len(prefix):].strip(), '(expectedFailures=1)')
def _makeTodo(value): """ Return a L{Todo} object built from C{value}. This is a synonym for L{twisted.trial.unittest.makeTodo}, but imported locally to avoid circular imports. @param value: A string or a tuple of C{(errors, reason)}, where C{errors} is either a single exception class or an iterable of exception classes. @return: A L{Todo} object. """ from twisted.trial.unittest import makeTodo return makeTodo(value)
def addUnexpectedSuccess(self, test, todo=None): """ Report that the given test succeeded against expectations. In Trial, tests can be marked 'todo'. That is, they are expected to fail. When a test that is expected to fail instead succeeds, it should call this method to report the unexpected success. @type test: L{pyunit.TestCase} @type todo: L{unittest.Todo}, or L{None}, in which case a default todo message is provided. """ if todo is None: todo = makeTodo(self._DEFAULT_TODO) self.unexpectedSuccesses.append((test, todo))
def addExpectedFailure(self, test, error, todo=None): """ Report that the given test failed, and was expected to do so. In Trial, tests can be marked 'todo'. That is, they are expected to fail. @type test: L{pyunit.TestCase} @type error: L{Failure} @type todo: L{unittest.Todo}, or L{None}, in which case a default todo message is provided. """ if todo is None: todo = makeTodo(self._DEFAULT_TODO) self.expectedFailures.append((test, error, todo))
def _ebDeferTestMethod(self, f, result): if f.check(TodoTest): result.addExpectedFailure(self, f, unittest.makeTodo(f.getErrorMessage())) else: return unittest.TestCase._ebDeferTestMethod(self, f, result)
class TodoTest(unittest.TestCase): """ Tests for L{reporter.Reporter}'s handling of todos. """ def setUp(self): from twisted.trial.test import sample self.stream = StringIO.StringIO() self.result = reporter.Reporter(self.stream) self.test = sample.FooTest('test_foo') def _getTodos(self, result): """ Get the number of todos that happened to a reporter. """ return len(result.expectedFailures) def _getUnexpectedSuccesses(self, result): """ Get the number of unexpected successes that happened to a reporter. """ return len(result.unexpectedSuccesses) def test_accumulation(self): """ L{reporter.Reporter} accumulates the expected failures that it is notified of. """ self.result.addExpectedFailure(self.test, Failure(Exception()), makeTodo('todo!')) self.assertEqual(self._getTodos(self.result), 1) def test_success(self): """ A test run is still successful even if there are expected failures. """ self.result.addExpectedFailure(self.test, Failure(Exception()), makeTodo('todo!')) self.assertEqual(True, self.result.wasSuccessful()) def test_unexpectedSuccess(self): """ A test which is marked as todo but succeeds will have an unexpected success reported to its result. A test run is still successful even when this happens. """ self.result.addUnexpectedSuccess(self.test, makeTodo("Heya!")) self.assertEqual(True, self.result.wasSuccessful()) self.assertEqual(self._getUnexpectedSuccesses(self.result), 1) def test_summary(self): """ The reporter's C{printSummary} method should print the number of expected failures that occured. """ self.result.addExpectedFailure(self.test, Failure(Exception()), makeTodo('some reason')) self.result.done() output = self.stream.getvalue().splitlines()[-1] prefix = 'PASSED ' self.failUnless(output.startswith(prefix)) self.assertEqual(output[len(prefix):].strip(), '(expectedFailures=1)') def test_basicErrors(self): """ The reporter's L{printErrors} method should include the value of the Todo. """ self.result.addExpectedFailure(self.test, Failure(Exception()), makeTodo('some reason')) self.result.done() output = self.stream.getvalue().splitlines()[4].strip() self.assertEqual(output, "Reason: 'some reason'") def test_booleanTodo(self): """ Booleans CAN'T be used as the value of a todo. Maybe this sucks. This is a test for current behavior, not a requirement. """ self.result.addExpectedFailure(self.test, Failure(Exception()), makeTodo(True)) self.assertRaises(Exception, self.result.done) def test_exceptionTodo(self): """ The exception for expected failures should be shown in the C{printErrors} output. """ try: 1 / 0 except Exception, e: error = e self.result.addExpectedFailure(self.test, Failure(error), makeTodo("todo!")) self.result.done() output = '\n'.join(self.stream.getvalue().splitlines()[3:]).strip() self.assertTrue(str(e) in output)