[py-svn] r61754 - in py/branch/pytestplugin/py/test: . plugin testing

hpk at codespeak.net hpk at codespeak.net
Wed Feb 11 21:15:47 CET 2009


Author: hpk
Date: Wed Feb 11 21:15:45 2009
New Revision: 61754

Modified:
   py/branch/pytestplugin/py/test/config.py
   py/branch/pytestplugin/py/test/plugin/pytest_pytester.py
   py/branch/pytestplugin/py/test/pmanage.py
   py/branch/pytestplugin/py/test/testing/test_pmanage.py
Log:
smallr refactoring to allow conftests to add plugins as early as possible
in order to have extended cmdlineoptions available. 


Modified: py/branch/pytestplugin/py/test/config.py
==============================================================================
--- py/branch/pytestplugin/py/test/config.py	(original)
+++ py/branch/pytestplugin/py/test/config.py	Wed Feb 11 21:15:45 2009
@@ -48,6 +48,7 @@
         self._initialized = True
         adddefaultoptions(self)
         self._conftest.setinitial(args)
+        self.pluginmanager.setinitial(self._conftest.getconftestmodules(None))
         self.pluginmanager.add_cmdlineoptions(self)
         args = [str(x) for x in args]
         cmdlineoption, args = self._parser.parse_args(args) 

Modified: py/branch/pytestplugin/py/test/plugin/pytest_pytester.py
==============================================================================
--- py/branch/pytestplugin/py/test/plugin/pytest_pytester.py	(original)
+++ py/branch/pytestplugin/py/test/plugin/pytest_pytester.py	Wed Feb 11 21:15:45 2009
@@ -13,6 +13,10 @@
         self.outlines = outlines
         self.errlines = errlines
 
+    def assert_contain_lines(self, lines):
+        from py.__.test.testing.suptest import assert_lines_contain_lines
+        assert_lines_contain_lines(self.outlines, lines)
+
 class FileCreation(object):
     def makepyfile(self, **kwargs):
         return self._makefile('.py', **kwargs)
@@ -61,6 +65,8 @@
 
     def _writeconftest(self):
         p = self.tmpdir.join("conftest.py") 
+        if p.check():
+            return # don't overwrite
         pstring = repr(self._plugins)
         p.write("import py ; pytest_plugins = %s" % pstring)
         #else:

Modified: py/branch/pytestplugin/py/test/pmanage.py
==============================================================================
--- py/branch/pytestplugin/py/test/pmanage.py	(original)
+++ py/branch/pytestplugin/py/test/pmanage.py	Wed Feb 11 21:15:45 2009
@@ -64,15 +64,16 @@
     #
     # API for interacting with registered and instantiated plugin objects 
     #
-    def add_cmdlineoptions(self, config):
-        #for mod in self.config._conftest.getconftestmodules(None):
-        #    self.consider_module(mod)
+    def setinitial(self, modules):
+        for module in modules:
+            self.consider_module(module)
         envspec  = py.std.os.environ.get("PYTEST_PLUGINS", None)
         if envspec:
             for spec in map(str.strip, envspec.split(",")):
                 if spec:
                     self.import_plugin(spec)
-          
+
+    def add_cmdlineoptions(self, config):
         # XXX think about sorting/grouping of options from user-perspective 
         opts = []
         for name, options in self.listattr("pytest_cmdlineoptions"):

Modified: py/branch/pytestplugin/py/test/testing/test_pmanage.py
==============================================================================
--- py/branch/pytestplugin/py/test/testing/test_pmanage.py	(original)
+++ py/branch/pytestplugin/py/test/testing/test_pmanage.py	Wed Feb 11 21:15:45 2009
@@ -4,7 +4,7 @@
 from py.__.test.event import NOP
 from py.__.test.config import Config as pytestConfig
 
-class TestPluginManager:
+class TestInitialization:
     def setup_method(self, method):
         self.tmpdir = py.test.ensuretemp("%s.%s.%s" %
             (__name__, self.__class__.__name__, method.__name__))
@@ -107,6 +107,7 @@
         myplugin2 = pm.getplugin("Plugin") 
         assert myplugin1 is myplugin2 
 
+class TestPluginInteraction:
     def test_callplugins(self):
         pm = PluginManager()
         class My:
@@ -187,15 +188,34 @@
         assert opt
         assert opt.default == 242
 
-    def test_addcmdlineoptions_considers_ENV(self):
+    def test_setinitial_env(self):
         pm = PluginManager()
-        SPEC = "PYTEST_PLUGINS"
-        old = os.environ.get(SPEC, None)
+        KEY = "PYTEST_PLUGINS"
+        old = os.environ.get(KEY, None)
         try:
-            os.environ[SPEC] = "test_addcmdlineoptions_considers_ENV"
-            py.test.raises(ImportError, "pm.add_cmdlineoptions(pytestConfig())")
+            os.environ[KEY] = "test_setinitial"
+            py.test.raises(ImportError, "pm.setinitial([])")
         finally:
             if old is None: 
-                del os.environ[SPEC]
+                del os.environ[KEY]
             else:
-                os.environ[SPEC] = old 
+                os.environ[KEY] = old 
+
+    def test_conftest_specifies_plugin(self, fstester):
+        fstester.makepyfile(
+            conftest="""
+                import py
+                class MyPlugin:
+                    pytest_cmdlineoptions = [
+                        py.test.config.Option("--myplugin-option", dest="myplugin", 
+                        help="myplugin option",
+                        )
+                    ]
+                pytest_plugins = MyPlugin
+            """
+        )
+        result = fstester.runpytest(fstester.tmpdir, '-h')
+        result.assert_contain_lines([
+            "*--myplugin-option*", 
+        ])
+



More information about the pytest-commit mailing list