Register for Prevent bottlenecks during mobile development on September 13
Register
White arrow point right
Testing

Unit Testing vs. Automation Testing: A Beginner's Guide

Carlos Schults
Carlos Schults
Unit Testing vs. Automation Testing: A Beginner's Guide
September 6, 2022
6
min read

Testing is crucial in software development, yet its increasingly huge lexicon can feel quite overwhelming. In this post, we'll talk about the common "unit testing vs. automation testing" dilemma that confuses many a beginner in the hopes that we can make your testing learning journey easier. By the end of the post, you will understand:

  • what both "unit testing" and "automation testing" mean
  • how unit testing interacts with other types of automated tests
  • the value that unit testing/automation testing provides to you

Most importantly, by the end of the post you'll know why the "versus" in "unit testing vs. automation testing" doesn't quite make sense. But we're getting ahead of ourselves! Now it's time to start covering some fundamental definitions.
Let's dig in.

Unit Testing vs. Automation Testing: The Fundamentals

Before understanding how unit testing and automation testing compare to each other, we have to take a step back and understand what these two are in the first place.

What Is Unit Testing?

Unit testing is a type of software testing in which we verify whether small parts of an application are behaving the way we expect. These small parts are the "units" from which this type of testing gets its name. Unit tests are a particular form of testing in that they exercise these units in complete isolation. The units don't interact with each other, nor do they talk to external dependencies, such as databases, the filesystem, HTTP APIs, etc.
In more practical terms, think of a unit test as a small program that tests another program.
The workflow of unit testing goes roughly like this:

  • Software engineers write unit tests using a programming language and a unit testing framework.
  • Then, the tests are discovered and executed by a unit testing runner.
  • The unit testing runner verifies the success or failures of the tests and displays the results visually.

Typically, unit testing frameworks use colors to express the failure (red) or success (green) of a test execution. Popular unit testing frameworks include:

  • JUnit for Java
  • NUnit for .NET
  • Jest for JavaScript
  • pytest for Python

Why do people write unit tests? Well, they bring many benefits to the table:

  • Unit tests run fast; thus, people will execute them often.
  • They are cheap to write since they don't require complex setups or test data.
  • They serve as a regression test suite which gives engineers confidence that nothing will break after a code change.
In more practical terms, think of a unit test as a small program that tests another program.

What Is Automation Testing?

Automation testing is the process or technique of testing software applications using automated tools. There's a huge variety of automated testing tools; many of them are code-based, while others don't require coding.
When it comes to automation testing, you must know the answer to the following question: What part of the test is automated? This is something that confuses a lot of beginners, so it's crucial to understand precisely what gets automated in automation testing.
Simply put, the execution and verification of tests are what get automated. Human beings still need to author the test cases, be it by writing code or through another mechanism, such as record-and-playback tools.
Why is automation testing so crucial nowadays? Automation testing is essential for the fast delivery of high-quality software. There's simply no other way you could ship high-quality code at the speed users have come to expect if you had to comprehensively test the whole application by hand before each and every release.
Automation testing gives software teams the confidence to deliver value at shorter intervals while keeping the quality high.

Unit Testing: Going Deeper

You've just seen the definitions of both unit testing and automation testing. Let's now take a more detailed look into unit testing by answering a few common questions about it.

There's simply no other way you could ship high-quality code at the speed users have come to expect if you had to comprehensively test the whole application by hand before each and every release.

Is Unit Testing An Automation Test?

After reading the previous sections, it is probable that you, an astute reader, noticed that the definitions of unit testing and automation testing have several things in common. So, is unit testing an automation test?
Indeed it is! Automation testing comes in several types: integration, end-to-end, performance, and unit testing.
That's why, in the introduction, I said that the "versus" from the title doesn't quite make sense. "Unit testing" and "automation testing" aren't competitors in any way. In fact, it's quite the opposite; unit tests are a type of automation test.

What Is the Difference Between Manual Unit Testing and Automated Unit Testing?

Another common question among beginners is this: What's the difference between manual and automated unit testing? Before answering that, though, we must take another step back and ask the question: Is manual unit testing even possible?
If you ask around, you'll see different opinions on the matter. Some argue that manual unit testing is a contradiction in terms, since "unit testing" means the automated testing of small units of code. Others maintain that it's possible to have manual unit testing.
I tend to side with the former side of the argument. To make manual unit testing possible, one would need to create some form of user interface that allows a human tester to directly exercise individual functions from the code base, trying to cover as many execution paths as possible.
Doing this would be extremely slow, labor-intensive, and expensive. It would require many hours of work to execute a single test run, which wouldn't even be repeatable.
So, my final take on this is that, while theoretically possible, the notion of "manual unit testing" is too impractical and expensive to be taken seriously.

Automation Unit Testing Example

Last but not least, let's show a simple example of what a unit test looks like using the Kotlin language. This example is from the Kotlin official documentation.
So, imagine you have the following piece of code:


class Sample() {
    fun sum(a: Int, b: Int): Int {
        return a + b
    }
}

As you can see, the example above is a simple class with a function that takes two integers and returns their sum. Now take a look at this:


@Test
    fun testSum() {
        val expected = 42
        assertEquals(expected, testSample.sum(40, 2))
    }

The above example is a unit test verifying that the sum method works as expected. It starts by declaring a variable expressing the expected result as 42. Then, the method uses the assertEquals method from the JUnit testing framework to compare the expected result to the actual result the sum method gives after receiving 40 and 2.

Automate Your Tests and Ship Better Code!

Everyone knows how vital software testing is. But acknowledging its importance and understanding how it works are two very different things. To help you on your testing journey, we wrote this post in which we discuss the "unit testing vs. automation testing" dilemma.
As you've seen, at the end of the day, there's no dilemma at all. Automation testing comes in several types, and unit testing is one of them. End of story.

Unit testing is indeed one of the most essential types of automation testing. It brings a lot of value to software development, but in my opinion, its most important advantage is that it gives engineers the confidence to change the code without fear. That leads to more refactoring, which leads to higher quality code.
However, unit testing alone cannot catch all possible defects; software teams should also leverage other types of test automation, including integration testing, end-to-end testing, and codeless test automation.
Thanks for reading!

Automate testing for your mobile app.Start building your first test for free

Subscribe to our newsletter for the latest news and resources

Thank you for subscribing to our blog!