Tosca tester
GitHub Copilot and software testing
With the increasing complexity of software systems, the importance of effective and comprehensive testing becomes paramount. Developers face the challenge of ensuring thorough test coverage while optimizing their workflows to meet project deadlines. GitHub Copilot offers a promising solution to this challenge by providing relevant code suggestions.
GitHub Copilot definition
GitHub Copilot is an AI-powered tool integrated into a code editor that can write code on its own based on code you’ve already written in a project. All you have to do is enter a function name or a few comments and Copilot will automatically complete the implementation. The tool processes user input in the cloud and returns a snippet (code snippet) that you can just accept, reject or ask for further solution suggestions. This tool was developed by GitHub in collaboration with OpenAI.
GitHub Copilot helps developers by providing contextually relevant code suggestions when writing code in integrated development environments (IDEs). By analyzing code context and comments, GitHub Copilot offers suggestions for completing code snippets, writing functions, and even generating entire methods or classes. The goal of this tool is to increase developer productivity and streamline the coding process by reducing the need to manually write repetitive code.
If you want to read more about GitHub Copilot, just click here – github copilot documentation. If you’re interested in a pricing, click here – github copilot pricing.
How GitHub Copilot works
You write a string of code or a comment and it suggests a snippet of code. In some cases, just the function name or part of it is enough to generate the rest of the code. The code is generated uniquely for you, so it belongs to you.
Below is a brief overview of exactly how Copilot works:
SOURCE: github.com
Copilot creates ten designs at once. As you enter more code, the suggestions will be more accurate.
GitHub Copilot benefits
- Copilot offers real-time code suggestions that can speed up the process of programming and creating automated tests. It can be particularly useful for repetitive tasks.
- For beginners, Copilot can serve as a learning tool by suggesting best practices and offering code snippets they may not be familiar with.
- Copilot supports a wide range of programming languages, making it versatile for a variety of projects.
- Unlike traditional code completion tools, Copilot understands the context of the code and offers relevant suggestions.
- Thanks to AI-driven designs, it is possible to reduce the number of syntactic and logical errors.
- Being integrated with Visual Studio Code, one of the most popular code editors, it is easily accessible to many developers.
Disadvantages of GitHub Copilot
- There is a risk that developers become overly dependent on Copilot, which can lead to a limitation of their organic coding skills.
- As impressive as Copilot is, it is not infallible. Sometimes it may suggest incorrect or inefficient code.
- Concerns have been raised that Copilot is designing code that may be copyrighted or not properly licensed.
- With automated suggestions at hand, developers can choose the first suggestion without considering whether it’s the best solution.
- Some developers are wary of sharing their code with AI, even though GitHub assures that the data is used responsibly.
How to access GitHub Copilot
The IDEs or editors that Copilot currently supports are Visual Studio Code, Visual Studio, Vim, Neovim, JetBrains (PyCharm), Azure Data Studio.
Setting up GitHub Copilot with VS Code
If you want to use the power of Copilot for software testing, you can simply set it up as an extension to Visual Studio Code. Once installed, Copilot will become a valuable asset in your development environment, offering real-time code completion suggestions tailored to your current context.
As one of the most popular IDEs in the developer community, VS Code provides excellent support for extensions, making it an ideal choice for Copilot integration.
- Install VS Code: if you haven’t already, download and install Visual Studio Code from the official website.
- Open the Extensions view: Launch VS Code and go to the Extensions view by clicking the Extensions icon in the activity bar on the side of the window.
- Search for Copilot: In the Extensions view, search for “Copilot” using the search bar at the top. Once found, click the “Install” button to add the Copilot extension to your IDE.
- Sign in to GitHub: After installing the Copilot extension, you’ll be prompted to sign in with your GitHub account. This step is necessary because Copilot requires access to your GitHub repositories for code suggestions.
- API Access: During the setup process, you may need to access the Copilot API. Follow the instructions given during the setup process to complete API access.
- Once you have successfully completed these steps, Copilot will be integrated into your VS Code IDE and you can start using it to generate code snippets as you write.
GitHub Copilot for testing
Let’s take a look at a practical approach to understanding the impact of Copilot on software testing. Let’s imagine a scenario in which we need to write test cases for a web application registration form. We start by creating a new test suite in the project and defining the initial structure of the test scenario, including test steps and expected results.
// Testovací scenár: Registračný formulár používateľa
describe('User Registration', () => {
it('should allow users to register with valid credentials', () => {
// Testovacie kroky, ktoré sa majú dokončiť pomocou návrhov systému Copilot
});
it('should display an error message for invalid email addresses', () => {
// Testovacie kroky, ktoré sa majú dokončiť pomocou návrhov systému Copilot
});
it('should require a password with a minimum length of 8 characters', () => {
// Testovacie kroky, ktoré sa majú dokončiť pomocou návrhov systému Copilot
});
});
When we start writing test scenarios for our user registration form, Copilot proves to be an indispensable helper. When we enter test steps, Copilot immediately starts suggesting code snippets that match the context. Let’s take a look at how Copilot helps us perform some of the testing steps.
it('should allow users to register with valid credentials', () => {
// Návrh Copilota pre zadanie a odoslanie formulára
cy.visit('/register');
cy.get('#username').type('testuser');
cy.get('#email').type('testuser@example.com');
cy.get('#password').type('secretpassword');
cy.get('#submit').click();
cy.url().should('include', '/dashboard');
});
Copilot provided an efficient code snippet to interact with the registration form elements and simulate user registration with valid login credentials. This significantly reduced the time and effort required to write the test steps.
it('should display an error message for invalid email addresses', () => {
// Návrh Copilot pre zadanie neplatnej e-mailovej adresy
cy.visit('/register');
cy.get('#email').type('invalidemail');
cy.get('#submit').click();
cy.get('.error-message').should('be.visible').and('have.text', 'Invalid email address');
});
In this example, Copilot effectively designed a test case to verify the display of an error message when an invalid email address is entered into the registration form.
In the provided code context, .cy refers to a function that is typically used in end-to-end (E2E) testing using Cypress, a popular JavaScript testing framework. Cypress is designed to facilitate E2E testing for web applications and provides an elegant and easy-to-use API for interacting with elements on a web page.
In Cypress, the cy.get() statement is used to search and select DOM elements on a page. It allows testers to interact with these elements by performing actions such as clicking, typing, or verifying their properties and content. The cy.get() command uses a CSS selector or a jQuery-like expression to identify the element(s) to be selected.
For example, consider the following code snippet, which uses cy.get() in a Cypress test:
cy.get(‘#username’).type(‘testuser’);
In this case, cy.get(‘#username’) selects the input field with the ID attribute set to “username” on the web page. The .type(‘testuser’) method then simulates entering the text “testuser” into the selected input field.
More Copilot examples
1. Helping non-native English speakers
GitHub Copilot understands also other languages than just English! This is useful for developers of all nationalities because the programming languages are based on American English. Forgetting correct spelling and syntax can often lead to typos, unexpected errors and wasted time.
For example, if you type a comment in Spanish that reads “importar”, which translates to “import”, GitHub Copilot quickly completes the comment in Spanish and imports the necessary libraries as described in the comment. In addition, GitHub Copilot helps translate words from English into other languages.
2. Creating dictionaries with searchable data
GitHub Copilot is great for creating dictionaries with search data. Try this by writing a comment instructing GitHub Copilot to create a dictionary of two-letter ISO country codes and their country names. Writing a comment and the first few lines of code should help GitHub Copilot generate the desired results. For a visual representation, see the image below.
3. Testing your code
Writing tests is an important but sometimes tedious step in the software development lifecycle. Since GitHub Copilot excels at pattern recognition and completion, it can speed up the process of writing unit tests, visual regression tests, and more.
4. Pattern matching using regular expressions
With GitHub Copilot, you can spend less time playing with Regex playground or going through StackOverflow to find combinations of characters in strings. Instead, you can type a comment or a function name and run Copilot’s GitHub suggestions.
Use Copilot to help verify the phone number:
GitHub Copilot will also help you remove white spaces from the string:
5. Preparation for technical interviews
Although it may sound unconventional, developers use GitHub Copilot to study for interviews.
Here’s the strategy:
First, try to solve the problem without GitHub Copilot. If you’re feeling extremely stuck and frustrated when solving a problem, activate GitHub Copilot and use it to generate ideas on how to solve the problem better.
Then delete the code generated by GitHub Copilot, deactivate GitHub Copilot, and try to think of the solution with the new information in your head. By mastering this method, you’ll maintain momentum when you’re tempted to quit. Instead of giving up, you gain a new perspective, even if you don’t have a mentor or colleague to guide you.
6. Exiting Vim
Developers who are new to Vim often ask how to quit the editor. Vim editor termination difficulties are so common that they have become a meme on the internet. Since GitHub Copilot is available in Visual Studio Code, JetBrains, and Neovim, a forked version of Vim with additional features, you can quit NeoVim with GitHub Copilot. In the following video, Brian Douglas uses GitHub Copilot to quit NeoVim by typing a comment that reads, “How do I quit vim?”
7. Navigating the new code with Copilot Labs
GitHub Copilot Labs is an add-on extension that comes with access to GitHub Copilot. The GitHub Next team developed GitHub Copilot Labs, an experimental sidebar that helps developers translate code from one programming language to another and get step-by-step explanations of code snippets.
Conclusion
Although Github Copilot has its limitations, it represents a significant step forward in using the power of AI for software testing. As the technology continues to evolve, Copilot and similar AI tools are poised to become valuable assets in the software testing arsenal.
By striking the right balance between automation and human ingenuity, testers can leverage Copilot’s full potential to optimize their testing efforts. If you want to try Copilot straight away, here is a link to the copilot extension.
If you speak German and are looking for a job as an IT tester, take a look at our employee benefits and respond to the latest job offers.