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 4, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      June 4, 2025

      How To Prevent WordPress SQL Injection Attacks

      June 4, 2025

      Smashing Animations Part 4: Optimising SVGs

      June 4, 2025

      I test AI tools for a living. Here are 3 image generators I actually use and how

      June 4, 2025

      The world’s smallest 65W USB-C charger is my latest travel essential

      June 4, 2025

      This Spotlight alternative for Mac is my secret weapon for AI-powered search

      June 4, 2025

      Tech prophet Mary Meeker just dropped a massive report on AI trends – here’s your TL;DR

      June 4, 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

      Beyond AEM: How Adobe Sensei Powers the Full Enterprise Experience

      June 4, 2025
      Recent

      Beyond AEM: How Adobe Sensei Powers the Full Enterprise Experience

      June 4, 2025

      Simplify Negative Relation Queries with Laravel’s whereDoesntHaveRelation Methods

      June 4, 2025

      Cast Model Properties to a Uri Instance in 12.17

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

      My Favorite Obsidian Plugins and Their Hidden Settings

      June 4, 2025
      Recent

      My Favorite Obsidian Plugins and Their Hidden Settings

      June 4, 2025

      Rilasciata /e/OS 3.0: Nuova Vita per Android Senza Google, Più Privacy e Controllo per l’Utente

      June 4, 2025

      Rilasciata Oracle Linux 9.6: Scopri le Novità e i Miglioramenti nella Sicurezza e nelle Prestazioni

      June 4, 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

    I test AI tools for a living. Here are 3 image generators I actually use and how

    June 4, 2025
    News & Updates

    The world’s smallest 65W USB-C charger is my latest travel essential

    June 4, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Mobile App Development Policy

    News & Updates

    Rector v2.0

    Development

    The Game Pass hit STALKER 2 just got a huge Xbox award, and I can’t think of a game more deserving

    News & Updates

    Meet Verba 1.0: Run State-of-the-Art RAG Locally with Ollama Integration and Open Source Models

    Development

    Highlights

    Development

    Playwright Reports: The Definitive Guide

    March 26, 2025

    Reporting is a crucial aspect that helps QA engineers and developers analyze test execution, identify failures, and maintain software quality. If you are using Playwright Reports as your Automation Testing tool, your test automation reports will play a significant role in providing detailed insights into test performance, making debugging more efficient and test management seamless.
    The post Playwright Reports: The Definitive Guide appeared first on Codoid.

    Plop.js – A micro-generator framework: Introduction and Installation (Part-1)

    March 20, 2025

    ChatGPT for E-commerce: Crafting Product Descriptions that Rank and Convert

    August 31, 2024

    DeepKeep Launches GenAI Risk Assessment Module

    June 27, 2024
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

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