Karl Seguin

Sponsors

The Lounge

Advertisement

The 7 Phases of Unit Testing

1 - Refuse to unit test because "you don't have enough time"
2 - Start unit testing and immediately start blogging about unit testing and TDD and how great they are and how everyone should do it
3 - Unit test everything - make private methods internal and abuse the InternalsVisibleTo attribute. Test getters and setters or else you won't get 100% code coverage
4 - Get fed with how brittle your unit tests are and start writing integration tests without realizing it.
5 - Discover a mocking framework and make heavy use of strict semantics
6 - Mock absolutely everything that can possibly be mocked
7 - Start writing effective unit tests


Posted Mon, Apr 27 2009 9:02 PM by karl

[Advertisement]

Comments

Rory Primrose wrote re: The 7 Phases of Unit Testing
on Mon, Apr 27 2009 9:46 PM

I think you are missing one.

0 - What is unit testing?

Brian wrote re: The 7 Phases of Unit Testing
on Mon, Apr 27 2009 10:17 PM

1b. Fight against doing it because it's extra code

1c. Try and throw the people promoting unit testing under the bus.

5b/6b - Generate a gizillion interfaces that are strictly used for unit tests.

6c - Discover a mocking framework that doesn't depend on DI to create mocks and remove the gizillion interfaces that were strictly used for unit tests only.

8 - Enjoy watching others figure out why you don't have hardly any bugs over the course of the year.

9 - Enjoy making a change to your code and only having to update the handful of tests that now fail because of the change of requirements.

Kent Sharkey wrote re: The 7 Phases of Unit Testing
on Mon, Apr 27 2009 10:58 PM

What step do you apologize to everyone you've wronged again?

Chris wrote re: The 7 Phases of Unit Testing
on Mon, Apr 27 2009 11:01 PM

Think I'm still at 6 and a bit of 6b . . . I need that 6c ;)

karl wrote re: The 7 Phases of Unit Testing
on Mon, Apr 27 2009 11:04 PM

@Kent:

You never have to apologize if you stay 1 phase ahead of everyone else :)

Scott Bellware wrote re: The 7 Phases of Unit Testing
on Mon, Apr 27 2009 11:06 PM

8. Realize that mocking everything under the sun is no less brittle than testing internals and privates :)

karl wrote re: The 7 Phases of Unit Testing
on Mon, Apr 27 2009 11:13 PM

@Scott:

That's the difference between 6 and 7 :)

Thomas Williams wrote re: The 7 Phases of Unit Testing
on Tue, Apr 28 2009 12:07 AM

G'day Karl, generalising a little bit, your post has revealed a pattern in my blogging...that the time I've got the most to say on a technology or methodology (phase 2) is *before* I learn anything really useful. Thanks for exposing my shame :-)

Josh Grenon wrote re: The 7 Phases of Unit Testing
on Tue, Apr 28 2009 12:32 AM

Great post! I think that we are in the 2nd stage at work right now. I'll be sure to get them to the final stage as soon as possible! Thanks!

Chris Brandsma wrote re: The 7 Phases of Unit Testing
on Tue, Apr 28 2009 12:51 AM

8.  Start teaching other about effective unit testing.

But I think I skipped step 3 and went straight to step 4.  And step 5 was during step 2 -- but I was using Mocks!, not those mamby-pamby dynamic mocks, or stubs, or fakes!  I wanted CONTROL!

Then I got over it and switched to fakes and I've been much more pleasant ever since.

Now where does "Throw out everything I knew about TDD and adopt BDD" fit in?  Oh ya, back a #7.

BjartN wrote re: The 7 Phases of Unit Testing
on Tue, Apr 28 2009 1:03 AM

So what is your definiton of an effective unit test?

Siderite wrote re: The 7 Phases of Unit Testing
on Tue, Apr 28 2009 3:16 AM

What comes before the zero step? I want to know my place.

Paul Batum wrote re: The 7 Phases of Unit Testing
on Tue, Apr 28 2009 4:16 AM

This list is only true if you've been learning unit testing in a vaccum, right? By understanding this list, a beginner can skip some of these steps right?

If not I'm afraid I must be still stuck at step 2 and not realise it... :/

Simon wrote re: The 7 Phases of Unit Testing
on Tue, Apr 28 2009 6:32 AM

Step 2 is option!

Brownsblogging wrote Daily Reading List
on Tue, Apr 28 2009 8:29 AM

Daily Reading List

Gogole wrote re: The 7 Phases of Unit Testing
on Tue, Apr 28 2009 8:42 AM

On phase 3 indeed, found myself changing access modifiers in order to write tests for methods.

Erik Porter wrote re: The 7 Phases of Unit Testing
on Tue, Apr 28 2009 2:07 PM

Still at step 0 and just fine with that. ;)

meisinger wrote re: The 7 Phases of Unit Testing
on Tue, Apr 28 2009 3:01 PM

i think its clear that step 2 is actually repeated after 5, 6, and 7

Randolpho wrote re: The 7 Phases of Unit Testing
on Tue, Apr 28 2009 3:45 PM

I think I'm still stuck on step 6. :(

tieTYT wrote re: The 7 Phases of Unit Testing
on Tue, Apr 28 2009 9:01 PM

Am I on step 3, 6 or 7: Once in a while I will change a method from private to protected if I want to test it in isolation. I also do this for fields once in a while if I want to "sense" through them. Sometimes I test getters, usually if the field they return changes over the unit's lifecycle.  But, I use a mocking framework and mock every dependency.  Also, since I've used a mocking framework, I've never felt like my unit tests are brittle.

karl wrote re: The 7 Phases of Unit Testing
on Tue, Apr 28 2009 9:08 PM

I'll get back to this topic shortly (in a more serious way). I've blogged about effective unit testing in the past, but I probably have a more refined idea of it now.  Just gotta get through a couple other articles first.

jose wrote re: The 7 Phases of Unit Testing
on Wed, Apr 29 2009 4:53 PM

What would be the most use pattern for testing settings the methods as public or setting them internal with internalsVisisbleTo?

karl wrote re: The 7 Phases of Unit Testing
on Wed, Apr 29 2009 9:45 PM

jose:

You shouldn't make a private method internal or public just so that you can test it. The tests that you write which cover the real public methods should hit your private methods and cover them.

lpodolak wrote re: The 7 Phases of Unit Testing
on Thu, Apr 30 2009 5:03 AM

8 - Discover BDD

9 - there is no 9 .. for now.

Brian wrote re: The 7 Phases of Unit Testing
on Thu, Apr 30 2009 9:43 PM

@effective unit testing resource: The Art Of Unit Testing by Roy Osherove - practically free if you buy the e-version.

@Chris:  Typemock Isolator

@Scott: Ain't that the truth!  NEVER TEST PRIVATES OR REFACTOR SO YOU DON'T HAVE TO IF IT'S THAT BAD!

James Peckham wrote re: The 7 Phases of Unit Testing
on Mon, May 4 2009 10:52 PM

My experiences/phases haven't been like that at all. Mine were more like this:

1. read a crap ton of TDD books

2. read some more

3. read kent becks and finally 'get it'

4. do it ALOT by myself

5. blog about it alot because to teach is to learn

6. pair program and try to explain it.

7. try not writing any code without a test

8. realize it's not possible nor reasonable to test every single thing

9. find balance and humility and the sweet spot for ROI in testing in cohesion with QA people.

zhaorui wrote [译文]:单元测试的七种境界
on Tue, May 5 2009 11:32 AM

The7PhasesofUnitTesting单元测试的七种境界作者:KarlSeguin

1.以各种借口拒绝单元测试UnitTest,比较常用的是

zhaorui wrote re: The 7 Phases of Unit Testing
on Tue, May 5 2009 11:40 AM

Would you mind I translate this article to chineses?

www.cnblogs.com/.../20090505_Phases_of_Unit_Testing.html

if you don't like this, please tell me.

#.think.in wrote #.think.in infoDose #28 (29th Apr - 8th May)
on Fri, May 15 2009 4:31 AM

#.think.in infoDose #28 (29th Apr - 8th May)

#.think.in wrote #.think.in infoDose #28 (29th Apr - 8th May)
on Sat, May 30 2009 6:12 PM

#.think.in infoDose #28 (29th Apr - 8th May)