This blog post will delve into practical tools and insightful strategies for leveraging Artificial Intelligence to automate the creation of API functional tests. Furthermore, we will explore how to seamlessly integrate these AI-generated tests into a robust GitHub Actions Continuous Integration/Continuous Delivery (CI/CD) pipeline. This integration will enable automated execution of tests with every code change, significantly enhancing the efficiency and reliability of your software development lifecycle.
Background
I've recently found that using GitHub Copilot with IntelliJ (specifically, this plugin) is incredibly effective, especially for writing unit tests. It's been a while since I've actively written unit tests, and I sometimes worry about the future of Test-Driven Development (TDD). While transitioning from TDD to relying on AI for unit test generation took some adjustment, I've come to realize its significant benefits. Most notably, it's a huge time-saver, leading to much faster code deployments.
With AI now automating unit tests, the subsequent stage in the SDLC involves executing API functional or smoke tests in lower environments such as QA or Staging. This realization prompted me to investigate tools capable of enabling AI to generate these functional tests.
This blog outlines three key objectives:
- Automating API functional test creation using AI
- Enabling flexible execution of these tests across development and QA environments.
- Integrating these tests into the CI/CD pipeline via GitHub Actions.
Automating API functional test creation using AI
Begin by creating an API specification document using Swagger. A sample specification file can be found here. Once you have the apispec.json file, upload it as a collection in Postman.
Upon successful import, your APIs will be displayed in the collection, ready for invocation.
Access the "API-AI-IntegratorsAudienceGateway" collection. From there, select "View More Actions" (...), then "more," and finally "Generate Tests."
Postbot AI can even assert on negative responses. Simply click "Write tests for Bad Response" and allow AI to handle the process.
Wanna add more tests? Just use PostBot Chat. Type what you need, and the AI will handle it!
You can see that the following test is created
pm.test("Response is in JSON format", function () {
pm.expect(pm.response.headers.get('Content-Type')).to.include('application/json');
});
Execute the test in different environments
Once the tests have been meticulously saved, the next crucial step is to initiate their execution by clicking on "Run Collection." Upon activation, you will witness a seamless display of all tests undergoing execution. This feature proves invaluable within a collaborative team environment, fostering efficiency and robust functionality. Multiple developers can concurrently execute these tests before merging, thus speeding up development and streamlining workflows.
When using cloud infrastructure like AWS, correctly configuring credentials is vital. This provides tests with necessary permissions to interact securely and reliably with cloud resources, ensuring accurate results and maintaining integrity.
Integrating these tests into the CI/CD pipeline via GitHub Actions
Once testing is done, it's super important to get it into the CI/CD pipeline. That way, we can make sure tests run for every new feature that gets merged into the main branch, which helps keep our code stable and strong.
For Continuous Integration/Continuous Deployment (CI/CD), I utilize GitHub Actions (GHA), though other tools such as Jenkins and GitLab are also available. To configure GitHub Actions, a .github
folder and a YAML configuration file must be added to the repository. An example can be found here. The GHA workflow performs the following actions:
- The workflow is initiated upon each commit pushed to the main branch.
- The code is checked out, and the Node.js environment is configured.
- The Newman CLI is installed.
- The tests are executed.
- The results are uploaded in JSON format as a downloadable artifact
name: Run Postman Tests
on:
push:
branches:
- main
jobs:
postman-tests:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Install Newman CLI
run: npm install -g newman
- name: Run Postman Collection
run: |
newman run postman/collection.json \
--reporters cli,json \
--reporter-json-export results/report.json
- name: Upload Postman Report
uses: actions/upload-artifact@v4
with:
name: postman-report
path: results/report.json
Conclusion
Using AI to create API functional tests with GitHub Actions is pretty cool! It basically automates test creation, which saves a ton of time and makes our code more stable. This all means we can deliver awesome, high-quality software way more efficiently.
Top comments (0)