[svn r56285] sanitize doctesting a bit. this also fixes the problem that you could not have "import" working in doctests previously. thanks nshepperd.
--HG-- branch : trunk
This commit is contained in:
		
							parent
							
								
									cb53ad6010
								
							
						
					
					
						commit
						d4c3b3ce85
					
				|  | @ -502,13 +502,17 @@ class Generator(FunctionMixin, PyCollectorMixin, Collector): | |||
|             call, args = obj, () | ||||
|         return call, args  | ||||
| 
 | ||||
| class DoctestFile(PyCollectorMixin, FSCollector):  | ||||
| class DoctestFile(Module): | ||||
|     # XXX fix py.test reporting  | ||||
|     #     we subclass Module here in order to get py.test's reporting  | ||||
|     #     show the ".txt" filename in the test run much like a | ||||
|     #     python module shows up.  instead py.test needs to | ||||
|     #     support more direct means of influencing reporting.  | ||||
|     def run(self): | ||||
|         return [self.fspath.basename] | ||||
| 
 | ||||
|     def join(self, name): | ||||
|         from py.__.test.doctest import DoctestText | ||||
|         if name == self.fspath.basename: | ||||
|             item = DoctestText(self.fspath.basename, parent=self) | ||||
|             item._content = self.fspath.read() | ||||
|             return item | ||||
|             from py.__.test.doctest import DoctestFileContent | ||||
|             return DoctestFileContent(name, self) | ||||
| 
 | ||||
|  |  | |||
|  | @ -1,9 +1,6 @@ | |||
| import py | ||||
| 
 | ||||
| class DoctestText(py.test.collect.Item): | ||||
| 
 | ||||
|     def _setcontent(self, content): | ||||
|         self._content = content  | ||||
| class DoctestFileContent(py.test.collect.Item): | ||||
| 
 | ||||
|     #def buildname2items(self): | ||||
|     #    parser = py.compat.doctest.DoctestParser() | ||||
|  | @ -16,18 +13,12 @@ class DoctestText(py.test.collect.Item): | |||
|     #        d[str(i)] = ex | ||||
| 
 | ||||
|     def run(self): | ||||
|         mod = py.std.types.ModuleType(self.name)  | ||||
|         #for line in s.split('\n'):  | ||||
|         #    if line.startswith(prefix):  | ||||
|         #        exec py.code.Source(line[len(prefix):]).compile() in mod.__dict__  | ||||
|         #        line = "" | ||||
|         #    else:  | ||||
|         #        l.append(line) | ||||
|         self.execute(mod, self._content)  | ||||
|         self.execute() | ||||
|         | ||||
|     def execute(self, mod, docstring): | ||||
|         mod.__doc__ = docstring  | ||||
|         failed, tot = py.compat.doctest.testmod(mod, verbose=1) | ||||
|     def execute(self): | ||||
|         failed, tot = py.compat.doctest.testfile(str(self.fspath), module_relative=False, verbose=1) | ||||
|         #mod.__file__ = str(self.fspath) | ||||
|         #failed, tot = py.compat.doctest.testmod(mod, verbose=1) | ||||
|         if failed:  | ||||
|             py.test.fail("doctest %s: %s failed out of %s" %( | ||||
|                          self.fspath, failed, tot)) | ||||
|  |  | |||
|  | @ -78,14 +78,6 @@ class BaseReason(object): | |||
|     def __repr__(self): | ||||
|         return self.msg | ||||
| 
 | ||||
| class Broken(BaseReason): | ||||
|     def __repr__(self): | ||||
|         return "Broken: %s" % (self.msg,) | ||||
| 
 | ||||
| class _NotImplemented(BaseReason): | ||||
|     def __repr__(self): | ||||
|         return "Not implemented: %s" % (self.msg,) | ||||
| 
 | ||||
| # whatever comes here.... | ||||
| 
 | ||||
| def skip(msg=BaseReason()): | ||||
|  |  | |||
|  | @ -1,25 +1,30 @@ | |||
| 
 | ||||
| import py | ||||
| from py.__.test.doctest import DoctestText | ||||
| from py.__.test.outcome import Skipped, Failed, Passed, Outcome | ||||
| 
 | ||||
| 
 | ||||
| def setup_module(mod): | ||||
|     mod.tmp = py.test.ensuretemp(__name__)  | ||||
| 
 | ||||
| def test_simple_docteststring(): | ||||
|     testitem = DoctestText(name="dummy", parent=None) | ||||
|     testitem._setcontent(""" | ||||
|     p = tmp.join("test_simple_docteststring")  | ||||
|     p.write(py.code.Source(""" | ||||
|         >>> i = 0 | ||||
|         >>> i + 1 | ||||
|         1 | ||||
|     """) | ||||
|     """)) | ||||
|     testitem = py.test.collect.DoctestFile(p).join(p.basename)  | ||||
|     res = testitem.run() | ||||
|     assert res is None | ||||
|      | ||||
| def test_simple_docteststring_failing(): | ||||
|     testitem = DoctestText(name="dummy2", parent=None) | ||||
|     testitem._setcontent(""" | ||||
|     p = tmp.join("test_simple_docteststring_failing") | ||||
|     p.write(py.code.Source(""" | ||||
|         >>> i = 0 | ||||
|         >>> i + 1 | ||||
|         2 | ||||
|     """) | ||||
|     """)) | ||||
|     testitem = py.test.collect.DoctestFile(p).join(p.basename) | ||||
|     py.test.raises(Failed, "testitem.run()") | ||||
|     | ||||
| 
 | ||||
|  | @ -39,6 +44,6 @@ def test_collect_doctest_files_with_test_prefix(): | |||
|         col = config._getcollector(x) | ||||
|         items = list(col._tryiter(py.test.collect.Item)) | ||||
|         assert len(items) == 1 | ||||
|         assert isinstance(items[0], DoctestText) | ||||
|         assert isinstance(items[0].parent, py.test.collect.DoctestFile) | ||||
|     | ||||
|       | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue