The GitHub Blog 3小时前
GitHub for Beginners: Test-driven development (TDD) with GitHub Copilot
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文是GitHub初学者系列的第七集,介绍了如何使用GitHub Copilot辅助进行测试驱动开发(TDD)。文章首先强调了测试在软件开发中的重要性,然后详细介绍了单元测试的编写方法,并演示了如何利用GitHub Copilot来自动生成单元测试。此外,文章还深入探讨了TDD的概念,并提供了使用Copilot进行TDD的实践方法,帮助开发者更高效地编写高质量代码。

✅ 单元测试是确保代码按预期运行的关键,它将代码分解为更小的单元进行测试,提高整个应用程序的可靠性。

🤖 GitHub Copilot 可以通过简单的指令来自动生成单元测试,例如使用`/tests add unit tests for my code`命令,极大地简化了测试流程。

💡 测试驱动开发(TDD)是一种先写测试后写代码的开发方法,它有助于开发者更好地理解代码的行为,并确保测试覆盖了预期的功能。

🚦 TDD的核心流程包括“红、绿、重构”三个阶段:首先编写测试,测试失败(红),然后编写足够的代码使测试通过(绿),最后进行代码重构,保证代码质量。

✨ GitHub Copilot在TDD中扮演重要角色,它能够根据开发者提供的需求生成测试代码,并辅助生成实现代码,加速开发过程。

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

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.

Testing can take many different forms, such as:

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?

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

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:

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:

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.

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

GitHub Copilot 测试驱动开发 TDD 单元测试
相关文章