Test Coverage Leading You Astray? 48
An anonymous reader writes "Are your test coverage measurements leading you astray? Test coverage tools bring valuable depth to unit testing, but they're often misused. This article takes a closer look at what the numbers on the coverage report really mean, as well as what they don't. It then suggests three ways you can use your coverage to ensure code quality early and often."
Testing? (Score:5, Funny)
Segmentation fault
Re:Testing? (Score:1)
When i asked him where his error handling routines were he replied: "I don't program errors!"
I know, nothing to do with unit testing; but still worth a mention.
Pet peeve: Exception "eating" (Score:2)
Re:Pet peeve: Exception "eating" (Score:1)
and that reminds me of something i recently saw on thedailywtf: The Perils of Error-free Code [thedailywtf.com]
it all comes down to being competent or not
code flow is also important (Score:2, Informative)
Re:code flow is also important (Score:2)
That would be embarassing ;)
Re:code flow is also important (Score:2, Insightful)
What you can do, is use tools like PolySpace (www.polyspace.com) to ensure you won't have any array overruns, out of range errors, access through dangling pointers, etc. You can then run unit tests on the 'working' code in working scenarios to ensure it does what it should.
Unit tests are too simplistic for many apps (Score:2, Informative)
Can you test that the LCD has refreshed at the inputted rate? Can you verify that the input data was correctly injected into the database just be checking the output of the function?
Functions lie like dogs. You can test the
Re:Unit tests are too simplistic for many apps (Score:1, Funny)
YHBT
Your unit tests are too simplistic; mine aren't (Score:5, Insightful)
And the solution is... "holistic" unit tests.
While it's true that unit tests have a hard time making that last little yard (mostly in the form of hardware output, like graphics on the screen or your example), you're not writing your unit tests correctly. It's a rare unit test for me that is the equivalent of checking that adding two numbers work correctly, and while those are useful in development, they very, very rarely ever break later. Pure arithmetic function are the easiest to write, in general, and they correspondingly have the smallest need for continuous automated testing. (Not zero, of course, just the smallest. And when they do break, boy howdy...!)
In your other example, you ask:
Can you verify that the input data was correctly injected into the database...
(and I cut the rest of this question off as it posits an incorrect approach.)
The answer to this is yes, although you need a good database and a good understanding of how they work. (Not "great", just good.) I have thousands of tests that verify that certain code correctly manipulates the database, and that verifies calling certain webpages correctly manipulates the database. It's only marginally harder than testing a traditional function. The key here is to do everything inside a transaction; perform the task, do your verification, then roll the entire transaction back. Then it doesn't affect your database (which should normally be the "test" database, of course), and as a side effect under all but the "READ-UNCOMMITTED" transaction level, allows you to have any number of copies of the same test(s) running against the exact same database.
I can't imagine writing a distributed database-based application without such tests. Well, I can, but it's no fun.
In a lot of database-based applications, since the database is the application, this goes a long way toward testing the entire app.
Your unit tests ought to cover everything but the hardware output, which is more the exception than the rule.
Part of the problem is the number of APIs that exist with no thought for testing, making it seem as if unit testing them is impossible. For example, a lot of GUI toolkits are a major pain in the ass because it's difficult or impossible to fully simulate pressing a key in them and then processing the event loop exactly once, after which you will see what happened. This is a limitation of the toolkit, though, not unit testing, one I fervently hope will someday be eliminated after my whining on Slashdot catches the eye of one of the GTK developers or something.
In other cases, you have to a little work, but it can be done. We use Apache::ASP, and it ships with a little Perl script that can run an ASP page outside of the webserver via a command line. Still not terribly useful, but I was able to take that script and turn it into something that accepts multiple requests over a pipe, and wrap another Perl module around it that manages the connection to make it easy to use. Now, in my unit tests, calling a web page looks just like calling a function. Unfortunately, the rollback idea doesn't trivially work here, but I have some other things in place to help with this. The upshot is my unit tests include whether entire web pages work. This is some damned fine testing, and it's caught plenty of bugs long before they get out to the user.
Sure, right on the periphery of some systems is hard to reach, but the vast majority of any system is perfectly managable.
Re:Your unit tests are too simplistic; mine aren't (Score:1)
Re:Your unit tests are too simplistic; mine aren't (Score:2)
(Honestly, my problem hasn't been the frameworks, my problem has been people proving that you can write tightly-coupled spaghetti code in any environment if you don't watch them like a hawk. Ruby's no more the answer to that than what I've already got in place.)
Re:Unit tests are too simplistic for many apps (Score:2)
The idea that you can input some values and expect useful output from a function is nice in theory. Perhaps in some very limited mathematics oriented programs where the inputs must lead to a nice answer, but real world applications may end up manipulating more than just the input data.
You're right. And in such instances, if all you're doing is checking output, you don't really understand unit testing-- a key tenet of which is testing the code unit in isolation.
Can you test that the LCD has refreshed at the
Non-object oriented test tools? (Score:5, Interesting)
Anyway, having had a quick look, it is all about Java.
I'd love to hear from anyone who can recommend test coverage tools for C (ie. non-object oriented). I think that just about all of the articles I've ever read about testing methodologies have been exclusively about object-oriented patterns, and pretty much only Java or
Object-oriented techniques are a good tool, but not the right tool for every job...
Re:Non-object oriented test tools? (Score:3, Informative)
See http://www.polyspace.com/ [polyspace.com]
Re:Non-object oriented test tools? (Score:3, Informative)
http://c2.com/cgi/wiki?TestingFramework [c2.com]
G.
Re:Non-object oriented test tools? (Score:1)
Cheers
Re:Non-object oriented test tools? (Score:2)
You want gcc with "-fprofile-arcs -ftest-coverage", then you can use gcov [gnu.org]/ggcov [sourceforge.net]/lcov [sourceforge.net] to produce usable output. See Vstr [and.org] and And-httpd [and.org] for examples.
Re:Non-object oriented test tools? (Score:1)
Of course, I should have mentioned that I'm using Microsoft Developer Studio, but regardless, I'm looking for a completely cross-platform compiler-independent and build-system-independent test framework.
Re:Non-object oriented test tools? (Score:2)
For C?! ... yeh, I'm looking for a flying car too. So if you let me know when you find what you want that'll help, as I have a feeling they'll be in the same place.
Re:Non-object oriented test tools? (Score:1)
As far as the build system is concerned, most unit test frameworks I've seen to date rely on autoconf and automake, or Ant, etc. It would be nice to find a framework that doesn't specify the build system at all. Would that be so very difficult?
Maybe I'm missing something - I've never actually used a unit test framework.
Re:Non-object oriented test tools? (Score:1)
Stig Brautaset recently wrote about Testing C with Libtap [onlamp.com].
Test coverage is just annoying... (Score:5, Funny)
Bloody cricket.
Re:Test coverage is just annoying... (Score:1, Funny)
Re:Test coverage is just annoying... (Score:3, Insightful)
DO-178B (Score:5, Interesting)
1) Line Coverage - Has every line been tested
2) Branch Coverage - Has every branch been tested
3) Boolean Coverage - Is EVERY possibility on a truth table for each logical operator explicitly defined
These tests alone don't certify that the code is ready for an airplane and that it is indeed "bug free." My software engineering professor said it best when he stated, you can only prove the existence of bugs, you cannot prove the non-existence of bugs. These guidelines as adopted by the FAA for the certification of safety critical code, don't prove the non-existence of bugs, but they do go a long way towards proving the existence many bugs and provide a MINIMUM standard to which code must be exercised before being allowed into an airplane.
Software Engineering is a science, methodology has been pioneered to help us ENGINEER the software we develop to be as defect free as we know how to make it. As in other disciplines of engineering, there will always be things not yet quantified. Take architecture for example, an architect would design a bridge to withstand an earthquake of a specific magnitude, winds of a specific speed. Does that mean the bridge is safe? What if the materials used weren't rated for the temperature range needed for the locale, etc...
As much as we do to ensure quality, there is no silver bullet. The company I interned at which will remain nameless made a multi-function navigational display for airforce one. It rebooted during a touch and go at 40 degrees farenheit. Wasn't it tested you ask? Of course it was, it was tested at -40 degrees and 140 degrees, but the timing on one of the buses was off at 40 and the hardware watchdog took it into a reboot at a very critical time. It was DO 178B Level A certified, had 100% code coverage of course, but there will always be bugs. Don't trust tools to tell you otherwise, because you can never prove the non-existence of bugs.
(For those who don't know, a touch and go is where the plane starts landing and takes off again)
Re:DO-178B (Score:3, Interesting)
Re:DO-178B - MCDC (Score:3, Informative)
MCDC requires that "every point of entry and exit in the program has been invoked at least once, every condition in a decision in the program has taken all possible outcomes at least once, every decision in the program has taken all possible outcomes at least once, and each condition in a decision has been shown to independently affect that decision's outcome. A condition is shown to independen
Re:DO-178B - MCDC (Score:1)
Re:DO-178B - MCDC (Score:3, Funny)
She kept her processor clean
She was the best damn computer I had ever seen
She had Bugzilla eyes
Telling me no lies
Knockin' me out with those APIs
Taking more than her share
Had me fighting for air
She told me to com(pil)e but I was already there
'Cause the walls start shaking
The game was Quaking
My mind was aching
And we were make-ing it and you -
Test me all night long
Yeah you test me all night long
Re:DO-178B (Score:4, Insightful)
The code was designed, exercised, tested, and executed properly from what you're saying. The display failed due to hardware problems.
In what way is that hardware failure related to code coverage or any other form of software testing or QA metric?
Re:DO-178B (Score:2, Interesting)
To answer your question, I was trying to illustrate that the metrics while a good starting point are merel
Re:DO-178B (Score:1)
Re:DO-178B (Score:1)
Re: DO-178B (Score:2)
You sure that's what he said? Program proofs can indeed prove the correctness of programs (i.e. the non-existence of bugs). It's just that they're hard to do for any significant amount of code.
The way I heard the quote, it's about testing: "Program testing can at best show the presence of errors but never their absence." (Edsger W. Dijkstra)
Yes (Score:3, Funny)
Bit of a strange subject for slashdot, eh?
know what to test for (Score:4, Insightful)
The Emperor has very few clothes (Score:3, Informative)
Just executing a line of code or a branch (whilst running a test) does not imply that you are testing that code.
Re:The Emperor has very few clothes (Score:1)
Which is fine if you understand that. My fear, if I should use such tools, is that they would produce semi-meaningful figures (say, a percentage) and Management would learn about it, and start measuring progress and performance based on them. Once that happened, these semi-meaningful figures would control
Thought this was about smoking.. (Score:2)
..cuz I read "Test Coverage Leading You to Ashtray".
Test coverage efforts are more likely to drive people to drink, IMO.
Bug in the tool itself? (Score:1)
package com.vanward.coverage.example01;
public class PathCoverage {
public String pathExample(boolean condition){
String value = null;
if(condition){
value = " " + condition + " ";
}
return value.trim();
}
}
and the code was executed once with condition equal to TRUE. It then reported 100% coverage!
How is that 100% coverage? If condition was FALSE then a completely different path through the instructions would have been executed!
I
Re:Bug in the tool itself? (Score:2)
The bug occurs when one branch is NOT executed (that run will have 75% coverage)