在setup.py pytest.main中使用py.test --cov

时间:2015-05-04 12:18:00

标签: python pytest test-coverage

我正在开发一个包含一些测试的软件包。

使用CMD:

py.test --cov my_pkg

我用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%

失败:

尝试将其集成到pytest.main()并运行时使用:

python setup.py test

以下内容:

============================= 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

相关代码:

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)

问题:

如何在setup.py文件pytest.main内部使用覆盖率运行测试?

1 个答案:

答案 0 :(得分:10)

根据the documentation你应该这样做:

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

或:

self.pytest_args = "--cov my_pkg"