Commits

Antonio Cuni  committed def9029

add the possibility to write applevel tests inside the docstring (only if the body of the method is empty. This way we can write tests using py3k only syntax and still run them on top of cpython 2

  • Participants
  • Parent commits f1dde39
  • Branches py3k

Comments (0)

Files changed (3)

File pypy/conftest.py

             return target()
         space = target.im_self.space
         filename = self._getdynfilename(target)
-        func = app2interp_temp(target.im_func, filename=filename)
+        src = extract_docstring_if_empty_function(target.im_func)
+        func = app2interp_temp(src, filename=filename)
         w_instance = self.parent.w_instance
         self.execute_appex(space, func, space, w_instance)
 
+def extract_docstring_if_empty_function(fn):
+    def empty_func():
+        ""
+        pass
+    co_code = fn.func_code.co_code
+    if co_code == empty_func.func_code.co_code and fn.__doc__ is not None:
+        head = '%s(self):' % fn.func_name
+        body = py.code.Source(fn.__doc__)
+        return head + str(body.indent())
+    else:
+        return fn
+
+
 class PyPyClassCollector(py.test.collect.Class):
     def setup(self):
         cls = self.obj

File pypy/tool/pytest/test/conftest1_innertest.py

 
 class AppTestSomething: 
     def test_method_app(self): 
-        assert 23 == 23 
+        assert 23 == 23
+
+    def test_code_in_docstring_failing(self):
+        """
+        assert False # failing test
+        """
+
+    def test_code_in_docstring_ignored(self):
+        """
+        this docstring is not parsed as code because the function body is not
+        empty
+        """
+        assert True
+
     
 class TestSomething:
     def test_method(self): 

File pypy/tool/pytest/test/test_conftest1.py

     def test_selection_by_keyword_app(self, testdir): 
         sorter = testdir.inline_run("-k", "applevel", innertest)
         passed, skipped, failed = sorter.listoutcomes()
-        assert len(passed) == 2
-        assert not skipped and not failed 
+        assert len(passed) == 3
+        assert len(failed) == 1
+        assert skipped == []
         assert "app_test_something" in passed[0].nodeid
         assert "test_method_app" in passed[1].nodeid
-
+        assert "test_code_in_docstring_ignored" in passed[2].nodeid
+        assert "test_code_in_docstring_failing" in failed[0].nodeid
+        
     def test_runappdirect(self, testdir):
         sorter = testdir.inline_run(innertest, '-k', 'applevel', '--runappdirect')
         passed, skipped, failed = sorter.listoutcomes()
-        assert len(passed) == 2
+        assert len(passed) == 3
         print passed
         assert "app_test_something" in passed[0].nodeid
         assert "test_method_app" in passed[1].nodeid
         sorter = testdir.inline_run(innertest, '-k', 'applevel',
                                     '--appdirect=%s' % (sys.executable,))
         passed, skipped, failed = sorter.listoutcomes()
-        assert len(passed) == 2
+        assert len(passed) == 3
         print passed
         assert "app_test_something" in passed[0].nodeid
         assert "test_method_app" in passed[1].nodeid
         
+    def test_docstrings(self, testdir): 
+        sorter = testdir.inline_run("-k", "test_code_in_docstring", innertest)
+        passed, skipped, failed = sorter.listoutcomes()
+        assert len(passed) == 1
+        assert len(failed) == 1
+        assert skipped == []
+        assert "test_code_in_docstring_ignored" in passed[0].nodeid
+        assert "test_code_in_docstring_failing" in failed[0].nodeid