Recipe 7.9. Monitoring Test Coverage with rake stats


Problem

Generally speaking, the more tests you write, the more confident you can be that your application isand will remainbug free. You want a way to gauge how much test code you've written in relation to your application code.

Solution

Use the stats rake task to generate statistics about your Rails project, including the Code to Test Ratio:

$ rake stats (in /home/rob/typo) +----------------------+-------+-------+---------+---------+-----+-------+ | Name                 | Lines |   LOC | Classes | Methods | M/C | LOC/M | +----------------------+-------+-------+---------+---------+-----+-------+ | Helpers              |   500 |   401 |       0 |      75 |   0 |     3 | | Controllers          |  1498 |  1218 |      25 |     174 |   6 |     5 | | APIs                 |   475 |   383 |      17 |      27 |   1 |    12 | | Components           |  1044 |   823 |      33 |     132 |   4 |     4 | |   Functional tests   |  2505 |  1897 |      41 |     261 |   6 |     5 | | Models               |  2026 |  1511 |      46 |     209 |   4 |     5 | |   Unit tests         |  1834 |  1400 |      28 |     159 |   5 |     6 | | Libraries            |   858 |   573 |      15 |      91 |   6 |     4 | +----------------------+-------+-------+---------+---------+-----+-------+ | Total                | 10740 |  8206 |     205 |    1128 |   5 |     5 | +----------------------+-------+-------+---------+---------+-----+-------+   Code LOC: 4909     Test LOC: 3297     Code to Test Ratio: 1:0.7

Discussion

Complete coverage is when every line of your application has been exercised by at least one test. While this is a tough goal to achieve, it's worth working towards. If you write tests when or even before you build the components of your application, you should have a pretty good idea of what code needs more test coverage.

The solution shows the output of rake stats on a current version of the Typo blog application (http://www.typosphere.org). It shows a code to test code coverage ratio of 1 to 0.7. You can use this ratio as a general gauge of how well you are covering your source code with tests.

Aside from testing, you can use the output of rake stats as a vague gauge of productivity. This kind of project analysis has been used for decades. Measuring lines of code in software projects originated with languages such as FORTRAN and Assembler at a time when punchcards were use for data entry. These languages offered far less leeway than today's scripting languages, so using source lines of code (SLOC) as a measure of productivity was arguably more accurate, but not by much.

See Also

  • Section 7.24"




Rails Cookbook
Rails Cookbook (Cookbooks (OReilly))
ISBN: 0596527314
EAN: 2147483647
Year: 2007
Pages: 250
Authors: Rob Orsini

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net