Python 使用py.test--cov从内部setup.py pytest.main

Python 使用py.test--cov从内部setup.py pytest.main,python,pytest,test-coverage,Python,Pytest,Test Coverage,我正在开发一个包含一些测试的软件包 使用CMD: py.test --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_ar

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

使用CMD:

py.test --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)
我通过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%
失败:

py.test --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)
尝试将其集成到
pytest.main()
中并运行时:

python setup.py测试

以下是:

============================= 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
相关代码:

py.test --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)
问题:

py.test --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
中运行覆盖率为的测试?

根据您应该做的:

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


您是否尝试只传递一个字符串
self.pytest_args=“--cov my_pkg”
或将它们作为两个参数传递:
self.pytest_args=[“--cov”,“my_pkg”]
?如果使用
setup.cfg
,您可以将
addopts=--cov=my_pkg
添加到[tool:pytest]部分。注意:节的正确名称取决于
pytest
的版本,因此,请检查