Unit testing is… well, flawed.

Hey someone had to say it.

I clearly recall my first IT job. I was appointed Systems Co-coordinator for a leading South African retailer at store level. Don’t get me wrong, there is absolutely nothing wrong with an honest day’s labor and in fact I highly recommend it, however I’m obliged to refer to the designation cautiously; in reality all I had to do was monitor in-store prices and two UNIX front line controllers. If anything went wrong – I only had to phone it in…

Luckily that wasn’t all I did. My duties extended to some other interesting annual occurrence – stock take. Despite a bit more curious affair, it was still a tedious process that took weeks of preparation and several nights to complete.  Then also I remember that no matter how elaborate our planning was, the entire exercise would be rendered useless if we couldn’t get the basics right – that being the act of counting.

Sounds simple right? We’ll with a store which could potentially carry over tens of thousands of different items… we’ll let’s just say I believe that’s when I first became a coffee addict.

In those days the act of counting stock was a very humble process. Nothing like we have today. A staff member would be assigned a bin or shelve filled with items he or she had to sort then count. Thereafter they had to record their findings on a complementary piece of paper.

Every night I would manage several teams. Each team was divided into two groups - counters and auditors. Both groups had the same task, only auditors followed shortly on the heels of the counters, recounting stock levels, making sure the original count correspond to their findings.

It was a simple yet hugely responsible orchestration of people and thankfully there was one fundamental and golden rule I could always abide by to ensure things run smoothly – No-one was allowed to audit their own work. Nope, not even on nights when I didn’t have enough staff available. This meant I too at times had to get up there and get counting, or have the audit stand over until the next evening. The reason for this was obvious - late at night and with so much to do we were prone to make some mistakes, then on the recount, without a fresh set of eyes, you were likely to repeat the offence.

Now years later this rule or guideline still holds true as we develop software (as far removed as software development from counting stock may be). For some reason it is a fundamental guideline we’re simply ignorant of. We write our code, we write our tests and thus commit the same horrendous offence. Indeed, the process of writing unit tests as practiced in most development shops today – is flawed.

Most if not all of the tests we write today exercise application logic – our logic. They are based on the way we believe an application or method should/may/will behave or function. As we write our tests, our unit tests mirror our best understanding of the inner workings of our application code. Unfortunately these tests will therefore also include (or be unaware of) any imperfections and errors on our part. If your logic is flawed as you write your initial code, chances are, without a fresh set of eyes, you will commit the same error second time around too.

Not even experience seems to be a suitable solution. It certainly helps to have deeper insight, but is that really the answer we should be looking for? Is that really failsafe?

What about code review? Code review is certainly an answer. You could have one developer coding away and another (or team) making sure the logic is sound. The practice however has its obvious drawbacks. Firstly and mainly it is resource intensive and from what I’ve seen in most development houses, given heavy deadlines, this guideline is seldom adhered to. Hardly ever do we have the resources, money or time readily available.

So what other options are out there?

A quest to find some solution revealed a project by Microsoft Research called PEX. PEX is a framework which creates several test scenarios for each method or class you write, automatically. Think of it as your own personal auditor. Within a few clicks the framework will auto generate several unit tests for a given class or method and save them to a single project.

PEX help to audit your work. It lends a fresh set of eyes to any project you’re working on and best of all; it is cost effective and fast.

Check them out at http://research.microsoft.com/en-us/projects/pex/

In upcoming posts we’ll dive deeper into how it works and how it can help you.

 

Certainly there are more similar frameworks out there and I would love to hear from you. Please share your experiences and insights.  

 

posted @ Wednesday, November 23, 2011 11:40 AM
Print

Comments on this entry:

# re: Unit testing is… well, flawed.

Left by Travis at 11/23/2011 5:18 PM
Gravatar
Interesting point, however unit-tests are key to TDD, and TDD cant live with out you writing your own tests. However, having others write Integration tests for other's code sounds very intriguing.

# re: Unit testing is… well, flawed.

Left by Keith Nicholas at 11/23/2011 10:10 PM
Gravatar
I think you mis-understand unit tests.... they are simply there as mini specs of what you think your code should do.

as many many people have said over and over again...... unit testing is less about testing and more about design and specification. Its just also handy they allow for regression testing. But they don't prove that they perform any business value

Acceptance testing is verification that the system does the right thing.

Acceptance testing should definitely done by someone who isn't the developer..

# re: Unit testing is… well, flawed.

Left by Dewald Galjaard at 11/24/2011 9:16 AM
Gravatar
@Travis. Thanks for the comment.

Yes that certainly is a difficulty. I didn't want to touch on the subject of TDD just yet. Rather the focus of the post was how we easily can incorporate logical errors into our Unit tests.

# re: Unit testing is… well, flawed.

Left by Dewald Galjaard at 11/24/2011 9:28 AM
Gravatar
@Keith.

I think there may be very different views on what Unit Testing is and should be. Therefore I cannot fully agree with all you said.

I would like to comment on your first argument though which goes along with with the statement i'm trying to make - 'Tests are mini specs'. My argument is this, if your orignal logic is flawed then your 'mini specs' will be flawed as well. Unit test (and testing in general) can only tackle known unknowns.

# re: Unit testing is… well, flawed.

Left by Keith Nicholas at 11/24/2011 11:58 PM
Gravatar
yes, they can definitely be flawed. hence the need for acceptance testing.

In practice however, the whole process of having to reflect on things with unit tests actually tends to make you find a lot more faults.

the point of unit tests is they are the first line defence against bugs, they are attempting to stop faults being introduced within the circle of a developer. There are then techniques to stop faults being introduced within the circle of a couple of developers, techniques to stop faults being introduced within the circle of a team.

In agile, especially Extreme Programming, its required to have all those circles of assurance.

but unit tests by themselves just is a first line defense.

In reality, it was found that unit tests were just so effective in stopping a LOT of faults that it created pretty good quality software of production level that many people skip a lot of other testing. Especially in the web world.

# re: Unit testing is… well, flawed.

Left by Mark Pearl at 11/25/2011 1:57 PM
Gravatar
I am not sure if I would say they are flawed... they are just one of many mechanisms to create quality software... So if you were to use them alone then yes, you will still have issues... but if used with Integration Testing, Acceptance Testing, etc they give you a layer of protection that I would prefer to have...

PEX would just be another layer of protection and also would not sovle all your issues.

# re: Unit testing is… well, flawed.

Left by Dewald Galjaard at 11/25/2011 2:54 PM
Gravatar
Guys thank you for everyone's feedback.

Yes, unit testing in isolation certainly isn't a comprehensive way to test your work and there are far more tests including those you've mentioned that should be included. 

However this post is related to unit testing and unit testing only.
Nothing more, nothing less.

Forget about acceptance testing, integration testing etc for now. Baby steps. I'd like to deal with each of those on a separate occasion :)

I'm only pointing out this specific flaw at this specific stage of testing :)

Your comment:



(not displayed)


 
 
 
 
 

Live Comment Preview:

 
«May»
SunMonTueWedThuFriSat
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789