Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon

Writing improvised unit test cases with GitHub Copilot

Save for later
View related Packt books & videos

article-image

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. 

Setup your account at GitHub Copilot Account 

Let’s first create your account with GitHub Copilot and then use that account in IDE such as VS Code. 

writing-improvised-unit-test-cases-with-github-copilot-img-0 

Image 1:  GitHub Copilot · Your AI pair programmer. 

  • Click on the Get Copilot button and start your 30-day free trial but we need to enter the credit/debit card or pay-pal account details.  
  • After this, activate your GitHub Copilot account by following the instruction mentioned in the GitHub quickstart guide for individual or organization access. 
  • writing-improvised-unit-test-cases-with-github-copilot-img-1 

Image 2: Activate Github Copilot in Settings 

Setup in Visual Studio Code  

writing-improvised-unit-test-cases-with-github-copilot-img-2 

Image3: Visual Studio Home 

​​​Create a Python Function is_prime 

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. 

  • First, we create a Python file (prime_number.py) from the file tab by selecting the new file option or we can press Alt + Ctrl + N. After creating the Python script file, we will create the 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: 

writing-improvised-unit-test-cases-with-github-copilot-img-3 

Image 4: Doc-string suggestions from the Copilot 

  • ​​​​​After creating the file, we added the 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. 

writing-improvised-unit-test-cases-with-github-copilot-img-4 

Image 5: Github Copilot suggests an if statement 

  • After suggesting the if statement, Copilot suggests the return value True as shown below snapshot:  

writing-improvised-unit-test-cases-with-github-copilot-img-5 

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. 

​Write Unit Tests for Python Function is_prime 

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: 

  • First, we will create a function 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: 

writing-improvised-unit-test-cases-with-github-copilot-img-6 

Image 7: Github Copilot suggests a doc-string for the unit_test method 

  • After writing the doc-string, we will add the assertion condition for the unit test. As we can see Copilot is auto-suggesting the assertion condition for the test in the below snapshot: 

writing-improvised-unit-test-cases-with-github-copilot-img-7 

Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at $19.99/month. Cancel anytime

Image 8: Github Copilot suggests assertion condition 

  • Similarly, Copilot is suggesting the other assertion conditions with different possible inputs for testing the is_prime() function as shown below snapshot: 

writing-improvised-unit-test-cases-with-github-copilot-img-8 

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: 

writing-improvised-unit-test-cases-with-github-copilot-img-9 

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. 

Advantages and Disadvantages of the Copilot Unit Test  

  • GitHub Copilot offers the following advantages and disadvantages: 
  • Improve productivity and reduce development time by offering efficient test cases. 
  • Developers can focus on more significant problems rather focus on small code snippets for unit testing. 
  • A novice coder can also easily utilize its capability to ensure quality development. 
  • Sometimes it may suggest irrelevant or vulnerable code because it doesn’t have proper context and user behavior. As a developer, we also need to consider how edge cases are covered in unit tests.  
  • Sometimes it may lead to syntax errors, so we must verify the syntax. 

Summary 

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. 

Reference 

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/ 

Author Bio

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