Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Is "ref($class) || $class" a bad thing?

by hossman (Prior)
on Jul 13, 2004 at 01:19 UTC ( #373795=note: print w/ replies, xml ) Need Help??


in reply to Is "ref($class) || $class" a bad thing?

my $class = ref($class) || $class;

In order to write a test that would cover this, you would have to include the following in your test code:

my $foo = Foo->new();
my $foo2 = $foo->new();
my $foo3 = Foo::new();

Right off the bat, that's a missleading statement.

Code coverage tools can only tell you what lines are executed or not executed, I've never seen one smart enough that you can run it on a test suite, and have it tell you if all the side effects of every executed line of code is also executed.

If I was running Devel::Cover on some unit tests I wrote, and it told me there was a conditional in which one branch wasn't getting run, I would never assume that just writting a test that executed that branch is enough to consider that branch "tested". I would also look at any state modified in that branch, and make sure that my test included any other code that refrenced that state after the branch.

That's not just an important thing to remember about Code Coverage -- it's important to remember about any unit tests. I'm constently seeing people design/modify classes or objects which contain state, and write little micro-unit-tests which test each method individually, without ever testing "chains" of method invocations. The worst example I ever saw was an implimentatin of a glorified Hash that had methods like "add($key,$val)</code, <code>get($key), remove($key) and listKeys()" This is what the unit tests looked like...

function testAdd() { $hash = new Hash; assert($OK == $hash->add("foo","bar")); } function testGet() { $hash = new Hash; $hash->add("foo","bar"); assert("bar" == $hash->get("foo")); } function testList() { $hash = new Hash; $hash->add("foo","bar"); assert("foo" == $hash->listKeys()); } function testRemove() { $hash = new Hash; $hash->add("foo","bar"); assert($OK == $hash->remove("foo")); }

The unit tests all passed, the code was handed off to the client, and the client send back the following set of tests *they* had written to prove that the damn library was a load of crap...

function testGetShouldNotDieHorriblyIfKeyNotFound() { $hash = new Hash; $hash->get("foo"); # this was causing a core dump } function testRemoveShouldAcctuallyRemove() { $hash = new Hash; $hash->add("foo","bar"); $hash->remove("foo"); assert("bar" != $hash->get("foo")); # hey look, still there }

Just becuase you're code is getting executed, doesn't mean it works, or that all of the permutations of use cases are getting executed.


Comment on Re: Is "ref($class) || $class" a bad thing?
Select or Download Code
Re^2: Is "ref($class) || $class" a bad thing?
by stvn (Monsignor) on Jul 13, 2004 at 01:47 UTC
    Code coverage tools can only tell you what lines are executed or not executed, I've never seen one smart enough that you can run it on a test suite, and have it tell you if all the side effects of every executed line of code is also executed.

    I agree with you 100%, as I said in the OP

    But it occured to me that while this would give you sought after 100% coverage, it not only did nothing to improve the quality of the test, but it allowed a (potential) bug to slip through.
    The paper I linked to drives how this point even more, if you haven't already, you should give it a read, I thought it was quite good.

    -stvn

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://373795]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (11)
As of 2014-04-21 16:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (496 votes), past polls