6

I'm developing a package with some testing.

Working with CMD:

py.test --cov my_pkg

I get the results with covarage:

--------------- coverage: platform win32, python 2.7.9-final-0 ----------------
Name                            Stmts   Miss  Cover
---------------------------------------------------
my_pkg\__init__       8      0   100%
my_pkg\general        2      0   100%
---------------------------------------------------
TOTAL                              10      0   100%

Fails:

when trying to integrate it inside pytest.main() and running with:

python setup.py test

with the following:

============================= test session starts =============================
platform win32 -- Python 2.7.9 -- py-1.4.26 -- pytest-2.7.0
rootdir: C:\Users\kobi.kalif\Projects\automation_utilities, inifile:
plugins: cov, xdist

ERROR: file not found: --cov my_pkg

Relevant Code:

class PyTest(test_command):
    """class py.test for the testing

    """
    user_options = []

    def __init__(self, dist, **kw):
        test_command.__init__(self, dist, **kw)
        self.pytest_args = ["--cov my_pkg"]

.....

    def run_tests(self):
        # import here, cause outside the eggs aren't loaded
        import pytest
        err_no = pytest.main(self.pytest_args)
        sys.exit(err_no)

Question:

How can i run tests with coverage from inside the setup.py file pytest.main?

Kobi K
  • 7,743
  • 6
  • 42
  • 86
  • 1
    Have you tried passing just a string `self.pytest_args = "--cov my_pkg"` or passing them as two arguments: `self.pytest_args = ["--cov", "my_pkg"]`? – musically_ut May 04 '15 at 12:20
  • If you use `setup.cfg`, you could add `addopts = --cov=my_pkg` to the section **[tool:pytest]**. Be careful: the correct name of section depends on version of `pytest`, so, please, check the [documentation](http://doc.pytest.org/en/latest/customize.html#command-line-options-and-configuration-file-settings) – maxkoryukov Aug 26 '16 at 19:09

1 Answers1

11

According to the documentation you should either do:

self.pytest_args = ["--cov", "my_pkg"]

or:

self.pytest_args = "--cov my_pkg"
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437