From the course: Software Testing Assistance with GitHub Copilot AI

Automated testing explained - Github Copilot Tutorial

From the course: Software Testing Assistance with GitHub Copilot AI

Automated testing explained

- [Instructor] Automated testing is the process of creating statements of fact about your code. This means that if you make a change that violates those facts, you'll know. I want to illustrate this idea very simply. I'm going to revisit the simple add function. This function expects two integers and returns an integer. To demonstrate this function, I'll just go to the end of this file and call add directly and I'll pass it five and two. And Copilot really wants me to add a comment that shows that it's seven. When I run this through the php interpreter, the output is seven. This is not revolutionary code, I realize. I'm going to compose and install a package called phpunit. More on that in future videos, but the important thing to know is that phpunit is a test framework, meaning it provides the functionality you need to test your code. Now that testing framework is installed, I'm going to write a test. Remember, we are testing a simple function, so our test is going to be simple as well. First, I need a test directory, and then I'll create a new PHP class called Add_Test. For this to work, it needs to extend TestCase. And note, Copilot wants to autocomplete. The issue is if I accept that I'm going to miss the import when I select the appropriate TestCase class. Now I need to add the setUp method. And sometimes Copilot will realize that it needs to call the parent and sometimes it won't. And to require this file, I need to include everything above it. Now I'll write the first test. Well, in this case, Copilot wants to write the first test, but I want to name this something a little different, test_add_function. And I was going to write this assertion, an assertion is how a test is identified as passing or failing, but Copilot beat me to the punch. In this case, I will assert that seven equals five plus two. Now I'll run the test using phpunit tests. This tells me I have one passing test with one assertion. Cool. To review, I've created somewhat normal code with this add function, I included a testing framework, phpunit, and I've written one test and one assertion. While simple, this is very instructive. By itself this test has no value. It, like all PHP code, has to be executed to actually do anything. Writing tests that aren't executed often doesn't provide a lot of value. Also, I want to point out that this test is not complete. The single assertion assumes perfect intentions. What happens when you add an emoji and a string, or a float and an object? Because I didn't type into the args and the response those are valid TestCases. Very simply, automated tests are code that document how the code in your project should behave. Automated tests can help prevent regressions, but only if there are tests that cover that particular regression.

Contents