FREELessons: 13Length: 1.3 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

4.1 Unit Tests

Now that we know which PHP testing frameworks are available to us and have them installed, let's take a look at our first kind of test, using the awesome PHPUnit testing framework to write some unit tests.

4.1 Unit Tests

So up to now, we've learned what testing is and why it's important. We've seen a couple different testing methodologies that we can use while testing our applications. And then we examined, ever so quickly, and installed three popular PHP testing frameworks. In this lesson, we'll learn how to write unit tests using the very popular PHPUnit framework. Which will ease the testing process by providing us with a convenient test case class that we can extend from. Basically, this class will give us access to a lot of cool testing features that we can use to write our unit tests with. Additionally, when we were creating our own manual tests in the testing methodology section of the course, we had to write our own logic and messages for deciding whether or not a test failed or passed. With PHPUnit, we don't have to worry about that anymore, as PHPUnit handles it for us. And we now have fully automated unit tests available to us. All right, let's now first define what a unit test is. A unit test takes a small portion of code from your application, called a unit. And tests that it behaves according to a particular assumption. Now, when creating your unit tests, whether you're rolling your own test from scratch or using a framework, you should keep the following tips in mind. Your unit tests should be readable and maintainable. Your test should focus on testing just one behavior, not multiple. Your test should also be reliable and consistent. If you run your tests back to back, you should be getting the same result every time. As long as you're not changing the code that's being tested. If you're not, something is wrong with your tests. And then lastly, make sure that you're testing as much of your application code as possible. You wanna be confident that your code is behaving properly. All right, let's see some code. Here I am in Sublime. And here's my phptestingbasics folder, which we've been using throughout the course. Let's go ahead and create our first PHPUnit test case. We'll again be testing the exact same returnsTrue method that we tested before using TDD. Except this time, we're going to be using PHPUnit. And by using simple example code, you can focus on the core concepts of writing tests and the conventions that are imposed by PHPUnit. Rather than spending time writing application code. Now, to hold all of our PHPUnit tests, you need to create a new folder named Tests with a capital T. So inside phptestingbasics, I'll create a new folder. I'll name this Tests. There we go. And now, inside of my Tests folder, I will create a new file. I'll save this as AppTest.php. Your test file's name can start with whatever word or words that you'd like, but it must end with Test.php using a capital T. Just like I've done here. Now in this case, my test file is for an App.php application file which we'll write momentarily. This is the file that contains the code that we're testing. So I've named my test file AppTest.php. Let's now just open up our PHP tag here. And first, we'll put in the code to include the App.php file which will contain the application code that we'll be testing. There we go. This is basically the same as before, where we call the include method. But this time, we call the dirname method to create the proper path to our App.php file. Next, let's create a class. I'll name this AppTest, using a capital A for app, and a capital T for test. Where App is the name of the file that we're testing, and then we just end the class name with Test. And then we need to have this extend the PHPUnit_Framework_TestCase class. And that should be extends not extend. Okay, now let's create our first unit test. So your tests are simply public methods inside of this AppTest class. Inside of our tests, we'll use assertion methods to assert that our application behaves a certain way. And I'll give you more information on those assertion methods, in just a minute. Let's first create our first unit test. Let's create a new public function. And now, we want to make sure that we start our test method names, with the word test. And then we just give it a descriptive name of the test that we're running. In this case, were testing the ReturnsTrue method. Now, we want to assert that our ReturnsTrue function does in fact return true. Rather than having to write an if-else statement and print out passed and failed messages like we did before. PHPUnit provides us with those assertion methods that I was talking about that we can call, which handles all of that for us. The assertEquals method is probably the most commonly used assertion method because it's extremely flexible. This method accepts two arguments. The first is the expected value, and the second is the actual value that you're wanting to test. This value can be just about anything. You can pass in strings, numbers, objects, and so on. Now, when the assertEquals method is ran, it will report a failing test with a generic error message. Should the expected value be different from the actual value. And you can also pass in an optional third argument for a custom message if you'd like. So let's try using this to test if our ReturnsTrue function returns true. Let's pass in the Boolean true as our expected value. And then call our returnsTrue function as the actual value. And that's it for our test. As you can see, it's very readable. It's not cluttered, and it is focused on testing only one feature. Now, lets run our test. Thankfully, with PHPUnit, we can run our tests easily from within the Terminal or command line. And it gives us some pretty output of the test results. So I'm just going to switch into my Terminal, and make sure that you are inside the phptestingbasics folder. And let's run the following command, php vendor/bin/phpunit Tests, with a capital T. And with that command, it'll run all of the PHPUnit test files within the test folder that we created. As you can see, we're getting a PHP error this time. As expected, it's saying we have a call to an undefined function of returnsTrue. So our test failed. Let's get it passing by creating the App.php file. Let's go back into Sublime. And in the root of our phptestingbasics folder, I'll create a new file named App.php. Now, I'll write the smallest amount of code possible just to get our test to pass. So I'm going to open up my PHP tag. I'll create a new function named returnsTrue, and I'll leave it blank for the moment. Let's save it. And let's re-run our tests. I'll switch back into my Terminal. Let's re-run that command, php vendor/bin/phpunit Tests. And now this time, instead of a PHP error, we have a failing test. You can see we had one test ran. We have one assertion with one failure. At the top, you can see here that it says, There was 1 failure. AppTest testReturnsTrue. It's saying that, it failed asserting that null matches the expected true value. So to fix it, we just need to have our function return true. Let's go back into Sublime. And inside of our returnsTrue function, let's just return true here. Save our file. Let's switch back into the Terminal. And let's re-run our tests one more time. And perfect. This time, our test is passing. You can see, it says OK here, 1 test, 1 assertion. Now, the assertEquals method also has an inverse version called assertNotEquals, and it works in the same way, just in reverse. And feel free to experiment and add more tests for other functionality. So hopefully, you've found this helpful in learning how to use PHPUnit to write unit tests. There's so much more that you can do with PHPUnit. You can create set up and tear down methods which will run code that you'd like at the beginning and the end of your unit tests. This is useful for when you need to use a piece of code multiple times across various tests. Reducing code duplication, as well as many other methods to perform different kinds of assertions. So be sure to check out the documentation for more information. Next up, we'll learn how to write functional tests using the Codeception framework. I'll see you there.

Back to the top