question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Coverage Report is incorrect when there are multiple modules with same name but in different namespaces

See original GitHub issue
platform win32 -- Python 2.7.16, pytest-4.4.1, py-1.8.0, pluggy-0.11.0 -- c:\workspace\projects\pytest-dummy\.venv\scripts\python.exe
cachedir: .pytest_cache
rootdir: C:\workspace\projects\pytest-dummy
plugins: cov-2.7.1

This issue appears when there are some modules with the same name but in different namespaces within the same project or if you have code in init files. I have this dummy project with the following structure:

| /my-project/
|----| /Module1/
|----|-----| /tests/
|----|-----| __init__.py
|----|-----| MyFunctionality.py
|----| /Module2/
|----|-----| /tests/
|----|-----| __init__.py
|----|-----| MyFunctionality.py
|----| /Module3/
|----|-----| /tests/
|----|-----| __init__.py
|----|-----| AnotherFunctionality.py

If I run the following command: pytest -v --junitxml unittests.xml --cov=Module1 --cov=Module2 --cov=Module3 --cov-config .coveragerc --cov-report xml --cov-report term

Although the “term” Cobertura report shows correctly the coverage of all the modules:

Name                              Stmts   Miss  Cover
-----------------------------------------------------
Module1\MyFunctionality.py            5      5     0%
Module1\__init__.py                   0      0   100%
Module2\MyFunctionality.py            5      5     0%
Module2\__init__.py                   0      0   100%
Module3\AnotherFunctionality.py       5      5     0%
Module3\__init__.py                   0      0   100%
-----------------------------------------------------
TOTAL                                15     15     0%
Coverage XML written to file coverage.xml

The coverage.xml file includes only the coverage for two files:

<?xml version="1.0" ?>
<coverage branch-rate="0" branches-covered="0" branches-valid="0" complexity="0" line-rate="0" lines-covered="0" lines-valid="15" timestamp="1557386168872" version="4.5.3">
	<!-- Generated by coverage.py: https://coverage.readthedocs.io -->
	<!-- Based on https://raw.githubusercontent.com/cobertura/web/master/htdocs/xml/coverage-04.dtd -->
	<sources>
		<source>C:\workspace\projects\pytest-dummy\Module1</source>
		<source>C:\workspace\projects\pytest-dummy\Module2</source>
		<source>C:\workspace\projects\pytest-dummy\Module3</source>
	</sources>
	<packages>
		<package branch-rate="0" complexity="0" line-rate="0" name=".">
			<classes>
				<class branch-rate="0" complexity="0" filename="AnotherFunctionality.py" line-rate="0" name="AnotherFunctionality.py">
					<methods/>
					<lines>
						<line hits="0" number="1"/>
						<line hits="0" number="2"/>
						<line hits="0" number="3"/>
						<line hits="0" number="5"/>
						<line hits="0" number="6"/>
					</lines>
				</class>
				<class branch-rate="0" complexity="0" filename="MyFunctionality.py" line-rate="0" name="MyFunctionality.py">
					<methods/>
					<lines>
						<line hits="0" number="1"/>
						<line hits="0" number="2"/>
						<line hits="0" number="3"/>
						<line hits="0" number="5"/>
						<line hits="0" number="6"/>
					</lines>
				</class>
				<class branch-rate="0" complexity="0" filename="__init__.py" line-rate="1" name="__init__.py">
					<methods/>
					<lines/>
				</class>
			</classes>
		</package>
	</packages>
</coverage>

As you can see, the report only shows one “Functionality.py” coverage and on “init.py” where it should show all of them. If you try to parse this log with Jenkins using the Cobertura plugin, it will only show one file for every group of files that share name (and only one init file as well).

This issue was not reproduced in version pytest-cov==2.2.0, where it would treat the coverage of each file separately in the xml report even if the names where the same.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:4
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
xfenixcommented, Dec 26, 2019

Hello! I face same trouble (in CI pipeline between atlassian bamboo and sonarqube) and spent many hours for debug. Finally i googled this issue, but as i can understand, here is no clear explanation. So. Finally i found out following: if you have similar project structure (like described in issue), use whole folder, not apps/modules. For example, if you have module1, module2, module3 and use “–cov module1 --cov module2 --cov module3”, just replace it with “–cov .” and add some omit/restictions to skip necessary files. This helps me (sonarqube with previous coverage report show coverage at 46% for our project, now it shows real 97% (as you understands, we often repeat names in different packages - like helpers.py and other))

Explanation: as i understands (i may misunderstood logic), coverage get files from folders (“–cov module1 --cov module2 --cov module3”) and use paths as keys, so if you have same names, they obiously will be overriden in final report file, because it’s just flat list, without parent folders (parent folder can be found only at the top of report file (cubertura at least)).

It seems very obvious for expirienced users of coverage, but i was not like them (though i spent many years in it industry and many years i generated those reports, but faced this issue first time), so i decided to save time for any user who gets here

0reactions
ionelmccommented, Jan 12, 2020

Perhaps document this somewhere?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Webpack: "there are multiple modules with names that only ...
What's confusing is that the 'two' files referenced are just one file—there are no two files in the directory whose names differ only...
Read more >
Change history for coverage.py - Read the Docs
This applies to file names, module names, environment variables, and test contexts. Performance: Branch coverage measurement is faster, though you might only ......
Read more >
Improve Code Quality Using Test Coverage - CODE Magazine
When I get to the reporting capabilities of Coverage.py, I'll show an easy way to look for missing function calls. However, there's no...
Read more >
Customizing Code Coverage Analysis - Visual Studio (Windows)
Save the file with a name such as CodeCoverage.runsettings. ... The following table shows the various ways that assemblies and members can ...
Read more >
SWIG and C++ - SWIG.org
As a simple example, consider the problem of wrapping C++ multiple inheritance ... This means that the scripting proxy class AND another C++...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found