tags:

views:

190

answers:

6

I want to introduce Unit Testing to some colleagues that have no or few experience with Unit Testing. I'll start with a presentation of about an hour to explain the concept and give lots of examples. I'll follow up with pair programming sessions and code reviews.

What are the key points that should be focussed on at the intrduction?

+2  A: 

You might get some inspiration here too http://stackoverflow.com/questions/581589/introducing-unit-testing-to-a-wary-team/581610#581610

Gerrie Schenck
+1  A: 

Remember to point out that Unit Testing is not a silver bullet and shouldn't replace other forms of traditional testing (Functional Tests etc) but should be used in conjunction.

Unit testing works better in some areas than others, so the only way to have truly comprehensive testing is to combine it with other forms.

This seems to be one of the biggest criticisms I see of Unit Testing as a lot of people don't seem to 'get' that it shouldn't be replacing other forms of testing in totality.

RSlaughter
+4  A: 

To keep it really short: Unit testing is about two things

  • a tool for verifying intentions
  • a necessary safety net for refactoring

Obviously, it is a lot more than that, but to me that pretty much the sums it up.

Brian Rasmussen
A: 

Main points:

  • unit tests help both design (by expressing intent) and regression test (by never going away) code;
  • unit tests are for lazy programmers who don't want to debug their code again;
  • tests have no business of influencing or affecting business logic and functionality, but they do test it fully;
  • unit tests demand the same qualities as regular code: theory and strategy, organization, patterns, smells, refactoring;
grigory
+2  A: 

Unit tests test small things

Another thing to remember is that unit tests test small things, "units". So if your test runs against a resource like a live server or a database, most people call that a system or integration test. To unit test just the code that talks to a resource like that, people often use mock objects (often called mocks).

Unit tests should run fast and be run often

When unit tests test small things, the tests run fast. That's a good thing. Frequently running unit tests helps you catch problems soon after the occur. The ultimate in frequently running unit tests is having them automated as part of continuous integration.

Unit tests work best when coverage is high

People have different views as to whether 100% unit test coverage is desirable. I'm of the belief that high coverage is good, but that there's a point of diminishing return. As a very rough rule of thumb, I would be happy with a code base that had 85% coverage with good unit tests.

Unit tests aren't a substitute for other types of tests

As important as unit tests are, other types of testing, like integration tests, acceptance tests, and others can also be considered parts of a well-tested system.

Unit testing existing code poses special challenges

If you're looking to add unit tests to existing code, you may want to look at Working Effectively with Legacy Code by Michael Feathers. Code that wasn't designed with testing in mind may have characteristics that make testing difficult and Feathers writes about ways of carefully refactoring code to make it easier to test. And when you're familiar with certain patterns that make testing code difficult, you and your team can write code that tries to avoid/minimize those patterns.

JeffH
A: 

Unit tests should be FAIR.

  • F Fast
  • A Can be easily Automated
  • I Can be run Independently
  • R Repeatable
fastcodejava