In today’s fast-paced world of software development, speed, reliability, and automation are everything. When it comes to testing APIs, having a robust automation framework is just half the battle. The real magic happens when you integrate these tests seamlessly into your Continuous Integration/Continuous Deployment (CI/CD) pipeline. In this blog, we’ll walk through how to automate API tests using RestSharp, and how to integrate those tests with Azure DevOps and GitHub Actions.
What is RestSharp?
Rest Sharp is a popular .NET library for making HTTP requests and testing RESTful APIs. It simplifies sending requests and parsing responses in C#. It’s developer-friendly, widely used in automation testing, and integrates well with test frameworks like NUnit or xUnit.
Why Automate API Tests?
Catch bugs early: API tests help identify issues before they affect users.
Consistency: Automated tests run the same way every time.
Speed: Tests can run much faster than manual verification.
Scalability: Easily test large endpoints, edge cases, and data combinations.
Setting Up RestSharp for API Testing
- Create a .NET test project
dotnet new nunit -n RestSharpAPITests
cd RestSharpAPITests
- Install RestSharp and NUnit
dotnet add package RestSharp
dotnet add package NUnit
dotnet add package Microsoft.NET.Test.Sdk
- Basic Test Example
using NUnit.Framework;
using RestSharp;
namespace RestSharpAPITests
{
public class APITests
{
[Test]
public void GetUsers_ShouldReturnSuccess()
{
var client = new RestClient("https://reqres.in");
var request = new RestRequest("/api/users?page=2", Method.Get);
var response = client.Execute(request);
Assert.That((int)response.StatusCode, Is.EqualTo(200));
}
}
}
Adding to CI/CD: Azure DevOps
Once you have your tests ready, it’s time to automate running them in Azure DevOps.
Step 1: Create a new pipeline
Go to Pipelines > New Pipeline
Choose your repo (GitHub, Azure Repos, etc)
Use the classic editor or YAML (we’ll use YAML)
Step 2: Add a YAML file to your repo
Create a file named azure-pipelines.yml at the root of your repository.
trigger:
- main
pool:
vmImage: 'windows-latest'
steps:
- task: UseDotNet@2
inputs:
packageType: 'sdk'
version: '8.x.x'
- script: dotnet restore
displayName: 'Restore dependencies'
- script: dotnet build --configuration Release
displayName: 'Build project'
- script: dotnet test --no-build --verbosity normal
displayName: 'Run API tests'
Step 3: Commit and push
As soon as you push to your main branch, Azure will automatically trigger the pipeline and run your tests.
Adding to CI/CD: GitHub Actions
Prefer GitHub Actions? No problem. It’s just as simple.
Step 1: Create a workflow file
In your repo, go to .github/workflows and create a file named api-tests.yml.
name: Run API Tests
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: '8.0.x'
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release
- name: Run tests
run: dotnet test --no-build --verbosity normal
Step 2: Push the workflow
GitHub Actions will automatically detect your workflow and run the test suite whenever you push or create a pull request.
Best Practices for API Testing in CI/CD
- Use test environments: Don’t run tests against production APIs.
- Data management: Clean up test data after creation.
- Use secrets securely: Use secret variables to store API keys, tokens, or credentials.
- Fail fast: Make your tests fail early and clearly.
- Tag tests: Tag long-running or unstable tests so you can filter them in CI.
- Add test reports: You can integrate
reportgenerator,Allure, orExtentReportsfor nicer output.
Debugging Failures
Both Azure and GitHub provide detailed logs for test runs. If something fails:
- Check the full logs.
- See which step failed.
- Review test output and stack trace.
- Rerun locally using the same command as in CI:
dotnet test --no-build
Bonus: Parallel Test Execution
To speed things up:
- Add
--parallelflag in test commands. - Split tests by category or class.
dotnet test --no-build --filter Category=Smoke
Summary and Takeaways
Integrating RestSharp API tests into your CI/CD pipelines using Azure DevOps and GitHub Actions can significantly streamline your testing process and improve delivery confidence. By automating these tests, teams can catch issues earlier in the development cycle, ensure consistent performance, and maintain higher code quality. Whether you’re using Azure or GitHub, the key lies in setting up a reliable and repeatable pipeline that runs your tests efficiently. With the right configuration and good practices in place, API testing becomes a natural part of your development workflow, helping your team deliver faster and with greater reliability.
Reference
“Testing Web APIs”
By: Mark Winteringham
Focuses on API testing strategies, helpful context for integrating tools like RestSharp in automation pipelines.
“Clean Code: A Handbook of Agile Software Craftsmanship”
By: Robert C. Martin
While not focused on CI/CD directly, its emphasis on clean, testable code is essential when writing maintainable API tests.
For more Tech related blogs refer to Nashtech blogs