Created
September 1, 2024 13:28
-
-
Save d1820/3b49b6e25a1e4c539dac7430c7287d50 to your computer and use it in GitHub Desktop.
Fast Run Unit Tests Pipeline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
parameters: | |
runTests: false | |
testNamespacePattern: '' | |
BuildConfiguration: 'Debug' | |
testResultFolder: 'TestResults' | |
reports: '**/TestResults/**/*.xml' | |
targetdir: './TestResults/Report/' | |
testSearchPath: '*.csproj' | |
testProjects: '(Tests|Test)' | |
coverageFileExclusions: "**/entities/**/*,**/models/**/*,**/mocks/**/*,**/obj/**/*,**/*Test*/**/*" | |
testExclusions: "Integration.Tests" | |
coverageType: 'cobertura' | |
#Open Issue: https://github.com/dotnet/sdk/issues/29742 | |
#Have to run build and tests in Debug configuration | |
steps: | |
- task: UseDotNet@2 | |
displayName: 'Installing sdk' | |
condition: eq('${{ parameters.runTests }}', true) | |
inputs: | |
packageType: 'sdk' | |
version: '7.x' | |
installationPath: $(Agent.ToolsDirectory)/dotnet | |
- task: DotNetCoreCLI@2 | |
displayName: 'Restore dependencies' | |
condition: eq('${{ parameters.runTests }}', true) | |
inputs: | |
command: 'restore' | |
projects: '**/CattleCom-Cloud-Solution/*.sln' | |
- task: DotNetCoreCLI@2 | |
displayName: 'Building solution' | |
condition: eq('${{ parameters.runTests }}', true) | |
inputs: | |
command: 'build' | |
projects: '**/CattleCom-Cloud-Solution/*.sln' | |
configuration: ${{ parameters.BuildConfiguration }} | |
arguments: '--nologo --verbosity quiet --no-restore' | |
# We run this in a script to narrow the scope so we can speed up the testing process | |
- task: PowerShell@2 | |
displayName: 'Run unit tests' | |
condition: and(eq('${{ parameters.runTests }}', true), succeeded()) | |
inputs: | |
targetType: 'inline' | |
script: | | |
$matchPattern = "(${{ parameters.testNamespacePattern }}|domainLibrary)" | |
$testExclusions = "${{ parameters.testExclusions }}" | |
Write-Host "**********************************************************************" | |
Write-Host "* Searching for test projects like '${{ parameters.testSearchPath }}' " | |
Write-Host "* Excluding test projects with '${{ parameters.testExclusions }}' " | |
Write-Host "* Finding projects matching '$matchPattern'" | |
$projects = Get-ChildItem "${{ parameters.testSearchPath }}" -Recurse -File | Where-Object { | |
if ($testExclusions.Length -gt 0) { | |
$_.Directory -match "${{ parameters.testProjects }}$" -and $_.Directory -notmatch "$testExclusions" -and $_.FullName -match "($matchPattern)" | |
} else { | |
$_.Directory -match "${{ parameters.testProjects }}$" -and $_.FullName -match "($matchPattern)" | |
} | |
} | |
Write-Host "* Matched $($projects.Count) test projects" | |
$projects | ForEach-Object { | |
Write-Host "* $($_.Name)" | |
} | |
Write-Host "**********************************************************************" | |
Write-Host "" | |
$projects | ForEach-Object { | |
Write-Host "Running tests for project: $($_.Name)" | |
Write-Host "dotnet test $($_.FullName) --configuration=${{ parameters.BuildConfiguration }} --no-build --nologo /p:nodeReuse=false /p:CopyLockLockFileAssemblies=true --logger=trx --collect=`"XPlat Code Coverage`" --results-directory=./${{ parameters.testResultFolder }}/ -- /Parallel DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=${{ parameters.coverageType }}" DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.ExcludeByFile="${{ parameters.coverageFileExclusions }}" | |
$result | |
dotnet test $_.FullName --configuration=${{ parameters.BuildConfiguration }} --no-build --nologo /p:nodeReuse=false /p:CopyLockLockFileAssemblies=true --logger=trx --collect="XPlat Code Coverage" --results-directory=./${{ parameters.testResultFolder }}/ -- /Parallel DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format="${{ parameters.coverageType }}" DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.ExcludeByFile="${{ parameters.coverageFileExclusions }}" | ForEach { | |
Write-Host $_ | |
$result = $result + $_ | |
} | |
$failed = $result | Select-String -Pattern 'Failed!' -SimpleMatch | |
if ($failed -ne $null) | |
{ | |
Write-Host Test run complete for $($_.Name) ExitCode: $LASTEXITCODE | |
exit $LASTEXITCODE; | |
} | |
} | |
- task: PublishTestResults@2 | |
displayName: 'Publish test results' | |
condition: and(eq('${{ parameters.runTests }}', true), succeeded()) | |
inputs: | |
testResultsFiles: '**/${{ parameters.testResultFolder }}/**/*.trx' | |
testRunTitle: 'Unit Tests' | |
testResultsFormat: 'VSTest' | |
publishRunAttachments: true | |
- task: PublishCodeCoverageResults@1 | |
displayName: 'Publish code coverage report' | |
condition: and(eq('${{ parameters.runTests }}', true), succeeded()) | |
inputs: | |
codeCoverageTool: '${{ parameters.coverageType }}' | |
summaryFileLocation: '**/${{ parameters.testResultFolder }}/**/coverage.${{ parameters.coverageType }}.xml' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment