Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
552 views
in Technique[技术] by (71.8m points)

pytest - Python code coverage missing when files have the same name

I'm using pytest and pytest-cov for code coverage.

My project looks as following. There're two modules services and workers.

python_tmp
├── tests
│?? └── some_test.py
├── services
│?? └── utils.py
└── workers
    └── utils.py

Both of them have utils.py inside. But the content are different.

services/utils.py

def function_1():
    return 1

and workers/utils.py

def function_2():
    return 2

In tests/some_test.py you can find the test cases.

import pytest
import services.utils as t1
import workers.utils as t2

def test_function_1():
    res = t1.function_1()
    assert res == 1

def test_function_2():
    res = t2.function_2()
    assert res == 2

If I run the test python -m pytest -v tests --cov=services --cov=workers --cov-report=xml:coverage.xml, it will generate following report. But you can see there's only one utils.py been reported, and there's ambiguity that you don't know which module it belongs to.

...
    <sources>
        <source>/Users/xxxxxxx/workspace/tmp/python_tmp/services</source>
        <source>/Users/xxxxxxx/workspace/tmp/python_tmp/workers</source>
    </sources>
    <packages>
        <package branch-rate="0" complexity="0" line-rate="1" name=".">
            <classes>
                <class branch-rate="0" complexity="0" filename="utils.py" line-rate="1" name="utils.py">
                    <methods/>
                    <lines>
                        <line hits="1" number="1"/>
                        <line hits="1" number="2"/>
                    </lines>
                </class>
            </classes>
        </package>
    </packages>
</coverage>

How to fix it? Thanks for your time!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This issue is mentioned here

So to fix this you can run python -m pytest -v tests --cov --cov-report=xml:coverage.xml.

Hopefully this will help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...