[py-dev] more doctest...
Following up on my other doctest email, here's a slightly better technique I'm using: from test_fixture import DoctestCollector import some_module collect_doctest = DoctestCollector(some_module) Then I added something to py.test.collect.Module: def collect_collectors(self, extpy): if extpy.check(basestarts='collect_'): iter = extpy.resolve()(self.extpy) for item in iter: yield item And this is the new test_fixture.py, with just a small addition to DoctestCollector (__init__ and __call__), though extpy starts to feel pretty extraneous: class DoctestCollector(PyCollector): def __init__(self, extpy_or_module): if isinstance(extpy_or_module, types.ModuleType): self.module = extpy_or_module self.extpy = None else: self.extpy = extpy_or_module self.module = self.extpy.getpymodule() def __call__(self, extpy): # we throw it away, because this has been set up to explicitly # check another module; maybe this isn't clean if self.extpy is None: self.extpy = extpy return self def __iter__(self): finder = doctest.DocTestFinder() tests = finder.find(self.module) for t in tests: yield DoctestItem(self.extpy, t) class DoctestItem(DoctestCollector.Item): def __init__(self, extpy, doctestitem, *args): self.extpy = extpy self.doctestitem = doctestitem self.name = extpy.basename self.args = args def execute(self, driver): runner = doctest.DocTestRunner() driver.setup_path(self.extpy) target, teardown = driver.setup_method(self.extpy) try: (failed, tried), run_output = capture_stdout(runner.run, self.doctestitem) if failed: raise self.Failed(msg=run_output, tbindex=-2) finally: if teardown: teardown(target) def capture_stdout(func, *args, **kw): newstdout = StringIO() oldstdout = sys.stdout sys.stdout = newstdout try: result = func(*args, **kw) finally: sys.stdout = oldstdout return result, newstdout.getvalue() -- Ian Bicking / ianb@colorstudy.com / http://blog.ianbicking.org
Ian Bicking wrote:
Following up on my other doctest email, here's a slightly better technique I'm using:
from test_fixture import DoctestCollector import some_module
collect_doctest = DoctestCollector(some_module)
Then I added something to py.test.collect.Module:
def collect_collectors(self, extpy): if extpy.check(basestarts='collect_'): iter = extpy.resolve()(self.extpy) for item in iter: yield item
Thinking about this, it might be better if every test was potentially a collection of tests. Then something like the parameter-adding decorator would work. Or did I already mention that? I might be going in circles on this. Anyway, I'm not entirely sure how to test if it's a collection; I suppose you could just try iterating over it. But right now collections need access to the extpy object, since that's used by the reporter or driver or something, so we can't actually iterate over the object we find. -- Ian Bicking / ianb@colorstudy.com / http://blog.ianbicking.org
participants (1)
-
Ian Bicking