Welcome to the next episode in our GitHub for Beginners series, where we’re diving into the world of GitHub Copilot. We’re now on our seventh episode, and we’ve covered quite a lot of ground. You can check out all our previous episodes on our blog or as videos.
Today we’re going to dive into the world of testing, a much needed but historically tedious part of the development process. This is especially true as our codebase becomes larger and more complex. Fortunately, we can use GitHub Copilot to help automate some of this process.
After all, one of the most basic questions we have when writing code is: “Does it work?”
Testing 101
Before we jump into how to use GitHub Copilot to write some tests, we should talk about testing, why it’s important, and different ways to test your code. Be aware that test testing is a very deep topic, and we’ll only be touching the surface here. Covering the nuances of testing would be an entire course in and of itself.
So why is testing important? In short, it’s how you make sure that your code does what you expect.
Testing can take many different forms, such as:
Acceptance tests: Tests that ensure your app meets a set of defined functionality.
Integration tests: Tests that verify your app can talk across various systems such as databases and APIs.
Unit tests: Tests focused on breaking the code into small, isolated pieces called units. These make sure the individual units do exactly what you’d expect them to do.
Writing unit tests
As we just covered, unit tests work by breaking down your code into smaller chunks that are easier to test. Making sure each individual piece is doing what it’s supposed to do increases confidence that the entire app will work when you put all the pieces together.
One of the great things about unit tests is that you can automate the process. Once you’ve created a large battery of tests, you can literally run thousands of tests with a single command. This gives you a good indicator regarding the health of your application. By regularly running these tests, you’ll also discover if any changes to your code broke something you might not have been expecting.
So how do you use GitHub Copilot to create some unit tests?
Open up your code and highlight a section that you want to test. For example, you might highlight a specific function.
Open up Copilot Chat. You might notice that Copilot suggests using the /tests slash command to write tests.
Send Copilot the following prompt:
/tests add unit tests for my code
If Copilot asks if you want to configure a test framework, select Dismiss.
Review the plan and code suggestions to make sure you understand what changes Copilot is going to make.
Click the Add to new file button at the top of the code suggestion to create the tests.
Save the new file.
Run the tests by running the following command in your terminal:
python -m pytest
Congratulations! You just added some unit tests to your code! If you’d like to see a demo of this in action, make sure to watch the video!
Test-driven development
Now that you’ve seen how to write some unit tests, let’s talk a little bit about test-driven development (TDD). What exactly is TDD? It’s a process where you use the tests to drive how you develop your code. When using TDD, you write your tests first, and then create the implementation afterward.
The process takes a little bit of adjusting how you think about development, but it does come with several advantages. It gives you the opportunity to see how your code will behave and ensure the tests you’re writing are testing what you expect them to test.
A concept that can be helpful for wrapping your brain around this is called “red, green, refactor.” In this process, you create the tests first, and they fail. They might not even build! This is the red stage.
Then you write just enough code to get your tests to pass. For example, if you’re writing a test that makes sure an error is thrown if a number is less than 0, you write just enough code to throw that error on that condition. When you return to the test, it now passes. You’ve actively made a change to the codebase to implement the desired functionality. This is the green stage.
Finally, you implement any refactoring to make the code look good. Now that it works, you can focus on making it pretty. The entire time you are working on this, you keep running the unit tests to make sure your changes don’t break anything. As you probably guessed, this is the refactor stage.
GitHub Copilot can help you with TDD. It’s one of the hidden little tricks that Copilot is able to do—you can tell it code will exist and generate tests based on that information. For example, if you were working on an email validation app, you could send the following prompt to Copilot Chat:
I'm going to be adding a new validator function for usernames. Usernames must be between 3 and 16 characters, start with a letter or an underscore, not use multiple underscores to start, and after the first character chan have letters, numbers, and underscores. Just create the new test functions.
This prompt provides the criteria that you’re expecting and gives it to Copilot. Copilot will then use this prompt to generate unit tests to test that functionality. If you ran these tests, they would fail, because you’ve only created the tests. Red stage.
Now, to move on to the green stage, you could send Copilot the following prompt:
Create the implementation
Copilot will now generate the code to make sure these tests pass. Now when you add this code to your validators and rerun the tests, they pass. Green stage.
Thanks to Copilot’s help, we’ve gone through some TDD and have code that works.
Best practices
Remember that unit tests are code. In order to make them more palatable to others, you should follow follow several of the same coding standards you’d use for production code:
Add documentation to your tests
Keep your tests organized
Create utilities to write your tests faster
Update your tests as you make changes to your code
We don’t have time to cover every aspect of TDD or unit testing, but there are plenty of resources available. Here are some to get you started:
Testing is an essential part of development. Having tools like GitHub Copilot that make tests less tedious to write improves your code and gives you more time to focus on the parts of coding you enjoy.
Don’t forget that you can use GitHub Copilot for free! If you have any questions, pop them in the GitHub Community thread, and we’ll be sure to respond. Join us for the next part in this series, which will be our final episode of the season.
Happy coding!
Need some help testing your code and keeping it all running smoothly? Give GitHub Copilot a try!
Kedasha is a Developer Advocate at GitHub where she enjoys sharing the lessons she's learned with the wider developer community. She finds joy in helping others learn about the tech industry and loves sharing her experience as a software developer. Find her online @itsthatladydev.