Effortless Code Generation and Automated Testing with Google Duet AI in VS Code.


4 min read


This guide illustrates the process of leveraging Google's Duet AI in VS Code to automatically generate or complete code, with no direct dependency on Google Cloud. By installing the Google Cloud Code extension, users can enhance their productivity and potentially streamline the creation of unit tests. Duet AI, a Google-powered collaborative AI, empowers development teams to accelerate the building, deployment, and operation of applications with increased efficiency.

Target Audience:

  1. VS Code Enthusiasts:

    • Those actively experimenting with various AI coding assistants to enhance productivity and elevate code quality using VS Code.
  2. Current Google Cloud Subscribers:

    • Users already engaged with Google Cloud services, are interested in exploring Duet AI during its complimentary period.
  3. New Google Cloud Adopters:

    • Individuals new to Google Cloud, are eager to leverage the $300 free credit opportunity and explore the capabilities of Duet AI.

Install and set up

Look for Google Cloud Code in the Visual Studio Code (VS Code) extensions marketplace and install it.

Upon installation, this serves as your welcoming hub, offering a starting point to initiate your journey.

To begin the setup process, simply click on "Open Walkthrough."

Proceed by authenticating your Google Cloud account. Click the "Connect" button within the "Connect with Google Cloud" section, and you will be redirected to the sign-in page in your default browser.

Upon successful sign-in, you will receive a confirmation stating, "Cloud Code for VS Code is now authorized to access your account." You can then return to the VS Code editor to continue.

Before choosing a Google Cloud Project, ensure that you have activated the Duet AI API in the console for the respective project. Once confirmed, proceed to select your desired project from the drop-down list.

Now you are good to explore

Click on the chosen project located at the bottom of the VS Code editor. Subsequently, observe the quick-start menu that will appear below the search box.

Auto-generating or Auto-completing Code

For this Duet AI experiment, I've crafted a Python file. To trigger code auto-generation, encapsulate your instructions (text prompt) in docstring format, enclosing them within quotation marks.

After inputting the instruction "write a function to extract the year from today's date" and pressing "enter," patiently await Duet AI to generate code suggestions. Once code hints appear, press "tab" to initiate auto-completion. The showcased code in the provided gif was entirely auto-completed with just the use of "enter" and "tab."

Keep in mind that code generation results may vary. If dissatisfied with a particular attempt, consider rephrasing the text prompt. It's worth noting that even without rephrasing, subsequent attempts may yield different code snippets. Feel free to remove and paste the docstring repeatedly.

Beyond code auto-completion, Duet AI also offers type annotation suggestions, showcasing its impressive capability to interpret the extracted year as the 'int' type. For those inclined to save time on writing type annotations, this feature is especially appealing to lazy programmers. Give it a try and experience the convenience.

Generating Unit Tests

Creating test functions with Duet AI is straightforward. For basic functions, simply prefix "test_" before the original function you intend to test.

Here's an example with a sample function named get_year():

import datetime

def get_year(date: datetime.datetime):
    """extract year from input date"""
    year = date.year
    return year

To generate a corresponding test function, follow this simple pattern:

def test_get_year():
    # Your test instructions here

Duet AI streamlines the process, making unit test generation efficient and hassle-free.

To prompt Duet AI to generate a test function, initiate the process by typing def test_get_year() in the editor. As you begin typing, code suggestions will automatically appear. Once they appear, simply press "tab" to swiftly auto-complete the test function. Refer to the gif below for a visual demonstration.

The complete code is 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


In conclusion, the exploration of Duet AI within the context of Python code generation and unit test creation showcases its potential as a valuable tool for developers. The seamless integration with VS Code, coupled with its ability to interpret natural language instructions, simplifies the coding process, especially for simple functions. The auto-completion feature, triggered by pressing "enter" and "tab," streamlines the workflow and enhances productivity.

While the experiment focused on non-Google Cloud-related code, the assumption is that developers heavily invested in Google Cloud resources stand to benefit the most from Duet AI. The tool's prowess in generating code snippets and suggesting type annotations could significantly aid developers in the Google Cloud ecosystem, offering a potential boost to efficiency and code quality.

Overall, Duet AI's capabilities demonstrated in this experiment, combined with its user-friendly integration into VS Code, suggest its potential as a valuable assistant for developers looking to expedite coding tasks and improve code quality, especially within the Google Cloud environment.

I hope this helps, you!!

More such articles:





Did you find this article valuable?

Support techwasti by becoming a sponsor. Any amount is appreciated!