Can GitHub Copilot generate test cases and unit tests for the code it suggests?

Content verified by Anycode AI
August 26, 2024
Explore if GitHub Copilot can generate test cases and unit tests for its code suggestions, and learn how it enhances coding efficiency and reliability.

Understanding GitHub Copilot

GitHub Copilot is like your coding buddy, powered by AI from OpenAI and GitHub. It uses machine learning to suggest code snippets, functions, and even entire modules. One cool thing it does is help you generate test cases and unit tests for the code it suggests.

 

Step 1: Writing Initial Code

First things first, write the core logic of the function or class you need tests for. Let's say you're writing a function to calculate the factorial of a number. Just get that function down first.

def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)

 

Step 2: Prompting for Tests

Once you've got your core function, you can prompt Copilot to generate test cases. Usually, starting a new comment with a keyword like "test" will signal to Copilot that you're looking to create a test function.

# test for factorial function

 

Step 3: Letting Copilot Generate Test Cases

Based on that initial comment, GitHub Copilot will auto-suggest a complete test case. For example, it might suggest using Python's unittest framework to create the test.

import unittest

class TestFactorial(unittest.TestCase):
    def test_factorial(self):
        self.assertEqual(factorial(0), 1)
        self.assertEqual(factorial(1), 1)
        self.assertEqual(factorial(4), 24)
        self.assertEqual(factorial(5), 120)

if __name__ == '__main__':
    unittest.main()

 

Step 4: Reviewing and Editing Suggested Tests

Take a good look at the auto-generated test cases. Make sure they cover different edge cases and are logically sound. Edit or add any additional test cases as needed.

class TestFactorial(unittest.TestCase):
    def test_factorial(self):
        # Existing tests
        self.assertEqual(factorial(0), 1)
        self.assertEqual(factorial(1), 1)
        self.assertEqual(factorial(4), 24)
        self.assertEqual(factorial(5), 120)
        
        # Additional edge cases
        self.assertEqual(factorial(10), 3628800)
        self.assertEqual(factorial(3), 6)

 

Step 5: Running the Tests

Run the test cases to make sure they work correctly and no errors pop up. This is usually done by running the Python file containing the tests.

python test_factorial.py

 

Step 6: Iteration and Improvement

Keep iterating on the test cases. As your core code evolves, so should the test cases. GitHub Copilot can help by suggesting relevant test cases as you add new functionalities or update existing ones.

# Updating the factorial function to handle negative inputs
def factorial(n):
    if n < 0:
        raise ValueError("Negative input not allowed")
    elif n == 0:
        return 1
    else:
        return n * factorial(n-1)
class TestFactorial(unittest.TestCase):
    def test_factorial(self):
        # Existing and new tests
        self.assertEqual(factorial(0), 1)
        self.assertEqual(factorial(1), 1)
        self.assertEqual(factorial(4), 24)
        self.assertEqual(factorial(5), 120)
        self.assertEqual(factorial(10), 3628800)
        self.assertEqual(factorial(3), 6)
        
        # New test for exception
        with self.assertRaises(ValueError):
            factorial(-1)

 

Step 7: Continuous Testing

Integrate the tests into a continuous integration (CI) pipeline to make sure every change to the codebase is automatically tested. This ensures that all changes are validated through the generated test cases before being merged into the main branch.

 

Using GitHub Copilot strategically can really streamline the process of creating solid test cases, making your codebase more reliable.

Improve your CAST Scores by 20% with Anycode Security AI

Have any questions?
Alex (a person who's writing this 😄) and Anubis are happy to connect for a 10-minute Zoom call to demonstrate Anycode Security in action. (We're also developing an IDE Extension that works with GitHub Co-Pilot, and extremely excited to show you the Beta)
Get Beta Access
Anubis Watal
CTO at Anycode
Alex Hudym
CEO at Anycode