Commits

Meme Dough  committed 8ddf5de

Move funcarg to module level so visible with py.test --funcargs.

  • Participants
  • Parent commits 92c486f

Comments (0)

Files changed (2)

File pytest_cov.py

 No doubt others have contributed to these tools as well.
 """
 
+
 def pytest_addoption(parser):
     """Add options to control coverage."""
 
     """Activate coverage plugin if appropriate."""
 
     if config.getvalue('cov_source'):
-        config.pluginmanager.register(CovPlugin(config), '_cov')
+        config.pluginmanager.register(CovPlugin(), '_cov')
 
 
 class CovPlugin(object):
     distributed slave.
     """
 
-    def __init__(self, config):
+    def __init__(self):
         """Creates a coverage pytest plugin.
 
         We read the rc file that coverage uses to get the data file
 
         self.cov_controller.summary(terminalreporter._tw)
 
-    def pytest_funcarg__cov(self, request):
-        """A pytest funcarg that provide access to the underlying coverage object."""
 
-        if self.cov_controller:
-            return self.cov_controller.cov
-        else:
-            return None
+def pytest_funcarg__cov(request):
+    """A pytest funcarg that provides access to the underlying coverage object."""
+
+    # Check with hasplugin to avoid getplugin exception in older pytest.
+    if request.config.pluginmanager.hasplugin('_cov'):
+        plugin = request.config.pluginmanager.getplugin('_cov')
+        if plugin.cov_controller:
+            return plugin.cov_controller.cov
+    return None

File test_pytest_cov.py

 """
 
 import py
-import sys
 import os
 
 pytest_plugins = 'pytester', 'cov'
     out, err = subprocess.Popen([sys.executable, 'child_script.py', str(idx)], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
 '''
 
+SCRIPT_FUNCARG = '''
+import coverage
+
+def test_foo(cov):
+    assert isinstance(cov, coverage.control.coverage)
+'''
+
+SCRIPT_FUNCARG_NOT_ACTIVE = '''
+def test_foo(cov):
+    assert cov is None
+'''
+
 
 def test_central(testdir):
     script = testdir.makepyfile(SCRIPT)
             '*- coverage: failed slaves -*'
             ])
     assert result.ret == 0
+
+
+def test_funcarg(testdir):
+    script = testdir.makepyfile(SCRIPT_FUNCARG)
+
+    result = testdir.runpytest('-v',
+                               '--cov=%s' % script.dirpath(),
+                               '--cov-report=term-missing',
+                               script)
+
+    result.stdout.fnmatch_lines([
+            '*- coverage: platform *, python * -*',
+            'test_funcarg * 3 * 100%*',
+            '*1 passed*'
+            ])
+    assert result.ret == 0
+
+
+def test_funcarg_not_active(testdir):
+    script = testdir.makepyfile(SCRIPT_FUNCARG_NOT_ACTIVE)
+
+    result = testdir.runpytest('-v',
+                               script)
+
+    result.stdout.fnmatch_lines([
+            '*1 passed*'
+            ])
+    assert result.ret == 0