[TIP] Meta-test methods...
Robert Collins
robertc at robertcollins.net
Mon Apr 27 16:40:30 PDT 2009
On Mon, 2009-04-27 at 13:39 -0400, Doug Philips wrote:
> On or about Monday, April 27, 2009, at 09:01AM, "Robert Collins indited:
> >I'd love it if you gave testscenarios a go, as it seems to address all
> >your points (including that each test+scenario gets a unique name so you
> >can select them individually).
>
> I presume you're referring to:
> http://lists.idyll.org/pipermail/testing-in-python/2009-April/001861.html
> ??
>
> I can see how to use that when the scenario lists can be computed at module load time, I'm still not sure I get how to compute them "late" (as per my examples in the previous emails) when the precise "values" are not known until the test method is run...
-----
import sys
from unittest import (TestCase, TestSuite, TestLoader, _TextTestResult,
_WritelnDecorator)
from testscenarios import generate_scenarios
class DemoTest(TestCase):
scenarios = []
def test_passes_lt(self):
self.assertTrue(self.start < self.end)
def test_fails_lt(self):
self.assertFalse(self.start < self.end)
if __name__ == '__main__':
# can't use TextTestRunner as we don't have a load hook available
# in python core yet, so we need a bit of plumbing that the test
# runner normally does.
loader = TestLoader()
tests = loader.loadTestsFromName(__name__)
# arbitrary delay goes here
# Now we decide what scenarios we want
DemoTest.scenarios.append(('01', dict(start=0, end=1)))
DemoTest.scenarios.append(('10', dict(start=1, end=0)))
# Now we're actually about to run.
suite = TestSuite(generate_scenarios(tests))
print suite.countTestCases()
for test in suite:
print test.id()
suite.run(_TextTestResult(_WritelnDecorator(sys.stdout), False, 2))
----
$ python <foo.py>
__main__.DemoTest.test_fails_lt(01)
__main__.DemoTest.test_fails_lt(10)
__main__.DemoTest.test_passes_lt(01)
__main__.DemoTest.test_passes_lt(10)
test_fails_lt (__main__.DemoTest) ... FAIL
test_fails_lt (__main__.DemoTest) ... ok
test_passes_lt (__main__.DemoTest) ... ok
test_passes_lt (__main__.DemoTest) ... FAIL
Note that the ids() have been set correctly; I'd forgotten that unittest
stopped showing ids some time back - and testtools' clone_with_new_id
doesn't set the __str__ for cloned tests; I think it should (though I'm
partial to showing test ids because they are so useful
programmatically).
-Rob
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part
Url : http://lists.idyll.org/pipermail/testing-in-python/attachments/20090428/d50666fb/attachment.pgp
More information about the testing-in-python
mailing list