We will use nose2
to make it easier to discover and run unit tests. We will measure test coverage, and therefore, we will install the necessary package to allow us to run coverage with nose2
. First, we will install the nose2
and cov-core
packages in our virtual environment. The cov-core
package will allow us to measure test coverage with nose2
.
Make sure you quit the Tornado's HTTP server. Remember that you just need to press
Ctrl
+
C
in the Terminal or command-prompt window in which it is running. We just need to run the following command to install the nose2
package that will also install the six
dependency:
pip install nose2
The last lines for the output will indicate that the nose2
package has been successfully installed:
Collecting nose2 Collecting six>=1.1 (from nose2) Downloading six-1.10.0-py2.py3-none-any.whl Installing collected packages: six, nose2 Successfully installed nose2-0.6.5 six-1.10.0
We just need to run the following command to install the cov-core
package that will also install the coverage
dependency:
pip install cov-core
The last lines for the output will indicate that the django-nose
package has been successfully installed:
Collecting cov-core Collecting coverage>=3.6 (from cov-core) Installing collected packages: coverage, cov-core Successfully installed cov-core-1.15.0 coverage-4.2
Open the previously created async_api.py
file and remove the lines that create the web.Application
instance named application and the __main__
method. After you remove these lines, add the next lines. The code file for the sample is included in the restful_python_chapter_10_02
folder:
class Application(web.Application): def __init__(self, **kwargs): handlers = [ (r"/hexacopters/([0-9]+)", AsyncHexacopterHandler), (r"/leds/([0-9]+)", AsyncLedHandler), (r"/altimeters/([0-9]+)", AsyncAltimeterHandler), ] super(Application, self).__init__(handlers, **kwargs) if __name__ == "__main__": application = Application() application.listen(8888) tornado_ioloop = ioloop.IOLoop.instance() ioloop.PeriodicCallback(lambda: None, 500, tornado_ioloop).start() tornado_ioloop.start()
The code declares an Application
class, specifically, a subclass of tornado.web.Application
that overrides the inherited constructor, that is, the __init__
method. The constructor declares the handlers
list that maps URL patterns to asynchronous request handlers and then calls the inherited constructor with the list as one of its arguments. We create the class to make it possible for the tests to use this class.
Then, the main method creates an instance of the Application class, registers a periodic callback that will be executed every 500
milliseconds by the IOLoop
to make it possible to use
Ctrl
+
C
to stop the HTTP server, and finally calls the start
method. The async_api.py
script is going to continue working in the same way. The main difference is that we can reuse the Application
class in our tests.
Finally, create a new text file named .coveragerc
within the virtual environment's root folder with the following content. The code file for the sample is included in the restful_python_chapter_10_02
folder:
[run] include = async_api.py, drone.py
This way, the coverage
utility will only consider the code in the async_api.py
and drone.py
files when providing us with the test coverage report. We will have a more accurate test coverage report with this settings file.
3.137.212.71