Simple Unit Testing with NetBeans

As we all know, real software developers use Unit Tests to help sanity check their code. Like all things, Unit Tests have their pros and cons.

Pro: Good unit tests will help you detect when a code change has broken the build in some obscure way
Pro: Writing them will help you better understand how your code works together
Pro: They will simplify testing to just the click of a button
Pro: You will be one step closer to becoming a Priest of the Temple of TDD

Con: It just sounds like so much work

It is true, unit tests can be tedious to create, and it seems easier to just not do it. After all, while everybody isn’t perfect, I sure am, amirite?

Or, you could just be a good person and do your due diligence in making unit tests. You’ll thank yourself later. I know my very first unit test in DMP Photo Booth uncovered a typo that would have gone undetected for a long time, and likely given me a few gray hairs.

The good news, is that it’s really not that hard. With NetBeans, much of the boilerplate is automated. The even better news is that you don’t even have to come up with some fancy framework to make decent tests. I’ll tell you how!

Creating a Unit Test

First, in NetBeans, in the projects pane, right click the source file you’d like to create tests for. Click Create Test > New C Simple Test ...

Select the functions you’d like to create tests for, and click Next >

In the next step, there are two fields you need to be concerned with: Test Name, and Test File Name. Test Name is the name of your test package. You can think of this as a folder that holds your tests. I like to call mine [PROJECT_NAME]_tests. Test File Name is the actual unit test source code file. I like to call mine [FILE_TO_BE_TESTED_NAME]_tests. However you configure these fields, you should consider clicking Finish when you’re done.

When you click Finish, a new test file will be created. As it stands, if you run your test, it will always fail. You need to do some configuration.

First things first; each function has a corresponding test function with a signature of:

void test[FUNCTION_NAME] ();

Each test function requires some customization to work correctly. Each test function sets up for, and calls the function it is testing. Your job is to tell it how to do all of this. Give it some test data, and in the if block, tell it what should be returned. The idea is that if the result check resolves as false, the test has failed.

After you have set up the failure check, you should customize the failure message. You can do this by modifying the end of the printf within the failure message. At the end of the message, you should see a section that says message=error message sample. This is what is displayed by NetBeans upon a test failure. You should customize this bit to say something useful. Since this is printf, you can use token replacing to customize this. You could probably go with something to the effect of ... message=Test failed, returned: %d\n", result); and be in good shape.

Either way, when you’re done save and close your test file.

Running Your Test

Now the easy part: running your tests. This is as simple as right clicking your project in the Projects pane, and clicking Test. Your tests will run, and if all is well, they will all pass. If not, then you’ve caught an error early.

Either way: mission accomplished!

2 responses to “Simple Unit Testing with NetBeans”

  1. jhuhtanen says :

    Complex refactoring of code is much much harder if you haven’t done any unit tests. It’s your safety net when you fall (and sometimes you just do). I have a good example: If you have been assigned to change behavior of some service in an old legacy app. Would you feel safe to mess around if it was fully covered by unit tests or no unit test at all? ;) Good post!


Trackbacks / Pingbacks

  1. DMP Photo Booth: Crunch Time | Doing My Programming... - March 3, 2014

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: