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
What TDD does, is force you to think about what your code looks like at the unit level. This doesn't mean these modules we'll always be used in the exact way you planned. It's like building with Lego. Unit testing verifies that each piece is compatible so that your application can then use those pieces when needed.
To learn more about TDD, check out Test Driven Development by Kent Beck
[MUSIC]
0:00
When you start learning about testing,
you very quickly start hearing about
0:04
a practice known as test-driven
development, or TDD.
0:09
It's the practice of writing a test for
0:13
a small module or
unit of code as if it worked already.
0:16
Then write the code
until that test passes.
0:21
Repeat until you have tests for
every unit of code that you've written.
0:25
What TDD does is force you to think
about what your code looks like
0:30
at the unit level.
0:35
This doesn't mean that these modules
will always be used in the exact
0:37
way that you planned.
0:41
It's like building with Lego.
0:42
Unit testing verifies that
each piece is compatible so
0:44
that your application can use
those pieces when needed.
0:48
Let's go build some units
from a test driven approach.
0:52
We're going to write a simple
English to Pig Latin translator.
0:57
Pig Latin is a game in which
English words are altered
1:02
usually by moving the first consonant or
1:06
consonant cluster of a word to the end
of the word and then adding a y.
1:09
So treehouse would become eehousetray.
1:15
Let's write the code for
this translator in a test driven approach.
1:19
In test driven development,
we write our test first and
1:23
then write the code until our test passes.
1:27
Let's add a new file in tests.
1:31
We'll name this PigLatinTest.php.
1:33
We're going to use PHP unit framework,
1:40
TestCase.
1:48
And then we'll add the class
1:50
PigLatinTest extends TestCase.
1:55
We'll add a .clock with
the annotation test.
2:01
And then we're ready to
add our first function.
2:07
The main functionality of our
converter is to take a single starting
2:12
constant word and change it to pig Latin.
2:17
So
convertSingleStartingConsonantWordToPigLa-
2:20
tin.
2:30
Now let's walk through how we expect
to use our PigLatin converter.
2:34
We'll have an English word that we
want to convert, and we'll use test.
2:40
Then we'll need our expected result.
2:46
And this will be esttay.
2:50
Next we create a new pigLatin object.
2:54
Then we're going to get the result
of calling pigLatin->convert.
3:02
And passing the word.
3:12
Now we're ready for our assertion.
3:16
Instead of assertTrue,
3:19
we want to make sure that the result
equals our expected result.
3:21
So we use $this->assertEquals
3:26
the $expectedResult and the actual result.
3:30
And the third parameter is optional,
but allows us to add a custom message.
3:36
PigLatin conversion did
not work correctly.
3:43
Now that we have our first test,
we're ready to run our code.
3:51
Vendor/bin/phpunit tests/PigLatinTest.php.
3:55
Let's fix line 9.
4:04
First, it fails because there
is not a Pig Latin class.
4:12
Let's add a PigLatin class
to our source directory.
4:18
Class, PigLatin,
now let's run our test again.
4:29
It's giving us the same error
even though we have a class file.
4:37
That's because of the way
we are autoloading.
4:42
If we look in vendor, composer,
and our autoload_classmap,
4:45
we can see that our
email is being included.
4:52
But nowhere in here is
it adding our Pig Latin.
4:57
We need to run Composer update
again to grab our Pig Latin file.
5:04
You only have to do this when you
add files you need in your classmap.
5:16
If you're using proper name spacing for
5:22
your project, this is a different way
of telling Composer where to look for
5:23
class files, and you don't need
to update Composer each time.
5:27
We could also require the specific class
we need directly in our test files.
5:31
We could require once,
5:45
Source, PigLatin.
5:52
But we already have our auto-load working,
so let's comment this out.
5:57
Now let's run our test again.
6:02
This time, it tells us that we
do not have a convert method.
6:05
So let's add that.
6:09
The function convert should accept a word,
And
6:19
then for
now we're just going to return the word.
6:24
Now let's run the test again.
6:31
This time we get a normal failure message,
6:34
we were expecting esttay, but
instead, we just got test.
6:38
You need to sign up for Treehouse in order to download course files.
Sign up