How to ignore / omit / exclude files during test coverage? - python

In order to get 100% test coverage, I need to ignore some file(s) in python.
I searched the web and I found nosetests which I don't want to use.
I also found that I can edit my .coveragerc file and omit files and functions, when running my tests using intellij (with unittest framework), it didn't manage to use .coveragerc file.
Any idea how to ignore / omit / exclude files during test coverage ?
How can I run the test using this file as a parameter ?

You can use this command in your .coveragerc file.
# .coveragerc
[report]
show_missing = True
omit =
junk/*
You include the path of files you want to omit under the omit command, for example, I want to omit every file in the junk folder hence my use of junk/*.

To add to proton's answer.
You can also use the # pragma: no cover commenting specific clauses. You can comment every clause in a file to exclude the content of the file. This is more tedious but gives you finer control if you want to partially ignore a file.
Here is a link to the coverage.py project that discuses coverage exclusion in further detail:
https://coverage.readthedocs.io/en/7.0.0/excluding.html
I would have commented on proton's answer instead of making a new answer, but I do not have enough reputation. More so, I would have suggested an edit, but there are too many pending edits and stack-overflow will not let me add another.

Related

VS code, django project have template errors, how to avoid them?

I have use autopep8 to check my Python error & warning in VS Code, and config the "Go to Next Problem in Files" to fast jump to the next one.
One day, I added one html file, found that the error list is long.
I do not care the html's error, cuz it is just test files. How can I avoid them?
I found one solution is that disable the HTML>Validate. Then the errors just from Python.
You could head to the settings.json and add this which will ignore validations in HTML files:
"html.validate.scripts": false,

Excluding line of code from pybuilder coverage

While PyBuilder Coverage is great excluding files, I could not find a property to set or any other way to exclude specific function or line of code.
For example I have a RESTFUL service that unittest tests, but i also have some functions outise of tested classes that I don't want to test explicitly. if that would be a standalone Coverage execution then it would be possible to change Coverage configuration file .coveragerc to something with below
[report]
exclude_lines = def MyUndesiredToBeTestedFunction
However, I could not find no obvious access to it from PyBuilder.
Any help of in finding Coverage configuration file .coveragerc that is used by PyBuilder run or other solution is welcomed!
for me it works if I place the .coveragerc file in the project root of my pybuilder project (pybuilder verion 0.11.10) with the following example content:
[report]
# Regexes for lines to exclude from consideration
exclude_lines =
# Have to re-enable the standard pragma
pragma: no cover
# custom
def __[a-zA-Z]+\(
Tips:
assure your regex is correct
try with the .* regex (coverage should then be 100%)

How to rename files in the folder output report (robotframework, ride)

When I have a few test cases, generates the output folder files to me:
For testing I use robotframework and pycharm
log.html
output.xml
report.xml
After each test, the files are overwritten.
Is there a possibility that the names of these files after the match in my tests, so I do not have to change their names or create separate folders for each test - for example:
log_test1.html
output_test1.xml
report_test1.xml
Whether to use some parameters that will take me the name of the test and passed it on to the name of the output file?
Please help how can I set this up using pycharm
Regards,
All Robot Framework output files can be automatically timestamped with the option --timestampoutputs:
pybot --timestampoutputs tests.html
See User Guide section "Timestamping output files"

How can I configure Sphinx to conditionally exclude some pages?

When generating documentation using Sphinx, I would like to be able to generate two versions of my documentation: one including everything, and one with only a particular set of pages. What's the best way of achieving that?
I could write a build script that moves files around to achieve this but it would be really nice if there was a way to tell sphinx to exclude or include particular documents during a particular build.
Maybe my answer comes a bit late, but I managed to do this with Sphinx via exclude patterns in the config file.
My documentation is partly for users and partly for admins.
Some pages have file names that contain the word admin, and like you, I wanted to build two versions: one with everything (the admin docs) and one with all "admin" pages excluded (the user docs).
To exclude all "admin" pages in all subfolders, you have to add this line to the config file conf.py:
exclude_patterns = ['**/*admin*']
That was the easy part.
My problem was that I didn't know how to run the build two times, one with and one without the exclude patterns without using two different config files.
I didn't find a solution by myself, so I asked a question here on SO and got an answer:
The config file is just a Python file and can contain Python code, which will be executed on build.
You can pass parameters ("tags") via the command line which can be queried in the config file.
So I have this exclude pattern in my config file:
exclude_patterns = ['**/*admin*']
if tags.has('adminmode'):
exclude_patterns = []
Now I can run the build without passing anything, which will exclude the "admin" files:
make clean
make html
⇒ this is my user documentation
...and I can set the "adminmode" tag, which will not exclude anything:
(Windows command line syntax)
set SPHINXOPTS=-t adminmode
make clean
make html
⇒ this is my admin documentation.
Bonus:
I can use the same tag to ignore some specific content on a page, by Including content based on tags.
Example:
regular documentation
=====================
This paragraph and its headline will always be visible.
.. only:: adminmode
secret admin stuff
------------------
This paragraph will be visible in the admin docs only.
This will (again) always be visible.
The only and ifconfig directives can be used to apply conditions within pages.
There does not seem to be any simple way to use conditions to completely exclude entire pages (.rst files).
The following (in index.rst) excludes the reference to doc2.html in the toctree in index.html when generating HTML output:
.. toctree::
doc1.rst
.. only:: latex
.. toctree::
doc2.rst
But this does not really work. The doc2.html file is still generated, and it is reachable via the "Next topic" link when doc1.html is the current topic.
How about sphinx.ext.ifconfig? You set config values in your conf.py file. As that is a regular Python file, you can make your inclusion criteria smart and automatic if you need to.

Manage #TODO (lots of files) with VIM

I use VIM/GVIM to develop my python projects and I randomly I leave #TODO comments in my code.
Is there any way to manage (search, list and link) all the #TODO occurrences inside VIM? I tried the tasklist plugin, it's almost what I need, but it only lists the current file #TODO occurrences. Generally my projects has some sub-folders and many .py files, so I'd like to find a way to search through all folders and files in the current working directory and list them.
If you just want a list of the occurences of "TODO" in .py files in the working directory, you can just use :vimgrep like so:
:vimgrep TODO **/*.py
Then open the quickfix window with:
:cw
(it might open it automatically anyway, not sure) and just scroll through the results, hitting Enter to go to each occurrence.
For more complicated management, I'd probably recommend setting up an issue tracker.

Categories

Resources