[py-dev] py.test: doctests
I played around with doctests today, here's the collector I have so far (requires the 2.4 version of doctest): class DoctestCollector(PyCollector): def __iter__(self): finder = doctest.DocTestFinder() tests = finder.find(self.extpy.getpymodule()) 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: (tried, failed), output = capture_stdout( runner.run, self.doctestitem) if failed: raise self.Failed(msg=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() Some problems: * Still the problem where overriding one collector (in this case, using DoctestCollector) cancels the normal collection, instead of adding to it. I guess it's possible that we could do collect_doctests, then use the finder on each object (since the finder can work on individual functions and methods, not just module objects). * Still the naming problem. DocTest instances (doctestitem) have a name attribute, I suppose that should be involved. * The exception reports in py.test aren't really meaningful. tbindex=-2 got rid of some of them, but I wasn't sure how to get rid of all of them. * Generally the results of running the doctest should be folded into the py.test output better. I find item.Item.Outcome a little confusing, as there's some possible attributes, but they aren't documented in that class (due to self.__dict__.update(kw)). I'd rather those attributes be class attributes, to indicate defaults and provide a place for documentation. Though generally I don't understand Outcome. Also, it seems to be raised, but it's not a subclass of Exception? Anyway, it's probably not too hard, if I understood the reporter better; maybe subclassing DocTestRunner. -- Ian Bicking / ianb@colorstudy.com / http://blog.ianbicking.org
participants (1)
-
Ian Bicking