Use Google Duet AI to Auto-Generate Code and Write Tests in VS Code
Summary
This article demonstrates how to auto-generate or auto-complete code (completely irrelevant to Google Cloud) using Google’s Duet AI in VS Code by installing the Google Cloud Code
extension. It might be potentially helpful to write unit tests for users.
Duet AI is an AI-powered collaborator, by Google, helping dev teams build, deploy, and operate applications faster and more efficiently.
Target Audience
- VS Code users who are trying out different AI coding assistant tools to improve productivity and code quality.
- Existing Google Cloud users who want to explore duet AI when it’s still free.
- New Google Cloud users who want to take advantage of the $300 free credit and try out Duet AI.
Outline
- How to install and set up
- How to auto-generate or auto-complete code
- How to generate unit tests
- How to install and set up
Look for Google Cloud Code
in the Visual Studio Code (VS Code) extensions marketplace and install it.
After installation, this is the welcome page for you to get started.
To set up, click on “Open Walkthrough”.
Next, authenticate your Google Cloud account. By clicking on the “Connect” button in “Connect with Google Cloud”, you will be directed to the sign-in page in your default browser.
After signing in, you will see “Cloud Code for VS Code is now authorized to access your account. You can go back to the VS Code editor now.
Before selecting a Google Cloud Project, make sure you have enabled Duet AI API in the console for this project. Select a project from the drop-down list.
Now, we are good to go.
Click on the selected project at the bottom of the VS Code editor. You will see the quick-start menu pop up below the search box.
2. How to auto-generate or auto-complete code
I created a Python file for this Duet AI experiment. To auto-generate code, we can write our instructions (text prompt) in docstring format. Namely, enclose the instructions with quotation marks.
I entered an instruction “write a function to extract year from today’s date
”. Then press “enter” to wait for Duet AI to generate code for us. When you see code hints appear, press “tab” to auto-complete. The code generated in the above gif was auto-completed by pressing only “enter” and “tab”.
The code generation will produce different results. If you are not satisfied with one attempt, try re-wording the text prompt. Even without re-wording, you might get a different code snippet. Just remove and paste the docstring over and over again.
In addition to code auto-completion, Duet AI also suggests type annotation. I am quite impressed. AI can even interpret the extracted year
as the int
type. Lazy programmers who don’t want to “waste” time writing type annotations should give it a try.
3. How to generate unit tests
Generating test functions is even simpler. For simple functions, just add test_
before the original function to be tested.
My sample function is called get_year()
:
import datetime
def get_year(date: datetime.datetime):
"""extract year from input date"""
year = date.year
return year
To get Duet AI to write a test function for me, I will start typing def test_get_year()
in the editor. Then code suggestions will appear automatically. Again, press “tab” to auto-complete. You can view the gif below.
The complete code snippets generated by Duet AI are pasted below:
import datetime
def get_year(date: datetime.datetime):
"""extract year from input date"""
year = date.year
return year
def test_get_year():
"""test get_year"""
assert get_year(datetime.datetime(2022, 1, 1)) == 2022
assert get_year(datetime.datetime(2022, 12, 31)) == 2022
assert get_year(datetime.datetime(2023, 1, 1)) == 2023
assert get_year(datetime.datetime(2023, 12, 31)) == 2023
This experiment shows that, at least for simple functions, Duet AI has done a great job auto-generating Python code and unit tests for me. I haven’t tested it on Google Cloud-related code yet. I assume developers who use mainly Google Cloud resources will benefit the most from Duet AI.