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
When we're talking about testing, what are we talking about? What's a unit test? Heck, what's a *unit*? Let's talk about a couple of these concepts and exactly what we're going to cover in this course.
[MUSIC]
0:00
Spend any time learning about programming,
and
0:04
you're gonna come across the acronym TDD,
which stands for Test Driven Development.
0:06
In TDD, once you know what you're going to
build, you write the test for
0:11
it before you write the code.
0:14
For some people, this works.
0:17
For others, it doesn't.
0:18
Don't feel bad if TDD doesn't make sense
to you right now.
0:20
What's important is that you write tests,
not whether you write the test before you
0:22
write the code, or whether you write tests
for code that you've already written.
0:26
So what is a test?
0:30
In this course, when I talk about tests,
I'm really talking about unit tests.
0:31
Well that brings up another question, huh?
0:35
What the heck is a unit?
0:37
A unit is a single feature or aspect of
your code.
0:39
Say you have a class that represents a
car.
0:43
It has a make, a model and a year.
0:44
It also has attributes like top speed and
fuel capacity and maybe it has methods
0:47
that let you start and stop the car's
engine or drive the car.
0:51
Each of those things, each attribute and
each method, would be a unit and
0:55
each would need at least one test for it.
0:59
Since these tests correspond to a unit of
code, we call them unit tests.
1:01
For example, our car might have a top
speed attribute which
1:06
should always be set to a number.
1:08
We can write a test that sets it to a
string like really fast, and
1:10
make sure our code raises an exception
when that happens.
1:14
There are other types of tests too.
1:17
Regression tests make sure that previous
mistakes don't happen again and
1:19
integration tests run your code through a
battery of uses to make sure that
1:22
a certain process, one that uses multiple
units, works from beginning to end.
1:26
For example, if you wanted to test the
registration flow of a Flask app,
1:30
you'd have a test that loads the account
creation page, fills out and
1:34
submits the form, follows the redirect,
and
1:36
checks that the user model was created,
and that you ended up on the sign in page.
1:38
We're gonna focus on unit tests in this
course, but
1:43
you can use the tools we talk about to
write all three kinds of tests.
1:45
Okay.
Now that we know what a test is,
1:48
let's go look at how to write the simplest
type of test, a dock test.
1:50
You need to sign up for Treehouse in order to download course files.
Sign up