This course will be retired on June 1, 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
Preview
Start a free Courses trial
to watch this video
Unit tests make debugging incredibly easy!
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
I mentioned earlier that one of
the great things about unit testing
0:00
is it gives us multiple
entry points into the code.
0:03
This means we can start debugging
the code just about anywhere we want.
0:07
We don't have to run the software from
where it would normally start and
0:11
try to get it to stop
at a given break point.
0:14
Let's see how this works.
0:17
We'll write another test,
this time, for the InRangeOf method.
0:19
So I right-click on InRangeOf,
click on Create Unit Tests, Okay?
0:23
I'll name this test method,
InRangeOfWithRange1.
0:30
I'll create the map object,
you say var map = new Map.
0:39
And again we use 3, 3.
0:45
Our target will be
the map location objects,
0:46
so we'll say var target = new MapLocation.
0:51
And we'll just have
the MapLocation be at 0, 0.
0:57
Now in this Assert.True,
1:02
we'll say target.InRangeOf(new
MapLocation.
1:05
And this one will be at (0, 1),
which is just one place away from (0, 0).
1:13
So, we wanna check that
it's in range of 1.
1:20
There we go.
1:26
Let's put a breakpoint right
here in the InRangeOf method.
1:28
Now, we can go back to our test,
right-click on the method and
1:34
click Debug Tests.
1:38
This only runs the test
method that we clicked on.
1:42
As you can see, we've hit the break
point in the in range of method.
1:46
If we click the step over button and
step out of the method,
1:50
we find ourselves in the test method.
1:54
We can also debug tests
from the test explorer.
2:05
Just right-click on the test or
2:08
group of tests that we want to debug and
then click Debug Selected Test.
2:09
This is incredibly handy for debugging
just the parts of the program we want
2:16
without involving the rest of the project.
2:20
Being able to begin executing anywhere
in the code makes debugging quicker and
2:22
easier.
2:27
In fact, when I'm tracking down a bug,
the first thing I usually do
2:28
is write a test to reproduce it and
then run the test in the debugger.
2:31
This kind of flexibility is just
another perk of having Unit Tests.
2:36
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