How to Use Azure DevOps for Automated Testing in Full Stack .NET
Azure DevOps is a powerful platform that integrates with .NET to automate the build, test, and deployment processes, helping you deliver high-quality applications with continuous integration (CI) and continuous delivery (CD). In a full-stack .NET application, this includes automating unit tests, integration tests, and UI tests (for front-end and back-end interactions).
Here's a detailed guide on how to set up automated testing in Azure DevOps for your full-stack .NET application:
1️⃣ Overview of the Full-Stack .NET Project
A full-stack .NET application might involve:
Back-end: ASP.NET Core Web API or MVC application (e.g., a RESTful API).
Front-end: ASP.NET Core MVC, React, or Angular (connected to the back-end API).
Testing: Unit tests (for back-end), integration tests (API-level), and UI tests (front-end).
2️⃣ Setting Up Azure DevOps
Create an Azure DevOps Organization (if you don’t have one already):
Go to Azure DevOps
and create a free account.
Create a new project in your Azure DevOps organization.
Add your .NET solution to a Git repository in Azure DevOps.
Set Up Azure Pipelines for automated build and testing.
3️⃣ Automated Testing Workflow in Azure DevOps
Azure DevOps uses Azure Pipelines for continuous integration and continuous delivery (CI/CD). Automated testing can be integrated directly into the build pipeline to run tests each time code is pushed to the repository.
Here's how you can automate unit tests, integration tests, and UI tests for your full-stack .NET Core project.
1. Setup Continuous Integration (CI) Pipeline
You need to create a build pipeline that compiles your code and runs tests automatically on every commit.
Step-by-Step Process:
Create a New Pipeline:
In your Azure DevOps project, navigate to Pipelines and click New Pipeline.
Choose your repository source (e.g., GitHub, Azure Repos).
Select .NET Core as the pipeline template (or start from scratch).
Install Required Tools (if not using the template):
Ensure that .NET SDK is installed on the build agent.
Use the UseDotNet task in Azure DevOps to install the correct version of the .NET SDK.
Example YAML for the pipeline:
pool:
vmImage: 'windows-latest'
steps:
- task: UseDotNet@2
inputs:
packageType: 'sdk'
version: '5.x'
- task: Restore@1
inputs:
restoreSolution: '**/*.sln'
- task: Build@1
inputs:
solution: '**/*.sln'
- task: Test@2
inputs:
testSelector: 'testAssemblies'
testAssembly: '**/*.Tests.dll'
testFilterCriteria: ''
searchFolder: $(System.DefaultWorkingDirectory)
publishTestResults: true
Add Unit Test Task:
The Test task is used to run the unit tests. In this example, we specify the test assemblies (*.Tests.dll) where the unit tests are located.
Publish Test Results:
Use publishTestResults: true to make test results available within Azure DevOps.
Run the Pipeline:
After setting up your pipeline, run it, and check that the unit tests are executed correctly.
Azure DevOps will generate a test report where you can see the results (pass/fail).
2. Add Integration Testing in the CI Pipeline
Integration tests verify how your API or different services in your system interact with each other. These tests might need real databases or external services to simulate the application’s behavior.
Example Setup for Integration Tests:
- task: DotNetCoreCLI@2
inputs:
command: 'restore'
projects: '**/*.csproj'
- task: DotNetCoreCLI@2
inputs:
command: 'build'
projects: '**/*.csproj'
- task: DotNetCoreCLI@2
inputs:
command: 'test'
projects: '**/*.Tests.Integration.csproj'
arguments: '--logger trx'
publishTestResults: true
In this case, the pipeline restores dependencies, builds the solution, and runs the integration tests located in the *.Tests.Integration.csproj project.
3. Setup UI Testing for Frontend (Selenium, Cypress, Playwright)
If your full-stack .NET application includes a frontend (ASP.NET Core MVC, React, Angular), you’ll need to run UI tests. You can automate UI tests using tools like Selenium, Cypress, or Playwright.
UI Testing with Selenium (Example)
Install Selenium WebDriver: In your test project, install the Selenium WebDriver NuGet packages.
dotnet add package Selenium.WebDriver
dotnet add package Selenium.WebDriver.ChromeDriver
Add UI Test Code:
In your test project (e.g., MyApp.Tests.UI), create test cases using Selenium to interact with your web pages.
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using Xunit;
public class UiTests
{
[Fact]
public void HomePageLoadsSuccessfully()
{
using (IWebDriver driver = new ChromeDriver())
{
driver.Navigate().GoToUrl("http://localhost:5000");
Assert.Contains("Home", driver.PageSource);
}
}
}
Run UI Tests in Azure Pipelines:
In your pipeline YAML file, add a task to run Selenium or other UI tests.
- task: DotNetCoreCLI@2
inputs:
command: 'test'
projects: '**/*.Tests.UI.csproj'
- task: PublishTestResults@2
inputs:
testResultsFiles: '**/TestResults/*.xml'
testRunTitle: 'UI Tests Results'
UI Testing with Cypress (Example)
For Cypress or Playwright, the pipeline can run the tests in headless mode (without a browser window).
Install Cypress (example for JavaScript/TypeScript frontend):
npm install cypress --save-dev
Configure Cypress to run headlessly:
// cypress.json
{
"baseUrl": "http://localhost:5000"
}
Add Cypress Commands in Pipeline:
- task: NodeTool@0
inputs:
versionSpec: '16.x'
displayName: 'Install Node.js'
- script: |
npm install
npx cypress run --headless
displayName: 'Run Cypress Tests'
4️⃣ Setting Up Continuous Deployment (CD) for Full Stack .NET
In addition to continuous testing, you can automate deployments with CD pipelines.
Create a New Release Pipeline:
Navigate to Pipelines → Releases.
Define the stages (Dev, Test, Prod) and deploy your application to each environment.
Configure Deployment Tasks:
Use Azure DevOps tasks to deploy your back-end API (ASP.NET Core) and front-end (React/Angular) to Azure App Service, Kubernetes, or any target.
Example deployment for an ASP.NET Core Web API:
- task: AzureWebApp@1
inputs:
azureSubscription: 'MyAzureSubscription'
appName: 'MyAppServiceName'
package: '$(System.DefaultWorkingDirectory)/drop/MyApp.zip'
5️⃣ Best Practices for Automated Testing in Azure DevOps
Run Unit Tests Early: Ensure unit tests are always executed first in the pipeline to catch issues early.
Test Environments: Set up staging or test environments to run integration tests before deploying to production.
Use Docker: For consistent testing environments, especially with integration and UI tests, use Docker containers for running tests against databases or services.
Parallel Test Execution: To speed up your pipeline, use parallel jobs to run different test suites (unit, integration, UI) simultaneously.
Test Results Reporting: Always publish test results and enable feedback loops so developers can see failing tests immediately.
6️⃣ Summary
Azure DevOps provides an integrated platform for automating build, test, and deployment for your full-stack .NET applications.
Set up unit tests, integration tests, and UI tests in your pipeline to ensure code quality and catch issues early.
Use tools like Selenium, Cypress, or Playwright for front-end testing, and Moq for mocking dependencies in back-end tests.
Implement continuous integration (CI) for automatic testing on every commit and continuous deployment (CD) for automatic release of your application.
With Azure Pipelines, you can automate testing, enhance code quality, and deploy faster, ensuring a smoother development and delivery process.
Learn Dot Net Course in Hyderabad
Read More
Test-Driven Development (TDD) in .NET Core
Mocking and Stubbing in Unit Testing for .NET Core
Writing Automated UI Tests for Full Stack .NET Applications
Testing Web APIs in .NET with Postman and xUnit
Visit Our Quality Thought Institute in Hyderabad
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments