I'm using nosetests to run unit tests and show the code coverage using (python 2.7):
nosetests -c .noserccover -q
.noserccover file:
[nosetests]
cover-package = src/
with-coverage = 1
xcoverage-file = test_results/coverage.xml
cover-erase = 1
cover-html-dir = build/nosetests
cover-html = 1
with-xunit = 1
xunit-file = test_results/nosetests.xml
verbosity = 2
exclude-dir=src/dir_c
What i want is to include src/dir_a and src/dir_b but exclude src/dir_c.
I guess exclude-dir don't work if you use cover-package. When i run tests, src/dir_c is still included in the coverage.
Related
Is it possible to ignore the missing branch coverage? I am using jenkins for test coverage and pylint testing. Is there any possibility to ingore missing statements and get 100% branch coverage? Maybe a property that can be set in project setting?
I have found the solution of my question.
1) create .coveragerc file in your django project
2) define
JENKINS_TASKS = ('django_jenkins.tasks.run_pylint',)
COVERAGE_EXCLUDES_FOLDERS = ['packsit/migrations/*','packsit/api/v1/images.py']
COVERAGE_RCFILE = '.coveragerc'
in your setting file.
3) .coveragerc file should contains:
[run]
branch = True
omit =
*/.local/*
/usr/*
[report]
exclude_lines =
pragma: no cover
def __repr__
if self\.debug
raise AssertionError
raise NotImplementedError
if 0:
if __name__ == .__main__.:
return
try:
except:
if
self.*
ignore_errors = True
include =
packsit/api/v1/client/*
[html]
directory = coverage_html_report
then run command on terminal:
$ python manage.py jenkins --enable-coverage --coverage-format html --coverage-exclude=COVERAGE_EXCLUDES_FOLDERS
this will exclude 'if, return, self, try , except' from report generated.
I am working in a project with other people in the team using GNU autotools. In the project we are using unit test for each non trivial C++ class. I found out that there is support for unit testing. For that I am using this structure:
./
+ tests/
+ Makefile.am
+ classA_test.cc
....
+ classB_test.cc
+ src/
+ lib/
+ Makefile.am
The problem comes since my main Makefile.am is using subdir-objects options --note that I am not using recursive makefile for the source files--, I cannot export my variables --such as, AM_CPPFLAGS-- to the other Makefile. So far I made it work using:
$ make check
but I keep getting problems for the paths and the options when I do
$ make distcheck
So my questions is, how is the standard way to deal with unit tests?
EDIT:
I made it work as long as I remove the subdir-objects from the tests/Makefile.am. Now it throw some warnings but it compiles. Still it seems not to be an appropriate way to deal with unit tests
After some research I came up with the appropiate way to deal with Unit tests and Automake:
Following the previous scheme:
./
+ tests/
+ Makefile.am
+ classA_test.cc
....
+ classB_test.cc
+ src/
+ lib/
+ Makefile.am
The makefile.am in the root will be the main one, this one calls the makefile in the tests directory
$ cat Makefile.am
SUBDIRS = . tests # (Super Important) note the "." before tests,
# it means it will be executed first
....
$ cat test/Makefile.am
AM_CXXFLAGS = ...
AM_LDFLAGS = -L #top_srcdir#/lib #If needed
LDADD = -llibraryfortests #If needed
TESTS = test1 .. testN
test1_SOURCES = test1.cc ../src/somewhere/classtotest.cc
testN_SOURCES = ...
$ cat configure.ac
AM_INIT_AUTOMAKE([subdir-objects])
AC_CONFIG_FILES([Makefile])
AC_CONFIG_FILES([tests/Makefile])
...
Now if you want to run the tests
$ sh ../pathto/configure
$ make check
As well dist[check] should work
$ make distcheck
...
make[3]: Entering directory `/home/vicente/test/tests'
PASS: settings
============================================================================
Testsuite summary for Pepinos 00.13.15
============================================================================
# TOTAL: 1
# PASS: 1
# SKIP: 0
# XFAIL: 0
# FAIL: 0
# XPASS: 0
# ERROR: 0
============================================================================
make[3]: Leaving directory `/home/vicente/test/tests'
...
So to answer the other question?
Q. I cannot export my variables --such as, AM_CPPFLAGS-- to the other Makefile.
A. True, but I can always declare a variable in the configure.ac and AC_SUBT to make it visible to other Makefile.am
Sources: https://stackoverflow.com/a/29255889/2420872
I installed ocp-indent 1.4.2 via opam. It works fine with most of ocaml files but it fails to indent the eliom files properly. For example the following
{shared{
open Eliom_lib
}}
is indented as
{shared{
open Eliom_lib
}}
The command ocp-indent --print-config gives
base = 2
type = 2
in = 0
with = 0
match_clause = 2
ppx_stritem_ext = 2
max_indent = 2
strict_with = never
strict_else = always
strict_comments = false
align_ops = true
align_params = auto
syntax = mll lwt
Is the configuration wrong?
I have written a example test and I'm trying to run it without creating a new database every time.
The first time I run my test everything is ok (takes sometime due to building the DB):
> REUSE_DB=1 python manage.py test contacts
Ran 1 test in 0.251s
The second time I get the following error:
> REUSE_DB=1 python manage.py test contacts
nosetests --verbosity 1 contacts
AttributeError: 'DatabaseCreation' object has no attribute '_rollback_works'
Why and how do I solve? Thanks.
My Test:
class ExampleTestCase(TestCase):
def test_contact_page(self):
resp = self.client.get('/contact/single/')
self.assertEqual(resp.status_code, 200)
Settings.py
DEBUG = True
TEMPLATE_DEBUG = DEBUG
INSTALLED_APPS += (
'django_nose',
)
TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
Just use nose from github and you're good to go! I belive this is your issue:
https://github.com/jbalogh/django-nose/pull/95
I came across this a long while ago, it is now fixed on the github master, but unfortunately django-nose is not updated on pypi since last year..
I am trying to run tests on django with coverage. It works fine, but it doesn't detect class definitions, because they are defined before coverage is started. I have following test runner, that I use, when I compute coverage:
import sys
import os
import logging
from django.conf import settings
MAIN_TEST_RUNNER = 'django.test.simple.run_tests'
if settings.COMPUTE_COVERAGE:
try:
import coverage
except ImportError:
print "Warning: coverage module not found: test code coverage will not be computed"
else:
coverage.exclude('def __unicode__')
coverage.exclude('if DEBUG')
coverage.exclude('if settings.DEBUG')
coverage.exclude('raise')
coverage.erase()
coverage.start()
MAIN_TEST_RUNNER = 'django-test-coverage.runner.run_tests'
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]):
# start coverage - jeśli włączmy już tutaj, a wyłączymy w django-test-coverage,
# to dostaniemy dobrze wyliczone pokrycie dla instrukcji wykonywanych przy
# imporcie modułów
test_path = MAIN_TEST_RUNNER.split('.')
# Allow for Python 2.5 relative paths
if len(test_path) > 1:
test_module_name = '.'.join(test_path[:-1])
else:
test_module_name = '.'
test_module = __import__(test_module_name, {}, {}, test_path[-1])
test_runner = getattr(test_module, test_path[-1])
failures = test_runner(test_labels, verbosity=verbosity, interactive=interactive)
if failures:
sys.exit(failures)
What can I do, to have classes also included in coverage? Otherwise I have quite a low coverage and I can't easily detect places, that really need to be covered.
The simplest thing to do is to use coverage to execute the test runner. If your runner is called "runner.py", then use:
coverage run runner.py
You can put your four exclusions into a .coveragerc file, and you'll have all of the benefits of your coverage code, without keeping any of your coverage code.