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
Behavior Driven Development (BDD) is an approach to building software. In BDD, instead of writing code and then seeing if it works, you write tests first and code second. This means you can use tests as an outline for the application code you actually want to write.
Video review
- Behavior Driven Development is like creating a plan before you write your program
- Start by describing how the program should work, then write code until it works the way you expect it to work
- If your tests are good, you can be confident that your functions really do what they’re supposed to do
Red > Green > Refactor
- Write the tests, even though at first they’ll fail
- Fix the function in the first way that comes to mind, just to get your tests passing
- Go back and refactor the function for improvements, repeating the cycle until you’re ready to move on
So we're almost ready to dig
into writing some unit tests.
0:00
I hope you're excited as I am.
0:03
Before we do I want to talk about
Behavior Driven Development or BDD.
0:05
BDD isn't a type of testing
like unit testing or
0:10
integration testing, instead BDD is
an approach to building software.
0:13
If you've ever written an essay or
0:18
a story you probably noticed that it's
really hard when you don't have a plan.
0:19
You forget what you wanted to say or
0:24
you don't say quite what you mean even
if you spend a lot of time writing.
0:26
So it helps to start your writing process
with a brainstorm or an outline, so
0:31
that you can always have
an idea where you're headed.
0:35
Behavior driven development
is like creating a plan
0:38
before you write your program.
0:41
Instead of writing a bunch of code and
then seeing if it works,
0:43
like a lot of programmers do, with BDD
you write test first and code second.
0:46
Now this may seem counter-intuitive but
0:52
BDD makes a start by describing
the behavior of parts of a program.
0:54
How should this function
behave when something happens?
0:59
What should we expect to get back from
this function if we give it a number
1:02
instead of a string?
1:05
In other words we start by describing
how the program should work,
1:07
these are our tests.
1:11
And then we write code until it
does work the way we expect it to.
1:12
[SOUND] At first our
expectations will not be met,
1:16
because the functions we're
testing don't even exist yet.
1:19
So the output of these errors will
be bright red, and that's okay.
1:23
Then step by step we code our
function until the tests pass,
1:27
that is until bright red errors are
replaced with passing green check marks.
1:32
As we fix the errors one by one
we get new errors that give us
1:37
guidance in writing the next
piece of our function.
1:41
At first you don't worry about
the code looking good or
1:44
being efficient you just
want to see the test pass.
1:47
If our tests are good we can be
confident that our functions
1:51
really do what they're supposed to.
1:54
At this point we can now
go back to our function and
1:55
change things around to
make it nicer to read or
1:58
faster to run, this process of improving
our code is called refactoring.
2:01
Now, the great part of this process is
that once we have our tests in place,
2:06
we'll immediately know when
a change to our code breaks our
2:11
application's functionality,
a red error indicating a failing test.
2:13
This development cycle is
called Red Green Refactor.
2:18
First, write the tests, even though
at first they'll fail, they might not
2:22
even compile correctly at first if they
refer to things that don't exist yet.
2:26
Second, base the function in
the first way that comes to mind
2:31
just to get our test, passing.
2:34
And third, go back and
re-factor the function for
2:36
improvements, repeating the cycle
until we're ready to move on.
2:39
In the next video, I'll show you exactly
how to do this with some clear examples.
2:43
You need to sign up for Treehouse in order to download course files.
Sign up