A unit test is an automated code-level test for a small and fairly isolated part of functionality, mostly in terms of functions. They should interact with external resources at their minimum, and includes testing every corner cases and cases that do not work.
Unit tests should always be pretty simple, by intent. There are a couple of ways to integrate unit tests into your development style [1]:
Comprehensive and integrally designed unit tests serves valuably as validator of your APIs, fuctionalities and the workflow that acctually make them executable. It will make it possibe to deliver your codes more quickly.
In the meanwhile, Coverage Test is the tool for measuring code coverage of Python programs. Accompany with Unit Test, it monitors your program, noting which parts of the code have been executed, then analyzes the source to identify code that could have been executed but was not.
Coverage measurement is typically used to gauge the effectiveness of tests. It can show which parts of your code are being exercised by tests, and which are not.
People use unit test discovery and execution frameworks so that they can forcus on add tests to existing code, then the tests could be tirggerd, resulting report could be obtained automatically.
In addition to adding and running your tests, frameworks can run tests selectively according to your requirements, add coverage and profiling information, generate comprehensive reports.
There are many unit test frameworks in Python, and more arise every day. It will take you some time to be falimiar with those that are famous from among the ever-arising frameworks. However, to us, it always matters more that you are actually writing tests for your codes than how you write them. Plus, nose is quite stable, it’s been used by many projects and it could be adapted easily to mimic any other unit test discovery framework pretty easily. So, why not?
Before you actually implement test codes for your software, please keep the following principles in mind [2]
There only are a few guidance for developing and testing your code on your local server assuming that you already have python installed. For more detailed introduction, please refer to the wesites of nose and coverage [3] [4].
Install Nose using your OS’s package manager. For example:
pip install nose
As to creating tests and a quick start, please refer to [5]
Nose comes with a command line utility called ‘nosetests’. The simplest usage is to call nosetests from within your project directory and pass a ‘tests’ directory as an argument. For example,
nosetests tests
The outputs could be similar to the following summary:
% nosetests tests
....
----------------------------------------------------------------------
Ran 4 tests in 0.003s OK
Coverage is the metric that could complete your unit tests by overseeing your test codes themselves. Nose support coverage test according the Coverage.py.
pip install coverage
To generate a coverage report using the nosetests utility, simply add the –with-coverage. By default, coverage generates data for all modules found in the current directory.
nosetests --with-coverage
% nosetests –with-coverage –cover-package a
The –cover-package switch can be used multiple times to restrain the tests only looking into the 3rd party package to avoid useless information.
nosetests --with-coverage --cover-package a --cover-package b
....
Name Stmts Miss Cover Missing
-------------------------------------
a 8 0 100%
----------------------------------------------------------------------
Ran 4 tests in 0.006sOK
Assuming that you have already got the main idea of unit testing and start to programing you own tests under Bottlenecks repo. The most important thing that should be clarified is that unit tests under Bottlenecks should be either excutable offline and by OPNFV CI pipeline. When you submit patches to Bottlenecks repo, your patch should following certain ruls to enable the tests:
After meeting the two rules, your patch will automatically validated by nose tests executed by OPNFV verify job.
[1]: http://ivory.idyll.org/articles/nose-intro.html
[2]: https://github.com/kennethreitz/python-guide/blob/master/docs/writing/tests.rst
[3]: http://nose.readthedocs.io/en/latest/
[4]: https://coverage.readthedocs.io/en/coverage-4.4.2
[5]: http://blog.jameskyle.org/2010/10/nose-unit-testing-quick-start/