beanz Magazine

Unit Testing

NASA

Unit testing tests a set of code with data to test with the code and details about how the code is used and operated upon.

Once you write code for a software application, how can you tell the code works? There are different ways to test code but unit testing is one common method.

A good unit test confirms a single assumption about a unit of work in your software application. The unit of work could be a single function or a set of methods in a class. The single assumption is whatever the unit of work is designed to do.

What is a Unit Test?

Unit testing is a set of code, data to test with the code, and details about how the code is used and operated upon. If you test the smallest most useful bits or unit of your code in isolation, and they all work as designed, then your software application should work.

For example, let’s say you have a function that outputs a positive integer (number) or zero when any number is passed into the function. A unit test might test the number output from the function is a positive integer or zero. The unit test might pass a negative integer and zero to the function to see how the function responds. If your function produces a positive integer or zero and does not generate errors when a negative integer is passed into the function, then your code has passed the unit test.
Unit tests are run by a programmer as they test their new or updated code before submitting it for review. The unit tests are created before code is written or during the programming process.

When unit tests are created before code is written, they can work well as design documents for the code and the software application. The tests help identify gaps in the design, for example, missing requirements based on connections to other bits of code in the application. The connections to other code might not be obvious without creating a unit test or actually coding.

Creating these tests before start of coding also provides programmers with the opportunity to challenge requirements and discuss the best way to do the work.

A Simple Example of a Unit Test

What does a unit test actually look like? Here’s a simple function, in a made up language, that adds two numbers when you pass them to the function, followed by a simple unit test:

 

Become a subscriber and get access to the rest of this article. Plus all our magazine articles.

Stories also include numerous links to help parents, kids, and teachers learn more. Get access today at just $15 per year!

Subscribe Today!

Also In The February 2016 Issue

Ideas for most young kids (and their families), from board games and more offline options to online games and apps.

Computers can be programmed to make intelligent decisions. Does that make a computer intelligent?

The many pieces that make up AI have been built and used for thousands of years in many cultures.

Math circles are groups of students who come together to have fun discussing and solving intriguing math questions.

Unit testing tests a set of code with data to test with the code and details about how the code is used and operated upon.

There are several places to go online to play classic video games like Donkey Kong and Castlevania.

How we manage limited resources and share costs is an important question far beyond software development.

For twenty years, since 1996, cars have used computers to control different parts of the car.

Danny Fenjves currently is the founder of Upperline, teaching students how to turn their ideas into reality through programming.

This Computational Fairy Tale explains how loops work through the sad tale of Simon, the hapless apprentice to a blacksmith.

Links from the bottom of all the February 2016 articles, collected in one place for you to print, share, or bookmark.

Interesting stories about computer science, software programming, and technology for February 2016.