What is Automated Unit Testing?

Instructor: Benjamin Blanchard

Ben has taught ESL and web programming and has a M.S. in education.

Learn about a powerful practice that helps organize programming projects and vastly simplifies debugging: automatically testing parts of your code as you write them.

Automated Unit Testing: An Overview

Automated unit testing is an important component of test-driven development, which allows the programmer or team to continually test their code while it's being developed. It takes a bit to set it up and implement it, but it comes with many advantages, as we'll see below.

Defining Unit Testing

First things first: What is a 'unit'? A unit can be described, somewhat circularly, as the smallest testable section of code. In practice, this usually means a function, class, method, module or similar discrete programming structure. The purpose of defining units to test is to make sure that the functionality you are designing works as you expect it to. If you define units that are too small, such as a single variable assignment statement, your test will be trivial, essentially testing the features of the language you're programming in, which is not the intention of unit testing. On the other hand, if your units are too broadly defined, it can be difficult or impossible to discover the points of failure you're looking for.

Unit testing can be contrasted with integration testing. Integration testing is the practice of testing the entire system to make sure that all of the units play well together. This, of course, is likely to go more smoothly and be more productive if unit testing has already been done, so that if you find a point of failure you can be reasonably certain it's the result of integration, rather than a problem within one of the units.

Ok, so that's what a unit is, so now why should we test them? Ideally, we design functions, methods, classes, etc. to do a specific thing or set of things. Testing allows you to be fairly certain that they reliably do the things we want them to do, without having any effects that we neither want nor expect. A test usually takes the form of some code that attempts to put the unit in question through its paces in isolation, that is, outside the context of the larger application. In the case of a function or method, you'd test its output against some set of expectations based on specific input that the test sends in. In the case of a class, you'd test the entire interface that you have defined for the class.

An example in Javascript will be illustrative:

Say you have a function that takes a url and returns just the route (that is, the part after the slash following the domain name). To test that, you'd send in a few urls and then test whether the expected string was returned.

Example Javascript code showing a unit test

In the above example, we are testing a function we wrote called getRoute, which should take a string containing a url and return just the part of the url after the last slash. To do this, we've created a function called testRoute, which takes a url string and the expected route string as arguments and returns a boolean value: true if the second argument is equal to the return value of getRoute when invoked with the first argument.

You can probably see that it would quickly become an overwhelming task to write a test function for every function you want to test.

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create An Account