The Unexpected Ally in My Project: GitHub Copilot

As an engineer, I'm always seeking to improve my workflow, deliver robust software, and stay ahead of the ever-evolving tech curve. One such instance is my ongoing endeavor to transition my project from a proof of concept to a reliable service. I've been writing automated tests extensively for both the hardware and garden services, which not only ensures code quality but is also a fundamental step towards establishing a solid CI/CD pipeline.

During this rigorous process, I stumbled upon an unexpected ally - GitHub Copilot.

For those who aren't familiar with it, GitHub Copilot is an AI-powered coding assistant that helps write new code and understand existing codebases. It provides recommendations for completing lines or blocks of code, allowing you to program faster and with fewer errors.

Admittedly, my initial experiences with Copilot were mixed. In fact, I only retained the service due to a billing oversight. However, my perception drastically changed when I accidentally left Copilot enabled while writing a test. Using a descriptive function name, Copilot conjured a remarkably precise and complex test, which truly astounded me.

Here's an interesting example: I provided context via detailed comments, like this:

/**
 * Given a valid HardwareController with a single Hardware of type 'light'
 * When a POST request is made to /hardwarecontroller/
 * Then a 201 status code is returned
 * And the HardwareController is returned
 * And the HardwareController is saved to the database
 * And the HardwareController contains a list of Hardware of size 1
 * And the HardwareController contains a Hardware of type 'light'
 */        

And the output Copilot produced was nothing short of impressive.

@Test
void createHardwareController_whenGivenAValidHardwareControllerWithAHardware_shouldReturnTheHardwareController() throws Exception {
    HardwareController hardwareController = new HardwareController();
    hardwareController.setSerialNumber("123456789");
    hardwareController.setName("Test Hardware Controller");
    Hardware hardware = new Hardware();
    hardware.setType("light");
    hardwareController.getHardware().add(hardware);
    String hardwareControllerJson = objectMapper.writeValueAsString(hardwareController);
    MvcResult result = mockMvc.perform(post("/hardwarecontroller/")
                    .content(hardwareControllerJson)
                    .contentType("application/json")
                    .content(hardwareControllerJson))
            .andExpect(status().isCreated())
            .andReturn();
    HardwareController createdHardwareController = objectMapper.readValue(result.getResponse().getContentAsString(), HardwareController.class);

    //retrieve the hardware controller from the db
    HardwareControllerEntity hardwareControllerEntity = hardwareControllerRepository.findAll().get(0);

    //check the values in the response
    assertEquals(hardwareControllerEntity.getId(), createdHardwareController.getId());
    assertEquals(1, createdHardwareController.getHardware().size());
    assertEquals("light", createdHardwareController.getHardware().get(0).getType());
}        


What's fascinating is that it seemed to improve as I wrote more tests, appearing to learn from the context of my existing code. However, I should note that this could be observer bias since I haven't found any official documentation confirming this aspect of Copilot's behavior.

Despite these benefits, I must stress that any code generated by Copilot should be reviewed thoroughly. While it often produces valuable output, it can sometimes generate inaccurate or even erroneous code. Proceeding with caution is crucial!

For those interested in seeing the results of these automated tests and the ongoing development of this project, feel free to check out the hardware service code here on GitHub. All of the integration tests have been written using this technique so far. Keep in mind that this is an ongoing project, continually being refined and improved.

I'm curious to hear about your experiences with GitHub Copilot. Do you have any tips for getting the most out of this AI-powered tool? Let's start a discussion in the comments section below.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了