Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
There are many benefits of unit testing.
-
0:00
In the previous video we learned that unit tests demonstrate that the code
-
0:04
written up to a given point is correct and they give us confidence to move on.
-
0:10
There are many more reasons to unit test.
-
0:11
Unit tests help to discover design flaws early in the coding process.
-
0:17
Unit test should always be written by the developer of the code.
-
0:21
Often, when we start coding we may not be thinking about
-
0:24
all the possible ways that the code could fail.
-
0:27
There are often corner cases that we may not have considered.
-
0:31
This can affect how the software is designed and coded.
-
0:35
When writing unit tests, we're forced to think about these things and we end up
-
0:39
with a more intimate knowledge of the code and the problem we're trying to solve.
-
0:44
Unit tests find runtime errors.
-
0:47
Runtime errors can cause the software to crash or to stop working.
-
0:51
In C# the most common example of a runtime error is an improperly handled exception.
-
0:57
By testing the code at the unit level, we can get much better visibility regarding
-
1:03
where exceptions may arise and how they should be handled.
-
1:06
In scripted and non-compiled languages such as Python or JavaScript, unit
-
1:11
tests are used to discover coding errors that could have been caught by a compiler.
-
1:16
Unit tests find logic errors.
-
1:18
Just because the software appears to work on the surface and
-
1:21
doesn't crash, doesn't mean that it's correct.
-
1:24
The software may still produce the wrong output or
-
1:28
display the wrong information to the user.
-
1:30
This is most likely due to errors in the way that
-
1:33
the logic of the software was coded.
-
1:36
When doing unit tests, we try to exercise the code
-
1:39
as many different ways as possible by providing it different sets of inputs.
-
1:44
For each input, we check to see if we get the expected output.
-
1:48
Unit tests provide multiple entry points into the code.
-
1:52
The entry point of a piece of software is where the code starts executing
-
1:56
when it's launched.
-
1:57
For example, the entry point of a console application is always the main method.
-
2:02
Unit tests give us the ability to run every unit of code
-
2:05
independently of the rest of the software.
-
2:08
This in essence provides multiple entry points into the code
-
2:11
that we wouldn't otherwise have.
-
2:14
This makes it very easy to isolate and debug the code, when we discover problems.
-
2:19
This is especially important when coding a library,
-
2:22
because libraries don't have entry points.
-
2:25
Unit tests are part of the documentation of the code.
-
2:28
When working with code, that you didn't write yourself.
-
2:30
It's important to have some sort of documentation, to learn how to use it.
-
2:34
If you're lucky, someone will have taken the time to write some good documentation.
-
2:39
Documentation can be hard to keep up to date though.
-
2:42
In the absence of good documentation, you can find examples of how to use the code
-
2:47
by looking at the unit test, provided that the developer wrote unit tests of course.
-
2:51
Running unit tests is easy to automate because they are written in code and
-
2:56
saved in files.
-
2:57
They can be run and re-run as often as we want.
-
3:00
We'll see many examples of that in this course.
-
3:03
The best developers in the world regularly make mistakes and that's okay.
-
3:08
Even the most experienced developers would never overlook testing though.
-
3:12
There's an old saying that claims an ounce of prevention is worth a pound of cure,
-
3:17
and that is so true for unit testing.
-
3:20
It often shortens the overall time it takes to finish a product
-
3:23
by building quality in early in the process.
-
3:26
At the very least,
-
3:27
it reduces the amount of uncertainty in a project, especially at the end.
-
3:31
With all of these benefits, it's easy to see why unit testing is a critical
-
3:35
part of the software development process.
You need to sign up for Treehouse in order to download course files.
Sign up