From the course: Software Testing Assistance with GitHub Copilot AI

AI and automated testing - Github Copilot Tutorial

From the course: Software Testing Assistance with GitHub Copilot AI

AI and automated testing

- [Instructor] This is the heart of this course. You know that you or your team probably don't write enough tests and Copilot can help. To be clear, this is about using AI to support traditional automated testing. The first step to adding tests is finding the right things to test. Not all functions or classes are in a testable state. Identifying the right place to start with will help you continue to write tests once you get going. Asking Copilot to write tests for a function or method is a way to quickly see how complex the test will be. And this test looks pretty simple. It passes an array and compares it to the expected sorted array. A tip, usually the first test I write in every app is a test that asserts that true equals true. This is so I can test that the tests we harness is correctly configured. That'll help a lot. From there, I can be confident that as I add tests and assertions, there will be value in running them. So I'll say tests. Test, tests, run, and I will assert that true equals true. Now, in this directory, I already had a working test, but this acts as a debug that my test config is valid. And when I run this, I need a composer install before I run this. But when I run this, we have two tests and two assertions. Once I have some tests in place, I can start using tests to help me refactor more complex pieces into testable logic. A quick way to do this is to select a function and ask Copilot how to refactor it. You'll end up with a handful of tinier functions that can then be tested. Using Copilot Chat directly for refactoring will get you started on the right foot, but also the explained slash command and the simplify command also bring a lot of value to landing the code in a testable state. Now that you're committed to writing tests, you don't have to always write them in response to the code you've written. You can start by documenting business logic in a test. Let me show you what I mean. First, I'll write a subtraction function test, test, subtract, and of course, Copilot knows what the intent is. My IDE highlights that there's no subtract function that exists in this code base yet. So, I'll go over to example where the add function exists and Copilot infers from the test that I wrote, what the appropriate function is to make the tests pass. From that point, you can rinse and repeat, add a little more context in business logic, generate code, update tests. By using AI to help write tests, you will have better test coverage.

Contents