Home  Automation-testing-blog   Steps for s ...

Steps for setting up a .NET automation testing project

Let’s go through the typical end-to-end steps for setting up a .NET automation testing project and running it in a CI/CD pipeline, covering unit, API, UI, performance, and reporting.


⚙️ 1️⃣ Setting up the Automation Project (Local Setup)

Step 1: Choose the Test Framework

Common .NET test frameworks:

Example (xUnit in .csproj):

<ItemGroup>
  <PackageReference Include="xunit" Version="2.5.0" />
  <PackageReference Include="xunit.runner.visualstudio" Version="2.5.0" />
</ItemGroup>

Step 2: Decide the Type of Automation

TypeFramework/Library
Unit testingxUnit / NUnit
API testingRestSharp / HttpClient + FluentAssertions
UI testingSelenium / Playwright / Cypress (JS)
PerformanceJMeter / k6 / NBomber (.NET load testing)

Step 3: Project Structure Example

AutomationFramework/
 ┣ src/
 ┃ ┗ MyApp/
 ┣ tests/
 ┃ ┣ UnitTests/
 ┃ ┣ ApiTests/
 ┃ ┗ UiTests/
 ┣ drivers/
 ┣ reports/
 ┗ ci/

Step 4: Implement Base Classes & Utilities

Example — Base API Test:

public class BaseApiTest
{
    protected RestClient client;

    public BaseApiTest()
    {
        client = new RestClient("https://api.example.com");
    }
}

Sample Test:

[Fact]
public async Task GetUsers_ShouldReturn200()
{
    var request = new RestRequest("/users", Method.Get);
    var response = await client.ExecuteAsync(request);
    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}

Step 5: Add Logging & Reporting

Use:

Example with Serilog:

Log.Logger = new LoggerConfiguration()
    .WriteTo.File("logs/test.log", rollingInterval: RollingInterval.Day)
    .CreateLogger();

🧪 2️⃣ Local Test Execution

Run from CLI:

dotnet test

or run specific tests:

dotnet test --filter TestCategory=Smoke

You can output results in TRX or JUnit XML for CI systems:

dotnet test --logger:"trx;LogFileName=TestResults.trx"

🚀 3️⃣ CI/CD Integration (Azure DevOps / GitHub Actions / Jenkins)

Step 1: Set Up Build Pipeline

Typical stages:

1️⃣ Checkout code  
2️⃣ Restore dependencies (dotnet restore)  
3️⃣ Build project (dotnet build)  
4️⃣ Run unit/API/UI tests (dotnet test)  
5️⃣ Publish test results  
6️⃣ Deploy to environment (for post-deploy tests)

Step 2: Example – GitHub Actions

name: .NET Test Automation

on: [push, pull_request]

jobs:
  build-and-test:
    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 --no-restore

    - name: Run Tests
      run: dotnet test --logger "trx;LogFileName=test_results.trx" --results-directory TestResults

Step 3: Example – Azure DevOps YAML

trigger:
- main

pool:
  vmImage: 'windows-latest'

steps:
- task: DotNetCoreCLI@2
  inputs:
    command: 'restore'
    projects: '**/*.csproj'

- task: DotNetCoreCLI@2
  inputs:
    command: 'build'
    projects: '**/*.csproj'

- task: DotNetCoreCLI@2
  inputs:
    command: 'test'
    projects: '**/*Tests.csproj'
    arguments: '--logger trx'

🧩 4️⃣ Post-Deployment / Regression Testing

After deployment:


🔒 5️⃣ Security, Quality & Accessibility Gates


📊 6️⃣ Reporting & Notifications


Summary Flow

StageToolPurpose
Builddotnet buildCompile code
Unit TestsxUnit / NUnitTest logic
API TestsRestSharp / HttpClientValidate endpoints
UI TestsSelenium / PlaywrightValidate user journeys
ReportsAllure / Extent / trxView results
CI/CDGitHub / Azure / JenkinsContinuous delivery
Code QualitySonarQubeMaintain standards

Published on: Oct 06, 2025, 11:36 PM  
 

Comments

Add your comment