Unit Testing - Tips For Getting Started

Unit Testing - Tips For Getting Started

It’s a new year, so it is a good time to start planning and setting some goals for this year. If you are not currently using unit testing, starting to use it would be a great goal for the new year.

People often struggle when starting to learn unit testing. There is a hurdle, similar to learning OOP, where you don’t see the point for a while and then eventually you just suddenly get it. With Unit Testing you don’t really see the benefits initially, you need a bunch of tests to see the benefits.

I suggest that if you are serious about unit testing you take some time and educate yourself. There is already lots of good information out there for free. I highly recommend the Unit Testing Group on the NI Forum. I also recommend following Fab and James McNally. They put out lots of good stuff on Unit Testing. You should also check out all my posts on unit testing if you haven’t already. There are also several books worth reading.

Another thing I highly recommend is to join some sort of community. I’ve started a LabVIEW Mastermind group, which meets twice a month. We deal with these types of issues a lot. Here is a video of one of our members talking about his struggles with Unit Testing. If you have tried to teach yourself unit testing, you probably can relate.

If you like what you see in the video above come check out our Mastermind Group. Click the button below to go to the landing page.


Aside from all the reading material above and finding a group to join, what else can you do? Here are a few tips.

1. Start Small

While it is true that it is hard to see the benefits of unit testing until you have a lot of tests, the best way to start is to start small. Don’t try to test everything. Simply pick some chunk that is small, easy to test, and isolated from the rest of the codebase. If you are fixing bugs in existing code, start by writing a test to catch the bug. Eventually, you will end up with more and more unit tests.

2. Start Fresh

Retrofitting unit tests into existing code is hard because the code wasn’t designed with testing in mind. If at all possible when learning unit testing, start with a fresh project. Writing the tests will be much easier. Once you learn how to effectively write unit tests for new code, retrofitting tests into older code will be much easier.

3. Write the tests first

If you are starting with a new project or adding a feature to an existing project, if possible, write the tests first. At the very least think about how you are going to test it first so that your design is testable. I did a talk on Test Driven Development at GDevCon#2. See the video below.

4. Be Selective about what you test

In the beginning, don’t try to test everything. It will be too overwhelming. Pick the things that will get you the most results. Start with critical code and reuse code. Avoiding testing private methods inside libraries and classes. Testing the public methods should be enough to exercise them. Another place you can see a lot of benefits from unit testing is by testing external interfaces, such as instrument drivers. They can be a little tricky to test (Test Doubles are your friends here), but the payoff can be huge as explained in the video below.

5. Stop Asking Permission

I recently wrote a whole post just about this, so I’ll just link it here to avoid repeating myself.

6. Take a Class

We are offering a Unit Testing workshop at the end of January. In our 16 hour workshop, you will learn everything you need to get you started writing unit tests in LabVIEW. We are currently offering it over Zoom. We also run a shorter 3 hour Intro to Unit Testing workshop geared towards LabVIEW beginners. Use the buttons below to check out the details and sign up.