Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are key points to explain Unit Testing

Tags:

unit-testing

I want to introduce Unit Testing to some colleagues that have no or little 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?

like image 516
Mel Gerats Avatar asked Jan 29 '10 10:01

Mel Gerats


People also ask

How do you explain unit testing?

Unit testing is a software development process in which the smallest testable parts of an application, called units, are individually and independently scrutinized for proper operation. This testing methodology is done during the development process by the software developers and sometimes QA staff.

What is the main benefit of unit testing?

One of the benefits of unit tests is that they isolate a function, class or method and only test that piece of code. Higher quality individual components create overall system resiliency. Thus, the result is reliable code. Unit tests also change the nature of the debugging process.


2 Answers

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.

like image 147
Brian Rasmussen Avatar answered Sep 24 '22 22:09

Brian Rasmussen


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.

like image 23
JeffH Avatar answered Sep 23 '22 22:09

JeffH