In the last couple of years, AI and ML have provided various tools and methodologies to transform the working styles of software developers. GitHub Copilot is an AI-powered tool that offers code suggestions for rapid software development. GitHub Copilot can be a game changer for developers across the world. It can increase productivity by recommending code snippets and auto-completions.
In this tutorial, we will discuss the Unit tests, GitHub Copilot Features, account creation, setup VS code, JavaScript example, Python example, unit test example, and GitHub Copliot advantages and disadvantages.
Let’s first create your account with GitHub Copilot and then use that account in IDE such as VS Code.
Image 1: GitHub Copilot · Your AI pair programmer.
Image 2: Activate Github Copilot in Settings
Image3: Visual Studio Home
In this section, we create the function is_prime
with Copilot recommendation and see how it can help software developers in their coding journey. For executing any Python code, we need a Python file (with .py
an extension). This file will help the interpreter to identify that this is a Python script.
is_prime(number =)
function. This is_prime()
the function will check whether a number is prime or not. We can see in the below snapshot Copilot also suggests the docstring for the function as per the name of the function:
Image 4: Doc-string suggestions from the Copilot
is_prime
function but on writing the logic for the is_prime()
function Copilot is suggesting an internal if condition inside for loop of the is_prime()
function. We can see that suggested if statement in the below snapshot.
Image 5: Github Copilot suggests an if statement
Image 6: Github Copilot suggests return value
This is how we can write the Python function is_prime. We can also write similar functions as per your project requirement. Writing code with GitHub Copilot makes it much faster and more productive for a developer by providing code snippet suggestions and auto completions.
Unit testing is a very important component of the coding module that builds confidence and faith in software developers and ensures bug-free and quality code. The main objective of this testing is to ensure the expected functionality at individual and isolated component-level. Let’s see how we can define them using the AI-powered code suggestion platform GitHub Copilot:
unit_test_prime_number()
for unit testing the is_prime()
function. Here, we can see how Copilot suggests the doc-string in the below snapshot:
Image 7: Github Copilot suggests a doc-string for the unit_test method
Image 8: Github Copilot suggests assertion condition
Image 9: Github Copilot suggests other assertion conditions
This is how GitHub Copilot can help us in writing faster unit-test. It speeds up development and increases developer productivity through code suggestions and auto-completions.
Let’s see the final output in the below coding snapshot:
Image 10: A final program created with GitHub Copliot suggestions
In the above snapshot, we can see the complete code for the is_prime()
function and unit_test_prime_number()
. Also, we can see the output of the unit_test_prime_number()
function in the Terminal.
GitHub Copilot revolutionized the way programmers write code. It offers AI-based code suggestions and auto-completions. Copilot boosts developer productivity and rapid application development by providing accurate, relevant, robust, and quality code. Also, sometimes it may cause problems in covering the edge cases.
In the above tutorial, I have shared examples from JavaScript and Python languages. We can also try other languages such as TypeScript, Ruby, C#, C++, and Go.
GitHub Copilot Quick Start Guide: https://docs.github.com/en/copilot/quickstart
Unit tests https://www.freecodecamp.org/news/how-to-write-unit-tests-for-python-functions/
Avinash Navlani has over 8 years of experience working in data science and AI. Currently, he is working as Sr. Data scientist, Improving products and services for customers by using advanced analytics, deploying big data analytical tools, creating and maintaining models, and onboarding compelling new datasets. Previously, he was a Lecturer at the university level, where he trained and educated people in Data science subjects such as python for analytics, data mining, machine learning, Database Management, and NoSQL. Avinash has been involved in research activities in Data science and has been a keynote speaker at many conferences in India.
LinkedIn Python Data Analysis, Third Edition