As I mentioned in a previous post, I recently gave a talk on Test Driven Development at the April NHJUG meeting. A few people have asked me for the slides so here they are. I experimented with recording the talk as well so you can see the results of that experiment as well.
At a high-level, the talk centered around the following topics:
- What is TDD?
- The developer workflow
- In depth, live coding demo of the developer workflow
- Some tips for test implementation, maintenance, and refactoring
- Lots of great interactive discussion from the audience!
The talk was a long one clocking in at over 2 hours so I appreciate everyone who hung in for the duration. There is a bunch of content in the slides that we didn’t talk about so you may find some interesting stuff in there even if you attended the talk.
NHJUG Talk on Test Driven Development from David Ehringer on Vimeo.
While the talk was over two hours in length, this video captures just the first hour or so. So a bunch of topics as well as some of the great discussion are unfortunately not included in the video but you can see the slides above.
0:45 – Background
4:40 – Agenda
6:15 – What is TDD?
8:40 – TDD Workflow
22:25 – TDD Workflow Demo
56:15 – Measuring TDD: Test Coverage, Code Quality, and Other Tools
I recently published a small, open source project that I’ve been messing around with for probably close to two years now but never got around to cleaning up and publishing. It’s called xml-matchers and is a collection of Hamcrest matchers for matching against XML documents. I primarily created it for testing but I’m sure you could find other uses for it. The project is hosted at Google Code: http://code.google.com/p/xml-matchers/.
The matchers allow you to do things like:
Source xml = ...
Node xml = ...
String xml = "<mountains><mountain>K2</mountain></mountains>";
String xmlWithSpaceWrappingText = "<mountains><mountain>\n\tK2\n\t </mountain></mountains>";
Schema schema = w3cXmlSchemaFromClasspath("org/xmlmatchers/validation/example.xsd");
String xml = "<person private=\"true\"><name>Dave</name></person>";
Being the TDD proponent and enthusiast that I am, I’ve given my share of the requisite “What is TDD?” presentations. I’ve promised to share some of them but have never gotten around to it. So here is one I’ve given two or three times over the past year. I know there are a couple typos so please ignore them. Otherwise, feedback is always welcome!
The presentation is also available on Slideshare at http://www.slideshare.net/dehringer/test-driven-development-5785229.
Parameterized tests is one of the features that have come along in the JUnit 4.x releases that I don’t find myself using very often but when I do, I find tremendously useful. In a nutshell, parameterized tests allow you to run the same test over and over again using different values. I’ve found this approach very useful when you are testing a function and have a table of data defining the input value(s) and the expected result. That being said, parameterized tests can be used for testing more than just pure “functions.”
This post walks through a detailed example of using JUnit’s parameterized tests.
Many people have asked how much time and effort they should spend testing the seemingly “trivial” code that they write. When I first started writing tests, I myself wondered about what types of tests were appropriate to write and how much benefit I would get from testing what seemed to be very trivial code. If the code I am writing is so simple that it can’t possibly be wrong, why should I spend time writing tests for it? What value do those tests provide? A classic example is verifying that preconditions are enforced during object instantiation. Continue reading
I would say that I fall into the camp that generally shies away from the use of singletons. To start, singletons are a form of global state that is to be avoided. They carry state across the entire execution of the program making both testing and debugging difficult. It becomes hard to execute tests in isolation because they may modify the state of the singleton in ways that undesirably ties tests to a strict execution order. Beyond problems with global state, objects that use singletons are highly coupled to the both the way the singleton is instantiated and the way it is implemented. It isn’t possible to test an object that depends on a singleton without testing (or in someway relying on the behavior of) the singleton as well. Furthermore, this coupling is hidden from the user of the object. To correctly interact with the class depending on the singleton, a dependency that is not explicit in its interface, you must often also understand the behavior of the singleton. In other words, the state and behavior of the singleton can affect the behavior of the depending object as well as transitive side effects. Continue reading