NoseTest: running with coverage from Python script - python

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?

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])

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

Related

Trying to run a unittest module

I have created a unittest module called test_blbmktdata.py to test some code. I am trying to run it from the console using the following command:
run test_blbmktdata.py
However I get the error message:
ERROR:root:File `'test_blbmktdata.py'` not found.
Please see below for the module code.
import unittest
class TestBlbMktData(unittest.TestCase):
staticName='StaticInstrumentData.csv'
def print(self,data):
print()
print(len(data))
print(data.head())
print(data.dtypes)
def read(self,name,func):
return func(os.path.join(self.current_dir,name))
def setUp(self):
self.current_dir=os.path.dirname(os.path.abspath(__file__))
def test_corp_act(self):
self.print(self.read(self.corpName,readCorpAction))
if __name__ == '__main__' :
unittest.main()
How can I run the code from the console?
Try python -m unittest test_blbmktdata in terminal
https://docs.python.org/2/library/unittest.html#command-line-interface
For running a python code say code.py you can simply use command
python code.py
on the terminal after you navigate to the same directory where code is located.

Pytest file not covered (0 percent lines covered)

I am using the python coverage package to determine line percent coverage for the following file
coverage report -m math_test.py
Once running the command however I ended up having 0 lines covered.
import example
import pytest
import unittest
class SampleTest(unittest.TestCase):
def testAddition(self):
expected = 10
math_addition = example.add(5,5)
self.assertEqual(math_addition, expected)
def add(x,y):
return x+y
Running math_test.py won't do anything. It defines a class and a function, but doesn't do anything with either of them. Coverage.py is not a test runner. You need to use something like pytest or unittest to run the tests:
coverage run -m unittest discover
coverage report -m

How to generate test report using pytest?

How can I generate test report using pytest? I searched for it but whatever i got was about coverage report.
I tried with this command:
py.test sanity_tests.py --cov=C:\Test\pytest --cov-report=xml
But as parameters represents it generates coverage report not test report.
Ripped from the comments: you can use the --junitxml argument.
$ py.test sample_tests.py --junitxml=C:\path\to\out_report.xml
You can use a pytest plugin 'pytest-html' for generating html reports which can be forwarded to different teams as well
First install the plugin:
$ pip install pytest-html
Second, just run your tests with this command:
$ pytest --html=report.html
You can also make use of the hooks provided by the plugin in your code.
import pytest
from py.xml import html
def pytest_html_report_title(report)
report.title = "My very own title!"
Reference: https://pypi.org/project/pytest-html/
I haven't tried it yet but you can try referring to https://github.com/pytest-dev/pytest-html. A python library that can generate HTML output.
py.test --html=Report.html
Here you can specify your python file as well. In this case, when there is no file specified it picks up all the files with a name like 'test_%' present in the directory where the command is run and executes them and generates a report with the name Report.html
You can also modify the name of the report accordingly.

Intellij/Pycharm can't debug Python modules

I use PyCharm/IntelliJ community editions from a wile to write and debug Python scripts, but now I'm trying to debug a Python module, and PyCharm does a wrong command line instruction parsing, causing an execution error, or maybe I'm making a bad configuration.
This is my run/debug configuration:
And this is executed when I run the module (no problems here):
/usr/bin/python3.4 -m histraw
But when I debug, this is the output in the IntelliJ console:
/usr/bin/python3.4 -m /opt/apps/pycharm/helpers/pydev/pydevd.py --multiproc --client 127.0.0.1 --port 57851 --file histraw
/usr/bin/python3.4: Error while finding spec for '/opt/apps/pycharm/helpers/pydev/pydevd.py' (<class 'ImportError'>: No module named '/opt/apps/pycharm/helpers/pydev/pydevd')
Process finished with exit code 1
As you can see, the parameters are wrong parsed, and after -m option a IntelliJ debug script is passed before the module name.
I also tried just put -m histraw in the Script field, but doesn't work, that field is only to put Python script paths, not modules.
Any ideas?
There is another way to make it work.You can write a python script to run your module.Then just configure PyCharm to run this script.
import sys
import os
import runpy
path = os.path.dirname(sys.modules[__name__].__file__)
path = os.path.join(path, '..')
sys.path.insert(0, path)
runpy.run_module('<your module name>', run_name="__main__",alter_sys=True)
Then the debugger works.
In PyCharm 2019.1 (professional), I'm able to select run as module option under configurations, as below
I found it easiest to create a bootstrap file (debuglaunch.py) with the following contents.
from {package} import {file with __main__}
if __name__ == '__main__':
{file with __main__}.main()
For example, to launch locustio in the pycharm debugger, I created debuglaunch.py like this:
from locust import main
if __name__ == '__main__':
main.main()
And configured pycharm as follows.
NOTE: I found I was not able to break into the debugger unless I added a breakpoint on main.main() . That may be specific to locustio, however.
The problem is already fixed since PyCharm 4.5.2. See corresponding issue in PyCharm tracker:
https://youtrack.jetbrains.com/issue/PY-15230

How to fail a conda package build when there are errors on run_test.py

I am creating a conda recipe, and have added run_test.py . These are unittest classes.
Unfortunatly, when there are errors, the package is still created.
My question, how to inform conda that the test failed, and it should not continue with the package build.
run_test.py contains :
suit = unittest.TestLoader().discover("../tests/unitTest")#, pattern="test[AP][la]*[sr].py")
unittest.TextTestRunner(verbosity=2).run(suit )
I do add the files in meta.yaml
test:
files:
- ../tests/unittest/
This is the output:
Ran 16 tests in 2.550s
FAILED (errors=5)
===== PACKAGE-NAME-None-np18py27_0 OK ====
I want to stop the build
The script needs to exit nonzero. If the tests fail, call sys.exit(1) in the script.
In run_test.py, you can invoke unittest.main() by doing the following:
if __name__ == "__main__":
unittest.main()
The conda build process will automatically fail if the tests do not succeed. This link will help demonstrate: invoking unittest main method

Categories