NoseTest: running with coverage from Python script

2.4k views Asked by At

I want to run NoseTest from a Python script. But I want not only run it, but also measure test coverage.

Just now I have the following code:

import os
import sys
import nose

sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))

import tests

if __name__ == "__main__":
    config = nose.config.Config(verbosity=3, stopOnError=False, argv=["--with-coverage"])
    result = nose.run(module=tests, config=config)

What should I add to get my coverage report?

2

There are 2 answers

0
Felix On BEST ANSWER

Hell yeah! After some small debugging of Nose Test I've managed to do it!

if __name__ == "__main__":
    file_path = os.path.abspath(__file__)
    tests_path = os.path.join(os.path.abspath(os.path.dirname(file_path)), "tests")
    result = nose.run(argv=[os.path.abspath(__file__),
                            "--with-cov", "--verbosity=3", "--cover-package=phased", tests_path])
7
Spencer On

EDIT: To run plugins with nose.run(), you need to use the 'plugins' keyword:

http://nose.readthedocs.org/en/latest/usage.html#using-plugins

Your code is all set -- you need to enable coverage via the runner. Simply run nose like this:

nosetests --with-coverage

There are more options here:

http://nose.readthedocs.org/en/latest/plugins/cover.html

FYI, you might need to run the following to get the coverage package:

pip install coverage