How to test nonexistent code with Copilot

GitHubAbout 2 min readAug 22, 2025Watch original
THE SUMMARYAI-generated

Key Concepts:

  • Username validation function
  • Copilot code generation
  • Unit testing
  • Red-Green testing cycle (fail then pass)

Username Validation Function Creation with Copilot

The video demonstrates using GitHub Copilot to create a username validation function from scratch, including generating both the function and its unit tests.

Defining Requirements

The user provides Copilot with specific criteria for the username validation function:

  • Length: Minimum 3 characters, maximum 16 characters.
  • Starting Character: Must start with a letter or an underscore.
  • Underscore Restriction: Cannot have multiple underscores at the beginning.
  • Subsequent Characters: After the initial letter or underscore, can include underscores, numbers, and additional letters.

Unit Test Generation

Based on the defined criteria, Copilot automatically generates unit tests designed to validate the function's behavior against these rules.

Initial Test Failure (Red State)

The user runs the generated unit tests. As expected, the tests fail because the username validation function does not yet exist. This represents the "red" state in the Red-Green testing cycle.

Function Code Generation

The user instructs Copilot to create the actual username validation function based on the previously provided requirements. Copilot generates the code for the function.

Code Insertion and Test Success (Green State)

The generated function code is inserted into the user's code base (specifically, a "validators" file). The user then re-runs the unit tests. This time, all tests pass, indicating that the generated function correctly implements the specified validation logic. This represents the "green" state in the Red-Green testing cycle.

Red-Green Testing Cycle Demonstration

The video showcases a complete Red-Green testing cycle:

  1. Red: Unit tests are created and fail because the function doesn't exist.
  2. Green: Copilot generates the function, and the unit tests now pass.

Conclusion

The video demonstrates Copilot's ability to generate both unit tests and functional code based on user-defined requirements, even for functions that do not initially exist. This streamlines the development process and allows for rapid prototyping and implementation of new features. The example highlights the efficiency of using Copilot to create a username validation function and the Red-Green testing cycle.

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.