Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      June 5, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      June 5, 2025

      How To Prevent WordPress SQL Injection Attacks

      June 5, 2025

      In MCP era API discoverability is now more important than ever

      June 5, 2025

      Google’s DeepMind CEO lists 2 AGI existential risks to society keeping him up at night — but claims “today’s AI systems” don’t warrant a pause on development

      June 5, 2025

      Anthropic researchers say next-generation AI models will reduce humans to “meat robots” in a spectrum of crazy futures

      June 5, 2025

      Xbox just quietly added two of the best RPGs of all time to Game Pass

      June 5, 2025

      7 reasons The Division 2 is a game you should be playing in 2025

      June 5, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Mastering TypeScript: How Complex Should Your Types Be?

      June 5, 2025
      Recent

      Mastering TypeScript: How Complex Should Your Types Be?

      June 5, 2025

      IDMC – CDI Best Practices

      June 5, 2025

      PWC-IDMC Migration Gaps

      June 5, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Google’s DeepMind CEO lists 2 AGI existential risks to society keeping him up at night — but claims “today’s AI systems” don’t warrant a pause on development

      June 5, 2025
      Recent

      Google’s DeepMind CEO lists 2 AGI existential risks to society keeping him up at night — but claims “today’s AI systems” don’t warrant a pause on development

      June 5, 2025

      Anthropic researchers say next-generation AI models will reduce humans to “meat robots” in a spectrum of crazy futures

      June 5, 2025

      Xbox just quietly added two of the best RPGs of all time to Game Pass

      June 5, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»News & Updates»GitHub for Beginners: Test-driven development (TDD) with GitHub Copilot

    GitHub for Beginners: Test-driven development (TDD) with GitHub Copilot

    May 26, 2025

    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?”

    For the demos in this series, we’re using GitHub Copilot in Visual Studio Code.

    Copilot is available in other IDEs, but the available functionality may vary depending on your environment.

    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.

    A slide explaining 'Why are tests important? Ensures code is working as expected.'

    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?

    1. Open up your code and highlight a section that you want to test. For example, you might highlight a specific function.
    2. Open up Copilot Chat. You might notice that Copilot suggests using the /tests slash command to write tests.
    3. Send Copilot the following prompt:
    /tests add unit tests for my code
    1. If Copilot asks if you want to configure a test framework, select Dismiss.
    1. Review the plan and code suggestions to make sure you understand what changes Copilot is going to make.
    2. Click the Add to new file button at the top of the code suggestion to create the tests.
    3. Save the new file.
    4. 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.

    A slide explaining Red, Green, Refactor steps:

1. Write tests first
2. Tests fail because there's no code (red!)
3. Write just enough code to allow tests to pass
4. Rerun the test to see it pass (green!)
5. Refactor and clean up code

    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:

    • Accelerate TDD with AI
    • How to generate unit tests with GitHub Copilot
    • Generating unit tests with GitHub Copilot
    • Writing tests with GitHub Copilot

    Your next steps

    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!

    The post GitHub for Beginners: Test-driven development (TDD) with GitHub Copilot appeared first on The GitHub Blog.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleDistribution Release: Armbian 25.05.1
    Next Article CVE-2025-5181 – Summer Pearl Group Vacation Rental Management Platform Cross Site Scripting Vulnerability

    Related Posts

    News & Updates

    Google’s DeepMind CEO lists 2 AGI existential risks to society keeping him up at night — but claims “today’s AI systems” don’t warrant a pause on development

    June 5, 2025
    News & Updates

    Anthropic researchers say next-generation AI models will reduce humans to “meat robots” in a spectrum of crazy futures

    June 5, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    This 5-star laptop is a “true battery champion” with 12 hours of screen time — it’s also only $799.99 right now.

    News & Updates

    A Step by Step Guide to Solve 1D Burgers’ Equation with Physics-Informed Neural Networks (PINNs): A PyTorch Approach Using Automatic Differentiation and Collocation Methods

    Machine Learning

    Serpent OS diventa AerynOS: un nuovo nome per una distribuzione GNU/Linux in evoluzione

    Linux

    crossdirstat is a file and directory statistics tool

    Linux

    Highlights

    Development

    JPMorgan Chase Researchers Propose JPEC: A Novel Graph Neural Network that Outperforms Expert’s Predictions on Tasks of Competitor Retrieval

    November 13, 2024

    Knowledge graphs are finding their way into financial practices, especially as powerful tools for competitor…

    Think GeoGuessr is fun? Try using ChatGPT to guess locations in your photos

    April 18, 2025

    CVE-2025-20213 – Cisco Catalyst SD-WAN Manager Local File System Overwrite Vulnerability

    May 7, 2025

    CodeSOD: False True is True False

    July 18, 2024
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.