Chapter 7. Measuring your Success with Test Coverage

In this chapter, we will cover:

  • Building a network management application
  • Installing and running coverage on your test suite
  • Generating an HTML report using coverage
  • Generating an XML report using coverage
  • Getting nosy with coverage
  • Filtering out test noise from coverage
  • Letting Jenkins get nosy with coverage
  • Updating the project-level script to provide coverage reports

Introduction

Coverage analysis is measuring which lines in a program are run and which lines aren't. This is type of analysis is also known as 'code coverage', or more simply 'coverage'.

A coverage analyzer can be used while running a system in production, but what are the pros and cons, if we used it this way? What about using a coverage analyzer when running test suites? What benefits would this approach provide compared to checking systems in production?

Coverage helps us to see if we are adequately testing our system. But it must be performed with a certain amount of skepticism. This is because, even if we achieve 100 percent coverage, meaning every line of our system was exercised, in no way does this guarantee us having no bugs. A quick example involves a code we write and what it processes is the return value from a system call. What if there are three possible values, but we only handle two of them? We may write two test cases covering our handling of it, and this could certainly achieve 100 percent statement coverage. However, it doesn't mean we have handled the third possible return value; thus, leaving us with a potentially undiscovered bug. 100 percent code coverage can also be obtained by condition coverage but may not be achieved with statement coverage. The kind of coverage we are planning to target should be clear.

Another key point is that not all testing is aimed at bug fixing. Another key purpose is to make sure that the application meets our customer's needs. This means that, even if we have 100 percent code coverage, we can't guarantee that we are covering all the scenarios expected by our users. This is the difference between 'building it right' and 'building the right thing'.

In this chapter, we will explore various recipes to build a network management application, run coverage tools, and harvest the results. We will discuss how coverage can introduce noise, and show us more than we need to know, as well as introduce performance issues when it instruments our code. We will also see how to trim out information we don't need to get a concise, targeted view of things.

This chapter uses several third-party tools in many recipes.

  • Spring Python (http://springpython.webfactional.com) contains many useful abstractions. The one used in this chapter is its DatabaseTemplate, which offers easy ways to write SQL queries and updates without having to deal with Python's verbose API. Install it by typing pip install springpython.
    Introduction
  • Install the coverage tool by typing pip install coverage. This may fail because other plugins may install an older version of coverage. If so, uninstall coverage by typing pip uninstall coverage, and then install it again with pip install coverage.
  • Nose is a useful test runner covered in Chapter 2, Running automated testsuites with Nose. Refer to that chapter for steps to install Nose.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.36.146