7 minute read

A women coding away on her laptop
Generated by Gen AI from Microsoft Copilot using DALL.E

GitHub Copilot is a fantastic addition to the professional software engineers toolbelt and can help with lot of common day to day tasks. I’ve been leveraging its capabilities heavily over the past year and I felt it makes a ton of sense to unpack this in a bit more detail.

In this blog we’ll use GitHub Copilot to illustrate these interaction modes

  1. Autocomplete suggestions as you type code in your IDE (Integrated development environment)
  2. Inline chat for a specific piece of code or problem
  3. Chat with commands and references

If you are using JetBrains IDE’s like IntelliJ or Pycharm or Visual Studio code

  1. Please make sure to install the Github Copilot plugin or extension in your IDE of choice
  2. Then, follow the steps to authorize them to access your GitHub account.

GitHub recently made Copilot free up to a limited set of autocomplete or chat interactions 🤩 and you can use this to play around with its capabilities 🙌.

Autocomplete ✅

I truly love this feature. ❤️

Why?

So much of writing automated tests involves writing similar code leveraging existing classes, utilities, or helper functions and assertions.

GitHub Copilot can help by helping you speed up the test authoring aspect. It autocompletes code for you as you type by displaying the suggested code in grey.

If the suggestion looks acceptable, you can press the TAB key, and that code is inserted at a given caret location. You can then edit the code to suit your specific requirements. You could also partially accept a suggestion as well.

If not, you can continue writing code and it would try to figure out what you are trying to write and suggest.

Could you make it more effective?

Yes, there are a few distinct things you can try to help Github Copilot make better suggestions. Let’s go over them next

Open relevant files

GitHub Copilot gives preference to what files you have open at the time that you are trying to write a piece of code. It also has context of your workspace but providing targeted reference to correct files would increase the chances of getting a more relevant autocompletion.

This makes logical sense since you would probably not have unnecessary files open when working on a codebase anyway.

Here are a few common use cases

  1. If you are trying to write a new test that follows a given pattern that is already repeated in another file, having that file open would help Copilot suggest better autocompletes.
  2. If you are writing assertions on a given test method and want to reuse existing methods then having the assertions file open would allow Copilot to suggest relevant suggestions better

Write only the method signature

You can write the method’s signature with its name, parameters, and types, and the Github copilot will auto-complete that function in your IDE.

This works better in strongly typed languages like Java, but you can also use Python-type hints to provide sufficient context to Copilot

Let’s take an example

Say you want to write the below function that returns epoch seconds for the current UTC time in Java

If you write below:

public long getEpochSecondsForCurrentTimeInUTC() {}

You can see Copilot takes the function signature and suggests an implementation

public long getEpochSecondsForCurrentTimeInUTC() {
   return System.currentTimeMillis() / 1000;

Write an inline comment

Another way of getting autocompletes is to write a short comment stating what code you want to generate and Copilot takes that as input and generates code along those lines

For example below, I’ve prompted Copilot to generate a function that gives me a delta date by describing what i’m looking for

// Write a java function that takes a date and time format and no of days and can return the date after adding the no of days

// write a java function that takes a date and time format and no of days and can return the date after adding the no of days
public String addDaysToCurrentDate(String dateFormat, int days) {
   SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
   Calendar c = Calendar.getInstance();
   c.add(Calendar.DATE, days);
   return sdf.format(c.getTime());
}

Generated test methods

Generate tests 🧪

What if you want to test your utilities?

Say you want to test the previously written test utility method, you can put the caret at the relevant position and then Right Click → select GitHub Copilot → Generate tests

Generate tests menu

This will initiate a Github Copilot chat session and generate tests for this simple function.

You can then copy and paste these methods and put them in a test file to have some instant coverage. Also notice, it uses the file DateTimeHelpers file as a reference

The same technique could be used to generate unit tests for any simple function. If the function you are testing is a bit complex, then you may have to do some back-and-forth prompting to arrive at a good set of tests.

Another tip is to have some example tests whose style you want to mimic.

Having that file open may result in better results, you can also prompt stating please generate tests following the coding style followed in <X.java> and it would consider them giving you more accurate tests following your projects style guide.

You can also see the generated tests here

Generated test methods in chat

Inline chat 💬

Inline chat is a way to prompt Copilot to do something in the context of the code you are working with.

Continuing our previous example, say I autocompleted getEpochSecondsForCurrentTimeInUTC method and I want to write a comment explaining the method.

Clean code Tip: Your code comments should always explain the why or what and not the how. Your code should be clear and readable so that the reader can understand the how by just reading the code

You can press Opt + enter (on mac) and then select Open GitHub Copilot Inline chat

Open inline chat

in this case, I just prompt it to generate a comment

Inline chat prompt

You can now see the suggested comment and even a side-by-side diff if required

Inline chat prompt

Side by side view can help you contextualize where the change would be inserted

Suggested comment

Now this is just one example, you can use this get Copilot to do specific generation in the context of your current code. For instance,

  1. You could use this way to prompt and generate some specific code
  2. or tests for a given condition.
  3. Fix a problem with the code

Copilot chat 🗨️

Beyond autocomplete and inline chat interaction modes that we saw above.

You can also start a regular chat session to ask Github Copilot to do something for you

This is similar to many other chat interfaces and you can leverage the same prompting techniques that we discussed in the 1st blog of this series.

Gen AI and Test Design: Supercharge your Prompting skills to get better results.

Add specific references

A couple of interesting new features here are:

You can choose which specific file you want the chat to take as a reference by clicking on the plus

Add reference files

Follow-up prompt suggestion

And that Copilot also provides you with a suggested next message

Follow-up suggestions

Use the forward slash to see commands

Unlike other LLMs, Copilot is tailored for coding and as such provides you with convenient commands to use. You can press forward slash and then review available actions.

Copilot commands

This is a rapid way of prompting Copilot to perform these common operations.

You can select a piece of code and then quickly use /doc /simplify /tests /fix to prompt Copilot to take action on them

For E.g. below I just asked it to generate a doc for the newly added method and Copilot suggested a pretty nice comment for me.

Generated documentation

Recap

Let’s recap what we learned in this blog

  1. Github Copilot offers coding assistance with autocomplete, inline chat and chat interfaces
  2. Keeping relevant files open or explicitly proving context helps get better results
  3. You can provide method signature or inline comment to prompt Copilot to take action
  4. Inline chat helps you get contextual changes at caret position
  5. You can leverage chat commands to perform common actions like fix, explain, generate tests, simplify, generate docs etc. I’ll leave it to you to try these on your own

Prefer videos?

In the next edition, we will explore Github Copilot multi file edits

Subscribe to the newsletter and YouTube channel (@automationhacks) for more ⚡insights into software testing and automation, or connect on topmate for a personalized 1:1 session.

Disclosure: I’ll earn a small commission if you decide to purchase some of the educative.io text courses linked in the blog for your learning and growth. Their system design courses and grokking coding interview courses are arguably quite helpful for interview prep.

Comments