4

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?

oz123
  • 27,559
  • 27
  • 125
  • 187
Felix
  • 3,351
  • 6
  • 40
  • 68

2 Answers2

2

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])
Felix
  • 3,351
  • 6
  • 40
  • 68
0

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 
Spencer
  • 709
  • 5
  • 12
  • I don't want to run nosetests as application: I have to run it on Windows machines that don't have it in path. So I should run it from Python. coverage package is already installed. – Felix May 12 '15 at 17:39
  • Whoops missed that. You should be able to add plugins via the plugins keyword: http://nose.readthedocs.org/en/latest/usage.html#using-plugins – Spencer May 12 '15 at 17:41
  • It's for the commandline usage. I need their selection just in Python code. – Felix May 12 '15 at 17:46
  • "If you are running nose.main() or nose.run() from a script, you can specify a list of plugins to use by passing a list of plugins with the plugins keyword argument." – Spencer May 12 '15 at 17:50
  • Oh, I've seen it too. But there was no one example how to pass them. I've tried `plugins=["coverage"]` and `plugins=["nose-coverage"]` but nothing changes. Maybe you can give me the right code snippet? – Felix May 12 '15 at 17:54
  • You should be able to add the same flags as arguments `nose.run('--cover_html')` for example. – Spencer May 12 '15 at 18:01
  • I've just tried: `result = nose.run("--with-coverage", module=tests, config=config)`, it gives me an error: `TypeError: __init__() got multiple values for keyword argument 'module'`. Oy vey... – Felix May 12 '15 at 18:04