Using py.test with coverage doesnt include imports

2024/11/20 1:37:55

For Jedi we want to generate our test coverage. There is a related question in stackoverflow, but it didn't help.

We're using py.test as a test runner. However, we are unable to add the imports and other "imported" stuff to the report. For example __init__.py is always reported as being uncovered:

Name                           Stmts   Miss  Cover
--------------------------------------------------
jedi/__init__                      5      5     0%
[..]

Clearly this file is being imported and should therefore be reported as tested.

We start tests like this [*]:

py.test --cov jedi

As you can see we're using pytest-coverage.

So how is it possible to properly count coverage of files like __init__.py?

[*] We also tried starting test without --doctest-modules (removed from pytest.ini) and activate the coverage module earlier by py.test -p pytest_cov --cov jedi. Neither of them work.

I've offered a bounty. Please try to fix it within Jedi. It's publicly available.

Answer

@hynekcer gave me the right idea. But basically the easiest solution lies somewhere else:

Get rid of pytest-cov!

Use

coverage run --source jedi -m py.test
coverage report

instead!!! This way you're just running a coverage on your current py.test configuration, which works perfectly fine! It's also philosophically the right way to go: Make each program do one thing well - py.test runs tests and coverage checks the code coverage.

Now this might sound like a rant, but really. pytest-cov hasn't been working properly for a while now. Some tests were failing, just because we used it.


As of 2014, pytest-cov seems to have changed hands. py.test --cov jedi test seems to be a useful command again (look at the comments). However, you don't need to use it. But in combination with xdist it can speed up your coverage reports.

https://en.xdnf.cn/q/26375.html

Related Q&A

Determine if a Python class is an Abstract Base Class or Concrete

My Python application contains many abstract classes and implementations. For example:import abc import datetimeclass MessageDisplay(object):__metaclass__ = abc.ABCMeta@abc.abstractpropertydef display(…

Pandas printing ALL dtypes

This seems like a very simple problem, however its driving me round the bend. Im sure it should be solved by RTFM, but Ive looked at the options and I can see the one to fix it.I just want to print the…

Which Python API should be used with Mongo DB and Django

I have been going back and forth over which Python API to use when interacting with Mongo. I did a quick survey of the landscape and identified three leading candidates.PyMongo MongoEngine MingIf you w…

Save a list to a .txt file

Is there a function in python that allows us to save a list in a txt file and keep its format?If I have the list:values = [1,2,3]can I save it to a file that contains:[1,2,3]So far I print parts of th…

How to run a python script like pm2 for nodejs

Ive used pm2 for my Node.js script and I love it. Now I have a python script which collect streaming data on EC2. Sometimes the script bombs out and I would like a process manager to restart itself lik…

Resize rectangular image to square, keeping ratio and fill background with black

Im trying to resize a batch of grayscale images that are 256 x N pixels (N varies, but is always ≤256). My intention is to downscale the images.The resize would have to output a square (1:1) image, wi…

How to capture arbitrary paths at one route in FastAPI?

Im serving React app from FastAPI by mounting app.mount("/static", StaticFiles(directory="static"), name="static")@app.route(/session) async def renderReactApp(request: Re…

Changing the active class of a link with the twitter bootstrap css in python/flask

I got the following html snippet from my page template.html.<ul class=nav><li class="active"><a href=/>Home</a></li><li><a href=/lorem>Lorem</a>…

overwriting a spark output using pyspark

I am trying to overwrite a Spark dataframe using the following option in PySpark but I am not successfulspark_df.write.format(com.databricks.spark.csv).option("header", "true",mode=…

How to patch a constant in python

I have two different modules in my project. One is a config file which containsLOGGING_ACTIVATED = FalseThis constant is used in the second module (lets call it main) like the following:if LOGGING_ACTI…