How to have pycharm recognizing .coveragerc file? - python

This question is not a duplicate of How do I configure PyCharm's Coverage checker to recognize .coveragerc? that is very old.
I have a project with a .coveragerc located at the root:
[run]
omit =
**/build/**
exclude_lines =
#abstractmethod
#abc.abstractmethod
pragma: no cover
I have configured the test suite like this:
However, in the PyCharm coverage window, I still get "0% files, not covered in build".
If I run pytest in the console, the coverage report generated is correct.
I have tried to put the .coveragerc file in the tests folder without better results.
Have you any idea what I should do differently here?
Thank you.

Related

CodeCov is ignoring certain files. No settings for ignore in YAML. Python/Django

I have a Python Django project on GitHub and I'm using CodeCov with this project.
I have two apps in this Django project, a general app, and a general_api app. For some reason, all of the changes made in the general_api app files are being ignored.
I had YAML settings like this to ignore my test cases:
codecov:
require_ci_to_pass: false
ignore:
- (?s:test_[^\/]+\.py.*)\Z
- (?s:tests_[^\/]+\.py.*)\Z
- ^test.py.*
- ^tests.py.*
However, I've removed them with the same issue.
Is there some other way to ignore, or set the ignore parameters in CodeCov other than the YAML settings?
The root of my problems was actually in how I was using coverage to generate my coverage report.
I was previously using the command line: coverage run -m pytest
This only creates a coverage report on files that have test cases against them or files that are interacted with while testing. Files that have no interaction or test cases would be completely omitted.
I found through the coverage documentation, that I needed to add --source=. to my command line if I wanted the coverage report to include untested files. This is now showing all files in my root source.
The final command was: coverage run --source=. -m pytest

Coverage-Gutter extension for visual studio code is not showing line coverage in python3 project

I installed coverage gutter extension for visual studio code but is not showing the line coverage, when I press Coverage Gutter display coverage or press the "watch" option in the footer it says "Could not find coverage file"
In this github doesn't mention anything about configuring a coverage file or anything
https://github.com/ryanluker/vscode-coverage-gutters
this is a code of one of my test I'm using unittest
class Test_SetValuesService(unittest.TestCase):
def test_given_none_property_when_checking_if_none_return_empty(self):
#ASSERT
self.assertEqual("&nbsp", setValuesService.check_if_json_property_is_null(""))
The error that I'm getting is a message that says "Could not find a Coverage file!"
By default, Coverage Gutter expectes to read the coverage data from a cov.xml file in the root of your project which contains the coverage data.
E.g. run this in the root folder of the project. It will run all tests within the test/ folder and will create a cov.xml
pytest --cov=. tests/ --cov-report xml:cov.xml
Of course you need to have the correct python plugins installed, which are "pytest", "coverage" and "pytest-cov" in this case.
You can also adjust the default filename which is used in the extension settings of coverage gutter within visual studio to sth. like mypersonalcovfile.xml if you like.
It looks like Coverage Gutters will read an lcov file or a .xml file. Have you generated a coverage.py xml file? For example, with coverage xml.

How can I make the application package available to the tests, when using py.test?

Suppose I'm writing a test. Obviously it is testing my app, so I need to import the app package somehow into the test script. The directory structure is this:
root/
app/
__init__.py
somemodule.py
tests/
my_test.py
And I run the tests like so:
cd tests
py.test # runs all the tests in the current directory
The question is: How should I import the application modules in my test modules?
In my_test.py, I tried doing from .. import app. This gives me an error Parent module '' not loaded, cannot perform relative import.
What is the standard way to accomplish this?
EDIT: Please note I edited the question to refer specifically to the py.test tool.
You should be able to make it run by properly configuring your py.test.
Add your module to your app/__init__.py the following line
from .somemodule import MyClass # or whatever ur class is called
Create a file called conftest.py in your main folder. You can leave it empty but it is used by py.test to find out the project path. Inside you can run some py.test initialization like adding fixtures.
In your my_test.py you will be able now to call
from app import MyClass
Now from your main folder you can finally:
py.test tests/test.py
This has worked for me. I think py.test has a way to include modules since you are probably not able to achieve the same without it. At least if I did not use py.test I would stick to modifying my PYTHONPATH to point to my application path.
EDIT:
Just to clarify py.test manipylates the sys.path for the testing session to include the root directory. Py.test identify the root path by using the conftest.py file. The root path is then added to the system path and used for testing.
You are indeed able to run:
py.test tests/test.py
and this would also work:
cd..
py.test rootTest/tests/test.py

Making py.test, coverage and tox work together: __init__.py in tests folder?

I'm having a weird problem with tox, py.test, coverage and pytest-cov: when py.test with the --cov option is launched from tox, it seems to require an __init__.py file in the tests folder which is not immediately obvious.
While writing this post, I have kind of solved the initial problem by adding the aforesaid tests/__init__.py, but to this moment I don't fully understand why exactly it works or doesn't work, so I'm still asking for help. Please see below for details.
I've found a related question on SO but it only makes it more confusing because the answer seems to be opposite to what I've figured out so far:
`py.test` and `__init__.py` files
See also the official docs here: py.test - Good Integration Practices (the very bottom of the page).
Simplified project structure:
setup.py
tox.ini
.coveragerc
project/
__init__.py
module1.py
module2.py
tests/
__init__.py (optional, an empty file)
test_module1.py
test_module2.py
Relevant part of tox.ini:
[testenv:check]
commands = py.test --cov=project --cov-report=term
deps =
pytest
coverage
pytest-cov
[pytest]
python_files = test_*.py
norecursedirs = .tox
Relevant part of .coveragerc:
[run]
branch = True
omit = project/tests/*
Now, the results:
py.test --cov=project --cov-report=term run from project root => correct coverage whether tests/__init__.py file is present or not.
tox -e check without tests/__init__.py => the tests are discovered and run, but I get a warning "Coverage.py warning: No data was collected." and the coverage is 0% for all modules
tox -e check with tests/__init__.py => correct coverage again.
It's not immediately obvious to me why the tests/__init__.py file has to be there (adding this empty file solved the initial problem) for the tox run, but it doesn't matter when you run the tests/coverage manually. Any ideas?
Use --cov {envsitepackagesdir}/<your-package-name> in tox.ini.
See:
Using py.test with coverage doesn't include imports
I got rid of using pytest-cov and run coverage outright instead..
Also noticed with pytest, I did need the blank __init__.py in my test directory to function correctly. There is probably a reason for it somewhere.
I realize this is a couple of years old, but in case someone else comes across this..

Is it possible exclude test directories from coverage.py reports?

I'm kind of a rookie with python unit testing, and particularly coverage.py. Is it desirable to have coverage reports include the coverage of your actual test files?
Here's a screenshot of my HTML report as an example.
You can see that the report includes tests/test_credit_card. At first I was trying to omit the tests/ directory from the reports, like so:
coverage html --omit=tests/ -d tests/coverage
I tried several variations of that command but I could not for the life of me get the tests/ excluded. After accepting defeat, I began to wonder if maybe the test files are supposed to be included in the report.
Can anyone shed some light on this?
coverage html --omit="*/test*" -d tests/coverage
Create .coveragerc file in your project root folder, and include the following:
[run]
omit = *tests*
Leaving this here in case if any Django developer needs a .coveragerc for their project.
[run]
source = .
omit = ./venv/*,*tests*,*apps.py,*manage.py,*__init__.py,*migrations*,*asgi*,*wsgi*,*admin.py,*urls.py
[report]
omit = ./venv/*,*tests*,*apps.py,*manage.py,*__init__.py,*migrations*,*asgi*,*wsgi*,*admin.py,*urls.py
Create a file named .coveragerc on your projects root directory, paste the above code and then just run the command:
coverage run manage.py test
In addition, if you want the tests to execute faster run this command instead.
coverage run manage.py test --keepdb --parallel
This will preserve the test DB and will run the tests in parallel.
You can specify the directories you want to exclude by creating a .coveragerc in the project root.
It supports wildcards (which you can use to exclude virtual environment) and comments (very useful for effective tracking).
The below code block shows how omit can be used (taken from the latest documentation) with multiple files and directories.
[run]
omit =
# omit anything in a .local directory anywhere
*/.local/*
# omit everything in /usr
/usr/*
# omit this single file
utils/tirefire.py
In your case, you could have the following in your .coveragerc:
[run]
omit =
# ignore all test cases in tests/
tests/*
For your question on coverage reports, you can think about testing and coverage in the following manner:
When you run pytest or unittest, all the test cases for your source code are executed
When you run coverage, it shows the part of the source code that isn't being used.
When you run pytest with coverage (something like pytest -v --cov), it runs all test cases and shows the part of the source code that isn't being used.
Extra:
You can also specify the location of your HTML report in the configuration file like:
[html]
directory = tests/coverage/html_report/
This is going to create html, js, css, etc. inside tests/coverage/html_report/ everytime you run coverage or pytest -v --cov
You can also explicitly specify which directory has the code you want coverage on instead of saying which things to omit. In a .coveragerc file, if the directory of interest is called demo, this looks like
[run]
source = demo

Categories