[Biopython-dev] docstring tests
Peter
biopython at maubp.freeserve.co.uk
Thu Feb 12 11:49:53 UTC 2009
Hi Michiel (and everyone else),
I was wondering about how the doctests are currently integrated into
run_tests.py, and wondered if this patch makes things more concise?
This patch is against run_tests.py CVS revision 1.22, essentially it
adds the doctest modules to the list of tests - rather than as a
separate list. The code becomes slightly shorter, but I am not sure
if this is actually clearer or not.
Note - this does not address the issue of how to run just the doctests
- something I think is very useful when working on them.
Peter
$ diff run_tests.py run_tests2.py
209,211c209
< if self.tests:
< self.doctest_modules = []
< else:
---
> if not self.tests:
218c216,222
< self.doctest_modules = DOCTEST_MODULES
---
> if sys.version_info[:2] < (2, 4):
> #On python 2.3, doctest uses slightly different formatting
> #which would be a problem as the expected output won't match.
> #Also, it can't cope with <BLANKLINE> in a doctest string.
> sys.stderr.write("Skipping doctests which require Python 2.4+\n")
> else :
> self.tests.extend(DOCTEST_MODULES)
234,240c238,253
< module = __import__(name)
< suite = unittest.TestLoader().loadTestsFromModule(module)
< if suite.countTestCases()==0:
< # This is a print-and-compare test instead of a unittest-
< # type test.
< test = ComparisonTestCase(name, output)
< suite = unittest.TestSuite([test])
---
> if "." in name :
> #Its a doc test
> #Can't use fromlist=name.split(".") until python 2.5+
> module = __import__(name, None, None, name.split("."))
> suite = doctest.DocTestSuite(module)
> del module
> else :
> #Its a unittest (or a print-and-compare test)
> suite = unittest.TestLoader().loadTestsFromName(name)
> if suite.countTestCases()==0:
> # This is a print-and-compare test instead of a
> # unittest-type test.
> test = ComparisonTestCase(name, output)
> suite = unittest.TestSuite([test])
263,277d275
< def runDocTest(self, name):
< #Can't use fromlist=name.split(".") until python 2.5+
< module = __import__(name, None, None, name.split("."))
< sys.stderr.write("%s docstring test ... " % module.__name__)
< suite = doctest.DocTestSuite(module)
< result = self._makeResult()
< suite.run(result)
< if result.wasSuccessful():
< sys.stderr.write("ok\n")
< return True
< else:
< sys.stderr.write("FAIL\n")
< result.printErrors()
< return False
<
287,297d284
< if sys.version_info[:2] < (2, 4):
< #On python 2.3, doctest uses slightly different formatting
< #which would be a problem as the expected output won't match.
< #Also, it can't cope with <BLANKLINE> in a doctest string.
< sys.stderr.write("Docstring tests require Python 2.4 or
later; skipping\n")
< else:
< for test in self.doctest_modules:
< ok = self.runDocTest(test)
< if not ok:
< failures += 1
< total += 1
More information about the Biopython-dev
mailing list