summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Lange <jml@mumak.net>2015-11-27 18:55:14 +0000
committerJonathan Lange <jml@mumak.net>2015-11-27 18:55:14 +0000
commit4962652dbf3726a2c30ff62c349ac3d88328a5d8 (patch)
tree7de16190f60c35002d61116041a54b7f731d0f10
parent8710d1de5172c37cf8ed40038b073b0d96aa228e (diff)
downloadtesttools-4962652dbf3726a2c30ff62c349ac3d88328a5d8.tar.gz
Begin to write scenario-based tests
-rw-r--r--testtools/testcase.py16
-rw-r--r--testtools/tests/samplecases.py31
-rw-r--r--testtools/tests/test_testcase.py23
3 files changed, 55 insertions, 15 deletions
diff --git a/testtools/testcase.py b/testtools/testcase.py
index 160351c..d219175 100644
--- a/testtools/testcase.py
+++ b/testtools/testcase.py
@@ -206,11 +206,7 @@ class TestCase(unittest.TestCase):
# Generators to ensure unique traceback ids. Maps traceback label to
# iterators.
self._traceback_id_gens = {}
- self.__setup_called = False
- self.__teardown_called = False
- # __details is lazy-initialized so that a constructed-but-not-run
- # TestCase is safe to use with clone_test_with_new_id.
- self.__details = None
+ self._reset()
test_method = self._get_test_method()
if runTest is None:
runTest = getattr(
@@ -230,6 +226,15 @@ class TestCase(unittest.TestCase):
(Exception, self._report_error),
]
+ def _reset(self):
+ """Reset the test case as if it had never been run."""
+ self._traceback_id_gens = {}
+ self.__setup_called = False
+ self.__teardown_called = False
+ # __details is lazy-initialized so that a constructed-but-not-run
+ # TestCase is safe to use with clone_test_with_new_id.
+ self.__details = None
+
def __eq__(self, other):
eq = getattr(unittest.TestCase, '__eq__', None)
if eq is not None and not unittest.TestCase.__eq__(self, other):
@@ -596,6 +601,7 @@ class TestCase(unittest.TestCase):
result.addUnexpectedSuccess(self, details=self.getDetails())
def run(self, result=None):
+ self._reset()
try:
run_test = self.__RunTest(
self, self.exception_handlers, last_resort=self._report_error)
diff --git a/testtools/tests/samplecases.py b/testtools/tests/samplecases.py
new file mode 100644
index 0000000..4f0f12a
--- /dev/null
+++ b/testtools/tests/samplecases.py
@@ -0,0 +1,31 @@
+# Copyright (c) 2015 testtools developers. See LICENSE for details.
+
+"""A collection of sample TestCases.
+
+These are primarily of use in testing the test framework.
+"""
+
+from testtools import TestCase
+
+
+class _NormalTest(TestCase):
+
+ def test_success(self):
+ pass
+
+ def test_error(self):
+ 1/0
+
+ def test_failure(self):
+ self.fail('arbitrary failure')
+
+
+"""
+A list that can be used with testscenarios to test every kind of sample
+case that we have.
+"""
+all_sample_cases_scenarios = [
+ ('simple-success-test', {'case': _NormalTest('test_success')}),
+ ('simple-error-test', {'case': _NormalTest('test_error')}),
+ ('simple-failure-test', {'case': _NormalTest('test_failure')}),
+]
diff --git a/testtools/tests/test_testcase.py b/testtools/tests/test_testcase.py
index 695b1a5..c3f8a93 100644
--- a/testtools/tests/test_testcase.py
+++ b/testtools/tests/test_testcase.py
@@ -54,6 +54,7 @@ from testtools.tests.helpers import (
FullStackRunTest,
LoggingResult,
)
+from testtools.tests.samplecases import all_sample_cases_scenarios
class TestPlaceHolder(TestCase):
@@ -1277,19 +1278,21 @@ class TestRunTwice(TestCase):
# 'run_tests_with'.
run_tests_with = FullStackRunTest
+ scenarios = all_sample_cases_scenarios
+
def test_runTwice(self):
- # Tests can be run twice.
- class NormalTest(TestCase):
- def test_method(self):
- pass
- test = NormalTest('test_method')
- result = unittest.TestResult()
- test.run(result)
- test.run(result)
- self.expectThat(result.errors, HasLength(0))
- self.assertThat(result.testsRun, Equals(2))
+ # Tests that are intrinsically determistic can be run twice and
+ # produce exactly the same results each time, without need for
+ # explicit resetting or reconstruction.
+ test = self.case
+ first_result = ExtendedTestResult()
+ test.run(first_result)
+ second_result = ExtendedTestResult()
+ test.run(second_result)
+ self.assertEqual(first_result._events, second_result._events)
def test_runTwiceTearDownFailure(self):
+ # XXX: jml: you should move this into _samplecases.
# Tests can be run twice, even if tearDown fails.
class NormalTest(TestCase):
def test_method(self):