Python coverage report covering only test file - python

I’m pretty new to contributing to open source projects and am trying to get some coverage reports so I can find out what needs more / better testing. However, I am having trouble getting the full coverage of a test. This is for pytorch
For example, lets say I want to get the coverage report of test_indexing_py.
I run the command:
pytest test_indexing.py --cov=../ --cov-report=html
Resulting in this:
================================================= test session starts =================================================
platform win32 -- Python 3.7.4, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: C:\Projects\pytorch
plugins: hypothesis-5.4.1, arraydiff-0.3, cov-2.8.1, doctestplus-0.4.0, openfiles-0.4.0, remotedata-0.3.2
collected 62 items
test_indexing.py ............................s................................. [100%]
----------- coverage: platform win32, python 3.7.4-final-0 -----------
Coverage HTML written to dir htmlcov
=========================================== 61 passed, 1 skipped in 50.43s ============================================
Ok, looks like the tests ran. Now when I check the html coverage report, I only get the coverage for the test file and not for the classes tested (the tests are ordered by coverage percentage).
As you can see, I am getting coverage for only test_indexing.py. How do I get the full coverage report including the classes tested?
Any guidance will be greatly appreciated.

I think its because you are asking to check the coverage from the test running directory, ie where test_indexing.py is.
A better approach would be like running the test from the root directory itself, rather than test directory, it has several advantages like the configuration file reading and all.
And regarding your question, try running the test from the root directory and try
pytest path/to/test/ --cov --cov-report=html

Related

pytest --cov is telling me I haven't imported something that I have

Here's what I run, and what I get:
me $ pytest --cov MakeInfo.py
================================================================================ test session starts =================================================================================
platform darwin -- Python 3.7.4, pytest-6.2.5, py-1.10.0, pluggy-0.13.0
rootdir: /Users/me/Documents/workspace-vsc/Pipeline/src/python
plugins: cov-2.12.1, arraydiff-0.3, remotedata-0.3.2, doctestplus-0.4.0, openfiles-0.4.0
collected 42 items
test_MakeInfo.py ............ [ 28%]
test_MakeJSON.py ... [ 35%]
test_convert_refflat_to_bed.py .. [ 40%]
test_generate_igv_samples.py ... [ 47%]
test_sample_info_to_jsons.py .... [ 57%]
read_coverage/test_intervaltree.py ......... [ 78%]
util/test_util.py ......... [100%]**Coverage.py warning: Module MakeInfo.py was never imported**. (module-not-imported)
Coverage.py warning: No data was collected. (no-data-collected)
WARNING: Failed to generate report: No data to report.
/Users/me/opt/anaconda3/lib/python3.7/site-packages/pytest_cov/plugin.py:271: PytestWarning: Failed to generate report: No data to report.
self.cov_controller.finish()
Here's the top of test_MakeInfo.py:
import pytest
import os
import sys
import json
import MakeInfo
from MakeInfo import main, _getNormalTumorInfo
What I'm looking for: Tell me how much of MakeInfo.py is covered by tests in test_MakeInfo.py
What I'm getting: confused
Is my command wrong for what I want? Nothing calls into MakeInfo.py, it's stand alone and called from the command line. So of course none of the other tests are including it.
Is there a way to tell pytest --cov to look at this test file, and the source file, and ignore everything else?
Try changing the call to just the python module name MakeInfo instead of the file name MakeInfo.py. Or if it is in an inner subdirectory, use the dot . notation e.g. some_dir.some_subdir.MakeInfo
pytest --cov MakeInfo
So, it turns out that in this case the order of arguments is key
pytest test_MakeInfo.py --cov
Runs pytest on my one test file, and gives me coverage information for the one source file
pytest --cov test_MakeInfo.py
Tries to run against every test file (one of which was written by someone else on my team, and apparently uses a different test tool, so it throws up errors when pytest tries to run it)
pytest --cov MakeInfo
Is part way there: it runs all the tests, including the ones that fail, but then gives me the coverage information I want
So #Niel's answer is what you want if you have multiple test files targeting a single source file

pytest-cov options for code coverage from external libraries

Currently I'm trying to implement an automation testing tool for python projects. Is it possible to collect the code coverage from external libraries using pytest-cov module? As far as I know only the coverage module will report the code coverage from external libraries!
Example:
import random
def test_rand():
assert random.randint(0,10) == 5
Using the command coverage run -m --pylib pytest file.py::test_rand we can get the code coverage from external libraries (e.g. random module in our case).
Is it possible to do the same thing using pytest-cov instead?
By default pytest-cov will report coverage for all libraries, including external.
If you run pytest --cov against your code it will produce many lines of coverage including py, pytest, importlib, etc.
To limit the scope of the coverage, i.e. you only want to inspect coverage for random, just pass the module names to the cov option e.g. pytest --cov=random. The coverage report then only considers the named modules. You can also pass multiple modules by specifying multiple cov values, e.g. pytest --cov=random --cov=pytest
Here's an example running your test to produce coverage only against random
$ pytest --cov=random
====== test session starts ======
platform linux -- Python 3.6.12, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
plugins: cov-2.12.1
collected 1 item
test_something.py F
[100%]
=========== FAILURES ============
___________ test_rand ___________
def test_rand():
import random
> assert random.randint(0,10) == 5
E AssertionError: assert 0 == 5
E + where 0 = <bound method Random.randint of <random.Random object at ...>>(0, 10)
test_something.py:6: AssertionError
---------- coverage: platform linux, python 3.6.12-final-0 -----------
Name Stmts Miss Cover
/.../random.py 350 334 --
TOTAL 350 334 5%

Pytest not detecting FizzBuzzTest.py

This is my code in FizzBuzzTest.py
import pytest
# content of test_sample.py
def fizzBuzz(value):
return value
def test_returns1With1PassedIn():
assert fizzBuzz(1) == 1
When I run pytest -v in the command line
================================= 1 passed in 0.05 seconds ===================================================================
PS C:\Users\pytest\FizzBuzz_Kata> pytest -v
======================================== test session starts ================================================================================================
platform win32 -- Python 3.5.2, pytest-4.4.1, py-1.8.0, pluggy-0.11.0 -- c:\users\a606143\appdata\local\programs\python\python35\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\pytest\FizzBuzz_Kata
collected 0 items
Could somebody please explain why pytest is not able to detect this file and run tests?
I have fiddled a little around and I seem to have found a solution, a little odd but it is working.
To have pytest to detect the file, it needs to be name with test_ infront of the filename, so your file should be named: test_FizzBussTest.py and your function which pytest have to execute needs to have that exact same name, so your function needs to be named as such:
def test_FizzBussTest():
Edit: After further researched, the function doesnt need to be named exactly as the file, it just needs to have test_ infront of the function name so fx test_sum():

Py.Test aborts when (py)ROOT is imported

I've a problem unit testing Python code, which uses the (py)ROOT package. It simply aborts collecting the tests when there is a line import ROOT in one of the scripts.
Does anybody have an idea what's going on?
foo#bar ~/project/dir [19:21:17]
(project)> $ py.test -v [±master ●]
============================= test session starts ==============================
platform darwin -- Python 2.7.6 -- py-1.4.25 -- pytest-2.6.3 -- /path/to/.virtualenvs/project/bin/python
collecting 0 items[1] 16520 abort py.test -v
I had a similar issue by adding the PyROOT bindings to export $PYTHONPATH in my .bashrc. The locally set PYTHONPATH was ignored. A solution is copying or linking the ROOT.py and libPyROOT.so files to the appropriate Python directory in /usr/lib. More details in this answer: https://stackoverflow.com/a/33130000/4753851

PyCharm, Django: zero code coverage

PyCharm has a "Run with Coverage" action for Django test targets. This runs the tests, but shows zero test coverage (0% files, not covered in the project pane, and all red in the editor). Checking or unchecking "Use bundled coverage.py" makes no difference.
Running the same tests from the CLI gives the expected results:
$ coverage --version
Coverage.py, version 3.5.1. http://nedbatchelder.com/code/coverage
$ coverage run ./manage.py test blackbox
Creating test database for alias 'default'...
....
----------------------------------------------------------------------
Ran 4 tests in 0.002s
OK
Destroying test database for alias 'default'...
$ coverage report
Name Stmts Miss Cover
---------------------------------------------
__init__ 0 0 100%
blackbox/__init__ 0 0 100%
blackbox/models 5 0 100%
blackbox/rules/__init__ 1 0 100%
blackbox/rules/board 62 19 69%
blackbox/tests 49 6 88%
manage 11 4 64%
settings 24 0 100%
---------------------------------------------
TOTAL 152 29 81%
What could cause this?
If you access your project via any symlink in the path, coverage display will fail.
Try to open same project through real path, and you will get correct behavior.
https://youtrack.jetbrains.com/issue/PY-17616
PS: Refreshing old question, as bug still has not been fixed.
I had a similar issue using the PyCharm bundled coverage.py
The tests were running fine, but the coverage results were not loaded, "0%" or "not covered" everywhere.
There was an error logged in the PyCharm console though, following the output of the tests, that was coverage.py related:
/System/Library/Frameworks/Python.framework/Versions/2.6/bin/python
"/Applications/PyCharm 2.5 EAP.app/helpers/run_coverage.py"
run "--omit=/Applications/PyCharm 2.5 EAP.app/helpers" bin/test
Creating test database for alias 'default'...
................................
----------------------------------------------------------------------
Ran xx tests in xxs
OK
No source for code: '/path/file.py' (<- error)
Process finished with exit code 0
My solution was to run the bundled coverage.py with the option to ignore errors: "-i".
I've edit the bundled "run_coverage.py" file, you can see its location in the console output, and add the "-i" option in the last line:
main(["xml", "-o", coverage_file + ".xml"])
to:
main(["xml", "-i", "-o", coverage_file + ".xml"])
This worked for me, the error is ignored and all the coverage data are now loaded in the UI.
If using "-i" solve the issue on your side, a better solution would be to fix the errors, but until then, you'll see coverage results.
I've also been trying to solve this issue in Ubuntu.
At the moment I tried with both the apt-get Python and the Enthought Canopy stack, with no success. In Windows however it does work (using Canopy).
I've used the following code:
# in a.py
class A(object):
def p(self, a):
return a
# in test_a.py
from unittest import TestCase, main
from a import A
class TestA(TestCase):
def test_p(self):
inst = A()
val = inst.p("a")
self.assertEqual("a", val)
if _name_ == "__main__":
main()
I had a similar issue. I ended up generating xml data using nosetests --cover-xml, but you can also generate an xml from a previous coverage.py run with coverage xml
Then that report can be conveniently loaded in PyCharm/IDEA from the Analyze -> Show Coverage Data… -> + button and selecting the xml file.

Categories

Resources