Showing posts with label Unit test. Show all posts
Showing posts with label Unit test. Show all posts

Wednesday, January 22, 2025

Speeding Up ASP.NET Framework Unit Tests in Azure DevOp

Speeding Up ASP.NET Framework Unit Tests in Azure DevOps

Unit tests are critical for ensuring code quality, but their execution time can often slow down your CI/CD pipeline. In this article, we’ll explore strategies to accelerate unit test execution for ASP.NET Framework applications in Azure DevOps pipelines.

1. Optimize Test Parallelization

Parallelizing your tests can significantly reduce execution time. Ensure your tests are independent and can run in parallel without shared state conflicts. Use the RunSettings file to enable parallel test execution:


<!-- RunSettings file -->
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
  <RunConfiguration>
    <MaxCpuCount>4</MaxCpuCount>
  </RunConfiguration>
</RunSettings>
    

Include this file in your pipeline configuration.

2. Use Test Filters

Run only the necessary tests by using test filters. This is particularly useful when you’re working on a specific feature or bug fix. Update your pipeline YAML as follows:


- task: VSTest@2
  inputs:
    testSelector: 'testAssemblies'
    testAssemblyVer2: '**/*Tests.dll'
    testFiltercriteria: 'TestCategory=SmokeTest'
    runSettingsFile: '$(System.DefaultWorkingDirectory)/RunSettings.runsettings'
    diagnosticsEnabled: true
    platform: '$(BuildPlatform)'
    configuration: '$(BuildConfiguration)'
    publishRunAttachments: true
    runInParallel: true
    codeCoverageEnabled: true
    testRunTitle: 'Smoke Test Execution'
    failOnMinCoverage: true
    runTestsInIsolation: true
    rerunFailedTests: true
    testRunParameters: >
      DatabaseConnectionString=$(DatabaseConnectionString);
    # Insert appropriate environment variables here.
      ServiceName=$(ServiceName);

    pipelineConfig:
      Use templates in jobs. Use mock dynamic variables to ensure business units are scoped contextually to their parent org structure

Tuesday, July 1, 2014

Nunit Framework Adapter VS2013

If you're working with Nunit framework and you want to get this inside Team foundation server /VS2013 then follow these steps


Benefits:
1. Test Explorer console appears as given below
2. TFS ALM -application life cycle management. More to come.

To add it is a Nuget package, you must have an active solution, then follow these steps:
  1. From Tools menu, use Library Package Manager, select Manage NuGet packages for solution
  2. In the left panel, select Online
  3. Locate (search for) NUnit Test Adapter in the center panel and highlight it
  4. You will find two packages, one without framework and one with framework. See notes below for which to choose.
  5. Click 'Install'
  6. In the "Select Projects" dialog, you need to select at least one project to add the adapter to, see notes below.
http://nunit.org/index.php?p=vsTestAdapter&r=2.6.2



 

Wednesday, June 11, 2014

AggregateException in Unit testing Task Parallel Library Asyc Call

Some Background Exception Handling (Task Parallel Library)

If a task is the parent of attached child tasks, or if you are waiting on multiple tasks, then multiple exceptions could be thrown. To propagate all the exceptions back to the calling thread, the Task infrastructure wraps them in an AggregateException instance

http://msdn.microsoft.com/en-us/library/dd997415(v=vs.110).aspx

Problem Statement:

Most of the time when we create a unit testing on Async Method- TPL it executes the unit test completely but ends up throwing AggregateException. This is due to the fact that it completes the one set of flow completely within a thread and comes out without executing all thread in parallel. If you see there is no UI and hence while testing the TPL must understand that unit testing is just to check the conditions of expected output with actual given a input as required whereas TPL works in different context. So ideally we must create test on non TPL methods in such case create separate methods to test on.
http://blog.stephencleary.com/2012/02/async-unit-tests-part-1-wrong-way.html

•An asynchronous method call will either produce a result immediately if one is available and continue execution in the current method or it will produce a result at a later time and immediately return control to the caller of the current method.
•The code that comes after an awaited method – the continuation – is what will be executed when the awaited method returns with a result.
http://www.jayway.com/2012/10/07/asyncawait-in-c-a-disaster-waiting-to-happen/

Workaround:

Brute force method not recommended but can be used if just unit test case conditions with failed and pass data.
[TestClass]
public class TestAsyncClassTest {
    [TestMethod]
    [ExpectedException(typeof(AggregateException))]
    public void SomeAsyncTest() {
    }
}

Sunday, June 8, 2014

Data Driven Excel data source Unit Testing Using VS 2013

 

Introduction:- We're using excel as data source for data driven unit testing.



Test Data:


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Trobleshooting Issues:
Probable Error: "Excel microsoft.ace.oledb.12.0 provider is not registered"
Download and install
2007 Office System Driver: Data Connectivity Components