How to create unit tests easily using GitHub Copilot Chat

GitHubAbout 2 min readJun 8, 2025Watch original
THE SUMMARYAI-generated

Key Concepts:

  • Unit Tests: Automated tests that verify individual components or functions of a software application.
  • Copilot: An AI-powered code completion and generation tool.
  • Validators.py: A Python file containing validation functions, specifically validate_email_format.
  • Test Framework: A structure and set of tools for writing and running tests.
  • Slash Command (/tests): A Copilot command to generate unit tests.
  • Test Validators.py: A Python file containing unit tests for the validator functions.

Unit Test Generation with Copilot

The video demonstrates how to use GitHub Copilot to generate unit tests for a Python function. The process begins by focusing Copilot on the validate_email_format function within the validators.py file. This is achieved by highlighting the function's code.

Copilot Chat and the /tests Command

The user opens Copilot Chat, which intelligently suggests using the /tests slash command. This command is specifically designed to generate unit tests.

Initial Configuration and Test Generation

Upon the first use of the /tests command, Copilot prompts the user to configure a testing framework for the project. The user dismisses this prompt, opting to focus solely on unit test generation. Copilot then provides an overview of its intended actions before generating the test code.

Test Structure and Validation

The generated test code includes tests for both successful and failing scenarios of the validate_email_format function. Specifically, it tests the function with valid email addresses (success case) and invalid email addresses (failure case).

Test File Creation and Execution

The generated test code is then added to a new file named test_validators.py. This file now contains a collection of unit tests for the validate_email_format function. When these tests are executed, the system reports that all tests have passed successfully.

Copilot's Proficiency with Unit Tests

The video emphasizes that Copilot excels at generating unit tests because unit tests are essentially code that asserts specific conditions. Copilot can effectively generate this type of code based on its understanding of the function being tested and common testing patterns.

Conclusion

The video showcases a practical application of Copilot for automated unit test generation. By using the /tests command and focusing Copilot on a specific function, developers can quickly create a comprehensive suite of tests to ensure the correctness and reliability of their code. The example highlights Copilot's ability to handle both success and failure scenarios, making it a valuable tool for improving software quality.

AI summaries can miss context or contain errors. Check important details against the original video.

Go a little deeper.

Have a question about this video? Load its transcript to open the video chat.