GitHub Copilot: Empowering Developers to Embrace Unit Testing

Share on your favorite social sites

With GitHub Copilot, we’ve witnessed its capabilities in generating code for constructing databases and implementing Entity Frameworks. However, GitHub Copilot can also generate unit test cases for both existing and new code. Let’s explore some examples and steps on how to generate unit test cases for various technologies using GitHub Copilot.

Unit testing isn’t within our scope?

Consider this: Approach a developer from your team or someone with less than 5 years of experience and inquire, ‘Do you regularly create unit test cases?’ Chances are, you’ll often hear responses like ‘What exactly is unit testing?’ or ‘Unit testing isn’t within our scope’.

As a developer, one of the key skills that sets you apart is the ability to conduct thorough unit testing on your code. In a future discussion, I’ll delve into the first skill that distinguishes you as a developer.

In an ideal scenario, the term ‘Unit’ in unit testing represents a single line of code (LoC) within your application. Unit testing involves constructing test cases, whether manually or automated, to verify the functionality of each unit (LoC).

In every technology, there exist frameworks designed specifically for writing unit test cases, such as NUnit, XUnit, Jasmine, Karma, and Py Test. Stay tuned for upcoming blogs exploring these frameworks in greater detail.

Basic building block of Unit Testing

When writing, a typical process involves :

  • Identifying the scenarios to build unit tests
  • Generate Test data for various scenario
  • Mocking Services
  • Execute automated unit test cases

You can use GitHub Copilot for all the above.

Unit Testing Standards

First step will be to define the Unit test standards that you want GitHub Copilot to refer when generating unit tests. Below is the example i have defined. This refers to concepts and details mentioned in the Pragmatic Unit Testing references.

All unit test cases for a service should include a test case for each method in the service.
ENsure the test cases generated cover RIGHT - BICEP scenarios

RIGHT scenario is when the method is called with correct input and the expected output is returned.
BICEP Stands for boundary, inverse, cross, error, performance.
Boundary scenario is when the method is called with the minimum or maximum input value possible for the method 
Inverse is a way to mock to get the opposite of the expected output
Cross is a way to mock to get the output of the method when called with the output of another method
Error is a way to mock to get the output of the method when called with an error
Performance is a way to mock to get the output of the method when called with a large input
Sequence each test case name with a running sequence number and the method name. For example,TEST_001
Identifying Scenarios

We begin by generating the Setup and Breakdown methods, these methods are executed before and after every test case respectively.

Generate a new unit test class with Setup and breakdown methods for RoleCoreServices.cs

You can use GitHub Copilot prompts to not only generate test cases for generate multiple scenarios based on the conditions in UnitTestStandards.txt.

Can you generate unit test cases for ListRoles RoleCoreServices.cs , following #UnitTestsStandards.txt

Add individual test methods with specific scenarios , below prompts adds a new test cases to validate the code response with Roles list is null (not empty)

Can you generate a new test method to test a null list of roles
Generating Data driven test cases

One of the key challenges when building unit tests is how to cover all the possible data points and scenarios – typically referred as data driven testing. Below is a prompt you can use to generate data for the given test case.

Can you generate a new test data json for #Roles.cs

This will generate a json as below, which you can use update with more specific requests and prompts

[
{
"Id": 1,
"Name": "Admin",
"Description": "Administrator role with full permissions",
"CreatedOn": "2022-01-01T00:00:00",
"UpdatedOn": "2022-01-01T00:00:00",
"IsDeleted": false
},
{
"Id": 2,
"Name": "User",
"Description": "Standard user role with limited permissions",
"CreatedOn": "2022-01-01T00:00:00",
"UpdatedOn": "2022-01-01T00:00:00",
"IsDeleted": false
}
]

Finally, you can update the setup method to initialize the roles data with the json file above

Can you update Setup method to read RolesData.json and initialize the roles
Conclusion

Unit Testing is what determines the quality of the code, design of the overall application and various other KPIs that you as a developer is reviewed on. Learning how you can use GitHub Copilot to generate the unit test cases, Mock services and even generate data.

Leave a Reply

Your email address will not be published. Required fields are marked *