How to Get Started With Unit Tests

Finding time to get something done can be difficult. We can all think of reasons to put anything off if we try hard enough. For software developers, one such example is unit testing. For the uninitiated, unit testing is a software development process in which the smallest testable parts of an application, called methods, are individually and independently scrutinized for proper operation. In simpler terms, it’s code that tests code. The general attitude towards these tests is “I don’t have the time!” But unit testing is like meditation: You should sit in meditation for 20 minutes every day — unless you’re too busy; then you should sit for an hour. Unit testing and meditation go hand in hand because if you don’t make the time for it, you’re not the best you can be. If you don’t make the time for unit tests, your software just isn’t up to par. Taking the time to get things right makes software shine. So while your team of developers might be wincing in pain at the thought, here are a few ways to get them going on unit testing.

Don’t Be Afraid to Train – If your developers have never done unit testing before, there will be mistakes made along the way. It’s important to let them know that it’s ok; tell them that everyone is learning together. Set up training sessions, or rather, training cycles. Begin with an initial training session before setting them loose and follow it up with a “training debriefing.” Doing so will allow you to see what went well, what went wrong and what’s next. Often developers will find out their code doesn’t lend itself to unit tests and this itself can be a training opportunity. Repeat this process and you’ll iron out all the wrinkles in your unit testing.

Figure Out Where to Start – Don’t just dive into unit testing. Sit down with your team and create a plan they can stick to. This means setting up reasonable goals and timeframes. A good place to start is to challenge every developer to build two unit tests. It’s a low and reasonable goal, which also allows your team to get accustomed to building unit tests. But you don’t want to set them up for failure with complexity.

Start with Simple Code – Tasking your developers to build unit tests for complex pieces of code is not a good idea. Find simple code to build unit tests around starting off. It helps ease the transition into more difficult testing but also gives the developers a true sense of accomplishment. Your team needs to learn what works and what doesn’t; simple code starting off is a great way to build their knowledge base.

Gamification – Unit testing doesn’t have to be boring! If you can incorporate gaming elements into the process, your team will have a reason to keep building. A quick idea is to have your development teams compete for unit test coverage. There are several tools that make measuring this pretty easy and you’ll be surprised how easy it is to get a developer’s competitive juices flowing. Then maybe you could reward the team with the best unit test coverage with a night out for drinks? Even creating a dashboard where teams can see who’s beating who will be a motivator. There are endless ways to making unit testing an incentive driven activity so everyone wins!

Bonus Tip: Don’t Forget to Integrate! – It’s also important to note that unit testing should be run alongside good integration tests. The simple reason for this is because while unit testing is great to tighten up code, it’s meaningless if the improved code can’t work together. Integration testing pulls everything together and tests how it works in a real-time environment.You don’t want to end up with code that doesn’t do what was originally intended! Look at it this way; you may have all the right pieces to make a car, but if it turns out to be a vacuum after assembly, something went wrong. Integration testing makes sure the pieces of the car become an actual car.

Unit testing will make your code stronger. It helps to show the pain points in your work, which allows for a final product that has a just off the lot kind of shine. Introducing new procedures and ideas requires full company buy-in, especially for unit testing. To make the process work, talk openly about what works as you train, figure out where to start, make things easy at first and game it up. The more open and honest you are with your team, the more they will want to be a part of the process. So get going and start coding!