views:

362

answers:

15

Hi,

I'm soon to do a 10min Grok talk on Unit Testing at my company. I've been trying it myself, and feel that it can certainly bring benefits to the company. We already do WebInject testing in our dedicated QA team, But I want to try and sell unit testing to the devs.

So with only 10mins what would you cover and why?

  • we're a Microsoft Shop C# Web Apps, I've used NUnit in my experience.
+15  A: 

Unit testing is all about confidence.

It allows you to be confident that your code is solid, and that other people can rely on it when they're writing their own parts of a system. If you can get across that unit testing will help to eliminate the trepidation that comes with the first release of a new system, I would hope that your audience will soon become very interested.

David Grant
+1 - Excellent observation...I wish I could give you +2.
Mark Brittingham
+1 (so Mark could get +2)
Toran Billups
Damn good answer!
jcollum
+3  A: 

Try to briefly talk about the aspect of Test Driven Development: write tests first and the interfaces as you go, then implement everything.

Maybe also about continuous integration, this means that as soon as you check something into your source control system, the project gets compiled and all the tests run so the developer knows immediately if he has done something wrong.

If there are any project managers in the audience, also be so fair to tell them that unit testing will make your project take 15-30 % more time, but it will be worth it in the long run.

Gerrie Schenck
I think "proper" TDD would be writing the tests before the interfaces.
RickL
I think the two things go hand in hand actually...
Gerrie Schenck
I guess I'm talking about text-book practice... what I have seen suggests you would write the test, it should not compile, then you write the minimal stub to get it to compile (i.e. create the interface), then the test should fail, then you implement the code, etc... although of course...
RickL
in practice you will start skipping steps, i.e. it doesn't really make much difference if you implement the interface before or after the test.
RickL
+4  A: 

Here's a good format for a short talk on a technique X:

  • why you decided to try it X the first place
  • what you personally have gained from using X
  • what limitations you've noticed, things that X doesn't address

Don't "sell" or spend lots of time on the theory. Do prepare beforehand and point people to books, URLs of articles or tutorials that you think are most helpful. Those who are interested after your talk can look up the details on the Web.

Morendil
+2  A: 

You could mention that it will be a difficult learning curve, and it will feel like productivity is being impacted, but the benefits are worth it:

e.g. effectively the creation of an automated regression test suite, which in turn allows you to make bigger additions or modifications to existing without worrying that you are breaking some existing functionality.

Creation of production code will be slower, but this should be offset by the higher quality of the code, i.e. fewer bugs, which in the long run means overall higher productivity.

RickL
A: 

From a business perspective, you may want to highlight the fact that unit tests can "de-risk" any changes you make to your code. Once you have a suite of unit tests, you can make changes to the code base and know what breaks and what doesn't.

I might not be a bad idea to go over user testing. If you have a good set of tests, you can bring failing tests to the users after you make changes to have them validate that the new results are correct. Additionally, you can streamline requirements gathering if you have the users write new unit test definitions for you. They don't need to be able to code, but they do need to be able to give you the appropriate inputs and expected outputs (otherwise how would they know if the changes they asked for were working?).

Visual Studio has a pretty nice set of tools for unit testing, so an example or two may go a long way toward giving your group an idea of what unit testing is like in practice.

Mark
+8  A: 

I'd start with a problem a lot of programmers might be familiar with: that is the fear of making a change to existing code because of the fear they might break something. How that prevents work from happening, or prevents it being done properly (because they're afraid to refactor) and so leads to having to rewrite everything every x years.

Unit Testing -> Refactoring -> Living Code.

Edit: btw, I would Not lead with the 'all code without unit tests is legacy code' quote from Michael Feathers. It certainly made me feel defensive the first time I heard it. By the time people stop feeling affronted the 10 minutes will be over :-)   (personally I think that quote is more true than it is helpful).

it depends
+1  A: 

Accountability, as highlighted by Kent Beck, is another trait that unit testing facilitates. Listen to his podcast at IT Conversations. (His point on accountability starts at 30:34.)

Ola Eldøy
+1  A: 

I think 10 minutes is enough to present a simple example of how unit testing can save you time.

Implement a class (you can TDD if you feel like it) and show how a unit test can catch a modification that breaks the class.

Also, you can highlight how you can be faster developing components if you test the isolatedly (i.e. you don't need to bring up your web application, log in, go to your functionality, test); you can just run your tests.

You might be able to perform this on a piece of code from your company- and maybe show how a unit test might have caught a bug you have had recently.

alex
+1  A: 

If you give a demonstration, do it on a working piece of code from a project that everyone is familiar with. Avoid contrived examples. Books on TDD are already full of them, and they don't really sell how TDD can work for a real project.

Bill the Lizard
A: 

Wear this t-shirt ;-)

toolkit
A: 

Well prepared live demo:

  1. Find a "bug" in your "application"
  2. Write a unit test that covers this bug.
  3. Fix this bug
  4. Show, your code is green.

So you can prove, that there's no way, that this bug will occur once more!

furtelwart
A: 

Another way to do this:

Propose an issue that can be solved by creating an algorithm. Something relatively simple, of course. Next, code this algorithm in a DLL project. Try to sneak in some weaknesses (i <= array.Length is always a good one). Next, ask them how they would test this DLL.

Most devs run their apps to test them. But you can't run a DLL. You might get some suggesting you create a console app to create methods that exercise the algorithm. Show them how you can craft unit tests to do this.

Will
+1  A: 

For the love of god, emphasize that unit tests are for testing "units" of logic. I hate looking at a QA suite of NUnit tests that nobody expected to have to maintain, where each "unit test" tests valid outputs for 150 (binary) input files and then shits itself if one fails, without telling you which one.

Oliver N.
+1  A: 

I would demonstrate:

  • The confidence it gives in code you produce
  • The confidence it gives when you change code because it passes the unit tests
  • The benefits of code coverage, no more "Oh that else statement was never tested!"
  • The benefits of running unit tests per each build on a CI platform like Hudson

FWIW we run the crappy visual studio testing via MSTEST on our Hudson box and I've got an xslt that Hudson uses to convert the results to the nunit format so Hudson can decipher them. Just putting that out there in case they want you to stick with a Microsoft testing platform.

Allen
A: 

Have a good set of resources for follow up/self directed learning:

  • the pragmatic unit test for java/c# are good books on the subject
  • the kent beck paper on unit testing
  • links to any larger samples using the testing framework of choice
MikeJ