Python code coverage missing when files have the same name

Viewed 806

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!

1 Answers

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.

Related