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