2.2 Test-Driven Development
In this lesson we'll learn about one of the most popular testing methodologies, Test-Driven Development, where we write our tests before we write any application-specific code.
1.Introduction3 lessons, 05:32
2.Testing Methodologies3 lessons, 24:26
3.PHP Testing Frameworks3 lessons, 08:25
4.Test Types3 lessons, 36:05
5.Conclusion1 lesson, 00:36
2.2 Test-Driven Development
In the last lesson, we reviewed the common testing process that many beginning PHP developers use to test their web applications. Many times, they begin by writing their application code, and they test its functionality in the browser to determine if everything's working as expected without writing any actual tests. When we run into a problem with our application, we may or may not have any errors or hints as to what the problem could be when using this testing process. Instead, we have to revert back to our application code to manually debug the problem. As we've seen in the last lesson, this does have several disadvantages. Instead, we can write separate PHP files that we can use to test our application code with. Our test code will provide us with feedback should our application not run how we intended it to. So for this lesson, we're going to do just that, but with a twist. We're going to write our tests using a test-driven development process or TDD for short. In which, we'll let our tests drive our applications development. We'll write our test first, then write our application code. This ensures that every new feature that we implement within our application is tested, and allows you to stay focused and on track. Before we begin coding, let's review some basic test-driven development guidelines that we can adhere to while writing our tests. So basically, with test-driven development, the idea is to let our tasks drive our development. We start by writing a failing test for a new feature that we want to develop. Next, we need to run our test and actually watch it fail. By seeing this failing test, we know exactly what we need to do, implement the new feature. Now what I meant by a failing test, is that the test itself will check for that feature within our application and since the test was written beforehand, the test will fail, giving us direction. Next, we'll write the least amount of code needed in order to get the test to pass. At this point, we'll begin writing our application code, but only enough to make that test pass, no more. By writing only the smallest amount of code that we need, it keeps us from getting too complicated too soon, and keeps on track of what we're really trying to do, as not to get sidetracked. Then lastly, we need to re-run the test to see if the application code that we wrote makes the test pass. We then continue with this development process to develop the entire application. Now we're not going to develop an entire application here. But I will show you a simple example of TDD in action using PHP. Most of the time, you'll write your tests with a testing framework of some kind and we will be reviewing three popular PHP testing frameworks in the next section. But for this example, we're just going to write our own custom test file. But in the final sections of this course, you'll be able to see how a framework handles writing tests and how it offers you many additional cool features that you can test with. Okay, let's begin. For this lesson, I'm going to continue using my phptestingbasics folder that I created previously. Although I just got rid of the app.php file that was in there from before. Now to illustrate the TDD process without introducing other distracting or complex application code. Let's create a very basic method called returnsTrue that just returns true. It's just like many of the common PHP methods that we use everyday in our web applications. Simply returning true or false when we call them, and testing them would be exactly the same process. Except here, we're just simplifying the methods logic. We can then test to ensure that it does as we expect. Now I know it seems overly simple, but nonetheless, it will be a good feature to practice TDD with. So we need to start by creating a failing test, to test for our new feature. Which, doesn't actually exist yet. Remember, we let our tests drive the development. So let's create a new file. Under the phptestingbasics folder, I'll create a new file. I'll save this as test.php. [BLANK_AUDIO] All right, so we want to write a test to make sure that we have a method named returnsTrue. And that it does in fact return true. So within our test.php file, let's write a method that we can use to test that this new returnsTrue method does exist. So I'll create a new function named testReturnsTrueMethod. And there we go. So now to test whether or not we have a returnsTrue method, we should first attempt to call it and make sure that it is returning true. We can do this by using it in a conditional. If it returns true, we can print out a success message saying that the test passed. If not, PHP will complain and tell us that our method is undefined. We'll also use an else statement if the test fails and does not return true, and we can then print out a helpful error message so we know where are the problem lies. So let's do that. Inside of our testReturnsTrueMethod, let's create an if statement. And then we're going to call our returnsTrue method, which we haven't actually wrote yet. So if that returns true, let's just print out a success message saying that the test has passed. So I'm just going to echo out a string, I'll put it inside of a paragraph, then we'll say Passed returnsTrue exists and returns true. Make sure to close your paragraph tag and let's also use an else statement here, and let's echo out an error message this time. Okay, now that we've written our test, we need to run the test and watch it fail. Now, you may actually want to extract your test file which is this file right here, extract that into a separate file form the file that actually runs the test. And this is exactly what the testing frameworks do that we're going to examine later on in the course. But to keep this simple, I'm just going to run my tests right from the same file. So right after our method, let's call it, testReturnsTrueMethod. Perfect. Let's save our file and make sure your web sever is up and running. And I'm just going to switch into my browser. And let's load up our test. We should be able to go to localhost port 8000/test.php. And perfect. This is exactly what we expected. We get a PHP error saying that our method is undefined, since we have not yet created a returnsTrue method. Let's do that now. Let's go back into Sublime. And inside of our phptestinbasics folder, let's create a new file. I'll save this as app.php. Now, remember, we just need to write the least amount of code to get this test to pass. We don't want to get too far ahead of ourselves and write too much code. We wanna stay on track, and let our tasks guide our development. So what would be the least amount of code to get our test to pass? Or to at least get rid of that error that we seen? Well, that would be simply to create, or define, the returnsTrue method. So inside of our app.php file, I'm going to open up my PHP tags here and let's create a new function named returnsTrue. And that's all the code we need in order to get that PHP error to go away. But it order to test our file, we need to include it within our test.php file, otherwise, we won't have access to our returnsTrue method here. So let's go back into test.php and at the top, ss let's include it, include app.php. Let's now re-run our test. Let's switch back into Firefox and we'll refresh our test page. And great. This time, we no longer have a PHP error complaining about an undefined method. This time our test printed out our error message saying that, the returnsTrue method did not return true. So now we know exactly what we need to do. We need our returnsTrue method to return true. So let's go back into our text editor and let's go into app.php, and let's simply [SOUND] return true inside of our method here. We'll save the file. Now lastly, let's run our test one more time and see if our test passes. I'll switch back into Firefox. And let's refresh the page. And perfect. Our test passes. So now you can always refer to your test, should anything go wrong with your application. You can run your tasks once something's acting up to see if any tasks fail, and if you see any failures, your tests can provide you with hints as to where the problem might be. In this case, should we accidentally break our returnsTrue method, our tests will catch it, and print out our error message. We then know exactly where to look for the problem. So, you can now use this development process of test-driven development to guide the creation of your entire application. Now, remember that this was just an introduction to TDD, and there's a lot of different ways to implement this type of development process. The one that I showed here was just the basics. The point here was to learn the concept of how to develop your applications using TTD so you can be more confident about the code that you write. Next up, we'll look at behavior-driven development. I'll see you there.