I have a .netcode test command and a publish code coverage results task in my pipeline.
config as below:
steps:
- task: DotNetCoreCLI#2
displayName: 'Test Public API Project '
inputs:
command: test
projects: '**/DWP.CDA.API.Test.csproj'
arguments: '--output publish_output --configuration $(BuildConfiguration) /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:Threshold=99 /p:ThresholdStat=total /p:CoverletOutput=$(Build.SourcesDirectory)\TestResults\Coverage\ --collect "Code coverage"'
steps:
- task: PublishCodeCoverageResults#1
displayName: 'Publish code coverage'
inputs:
codeCoverageTool: Cobertura
summaryFileLocation: '$(Build.SourcesDirectory)/TestResults/Coverage/*cobertura.xml'
reportDirectory: '($(Build.SourcesDirectory)/Src/TestResults/Coverage'
But it seems that the publish results doed not take effect,such messages will show
[warning]No code coverage results were found to publish.
Did you install and run the ReportGenerator tool as well to get code coverage report in the proper format? Your warning looks like the build task isn't finding the xml file to publish in the folder that you're looking in.
I've used the following yaml in the past to run and publish code coverage results. You will need to change it to find your projects, but otherwise it should work.
- task: DotNetCoreCLI#2
displayName: 'Install ReportGenerator'
inputs:
command: custom
custom: tool
arguments: 'install --global dotnet-reportgenerator-globaltool'
- task: DotNetCoreCLI#2
displayName: 'Run unit tests - $(buildConfiguration)'
inputs:
command: 'test'
arguments: '--no-build --configuration $(buildConfiguration) /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:CoverletOutput=$(Build.SourcesDirectory)/TestResults/Coverage/'
publishTestResults: true
projects: '**/*.Tests.csproj'
- script: |
reportgenerator -reports:$(Build.SourcesDirectory)/**/coverage.cobertura.xml -targetdir:$(Build.SourcesDirectory)/CodeCoverage -reporttypes:HtmlInline_AzurePipelines
displayName: 'Create code coverage report'
- task: PublishCodeCoverageResults#1
displayName: 'Publish code coverage report'
inputs:
codeCoverageTool: 'cobertura'
summaryFileLocation: '$(Build.SourcesDirectory)/**/coverage.cobertura.xml'
Yes, as SapuSevn commented, it should be Agent.TempDirectory.
- task: DotNetCoreCLI#2
displayName: 'dotnet test'
inputs:
command: 'test'
arguments: '--configuration $(buildConfiguration) --collect:"XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=cobertura'
publishTestResults: true
projects: 'tests/Frontends/GloboTicket.Web.Tests' # update with your test project directory
- task: PublishCodeCoverageResults#1
displayName: 'Publish code coverage report'
inputs:
codeCoverageTool: 'Cobertura'
#summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/coverage.cobertura.xml' ## This is not working. What you need is the following.
summaryFileLocation: '$(Agent.TempDirectory)/**/coverage.cobertura.xml'
I had a similar issue, take a look at the output
Attachments:
My pipeline was putting the reports into a temp folder
Also my test project was missing a Nuget coverlet.msbuild
Related
I have a workflow on GitHub to run unit tests on a Python application. I need the test to run for various Python versions and various OS versions. Right now the workflow looks like this:
name: Python tests
on:
push:
branches:
- dev
pull_request:
release:
types: [published]
jobs:
release-strategy:
push-pull-strategy:
pytest:
strategy:
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
os: [ubuntu-20.04, ubuntu-22.04]
runs-on: ${{ matrix.os }}
steps:
...
This is a bit too heavy to be run for every pull request on dev, but on the other hand we really want to run all these tests before a release in made, so we would like to run the full matrix strategy only upon release, and run a smaller subset for all other cases, eg ["3.7", "3.11"] and [ubuntu-20.04, ubuntu-22.04].
Is there a way to implement this behavior without having two separate workflows?
I realized I could describe this logic in a previous job using standard bash instructions (instead of trying to use the workflow logic). My solution looks like this:
name: Python tests
on:
push:
branches:
- dev
pull_request:
release:
types: [published]
jobs:
select-strategy:
runs-on: ubuntu-latest
outputs:
python-versions: ${{ steps.set-matrix.outputs.python-versions }}
steps:
- id: set-matrix
run: |
if [ ${{ github.event_name }} == "release" ]; then
echo "python-versions=[\"3.7\",\"3.8\",\"3.9\",\"3.10\",\"3.11\"]" >> $GITHUB_OUTPUT
else
echo "python-versions=[\"3.7\",\"3.11\"]" >> $GITHUB_OUTPUT
fi
pytest:
needs: select-strategy
strategy:
matrix:
python-version: ${{ fromJson(needs.select-strategy.outputs.python-versions) }}
os: [ubuntu-20.04, ubuntu-22.04]
runs-on: ${{ matrix.os }}
steps:
...
When i run my tests using the task VSTest#2 in Azure Pipelines, it generates a temporary .TRX file, I want to generate the same file but with a specific name:
It's generating like Administrator_TFSAGNT1533-1_2022-07-20.11_50_19.trx
I'd like to generate something like testResults.trx
My task
- task: VSTest#2
displayName: Unit Tests
inputs:
testSelector: 'testAssemblies'
testAssemblyVer2: '*(*.Test)*(*.Integration).dll'
searchFolder: '$(Agent.BuildDirectory)\git\test-bin'
resultsFolder: '$(Agent.BuildDirectory)\git\testsResult\unitTest'
vsTestVersion: 'toolsInstaller'
testFiltercriteria: 'TestCategory!=Integration'
runSettingsFile: 'CodeCoverage.runsettings'
codeCoverageEnabled: true
Figure it out:
I had to add the line otherConsoleOptions: '/Logger:"trx;LogFileName=testResults.trx"'
- task: VSTest#2
displayName: Unit Tests
inputs:
testSelector: 'testAssemblies'
testAssemblyVer2: '*(*.Test)*(*.Integration).dll'
searchFolder: '$(Agent.BuildDirectory)\git\test-bin'
resultsFolder: '$(Agent.BuildDirectory)\git\testsResult\unitTest'
vsTestVersion: 'toolsInstaller'
testFiltercriteria: 'TestCategory!=Integration'
runSettingsFile: 'CodeCoverage.runsettings'
codeCoverageEnabled: true
otherConsoleOptions: '/Logger:"trx;LogFileName=testResults.trx"'
All afternoon I have been trying to get my head around concatenating a parameter in an ADO template. The parameter is a source path and in the template a next folder level needs to be added. I would like to achieve this with a "simple" concatenation.
The simplified template takes the parameter and uses it to form the inputPath for a PowerShell script, like this:
parameters:
sourcePath: ''
steps:
- task: PowerShell#2
inputs:
filePath: 'PSRepo/Scripts/MyPsScript.ps1'
arguments: '-inputPath ''$(sourcePath)/NextFolder''
I have tried various ways to achieve this concatenation:
'$(sourcePath)/NextFolder'
see above
'$(variables.sourcePath)/NextFolder'
I know sourcePath is not a variable, but tried based on the fact that using a parameter in a task condition it apparently only works when referencing through variables
'${{ parameters.sourcePath }}/NextFolder'
And some other variations, all to no avail.
I also tried to introduce a variables section in the template, but that is not possible.
I have searched the internet for examples/documentation, but no direct answers and other issues seemed to hint to some solution, but were not working.
I will surely be very pleased if someone could help me out.
Thanx in advance.
We can add the variables in our temp yaml file and pass the sourcePath to the variable, then we can use it. Here is my demo script:
Main.yaml
resources:
repositories:
- repository: templates
type: git
name: Tech-Talk/template
trigger: none
variables:
- name: Test
value: TestGroup
pool:
# vmImage: windows-latest
vmImage: ubuntu-20.04
extends:
template: temp.yaml#templates
parameters:
agent_pool_name: ''
db_resource_path: $(System.DefaultWorkingDirectory)
# variable_group: ${{variables.Test}}
temp.yaml
parameters:
- name: db_resource_path
default: ""
# - name: 'variable_group'
# type: string
# default: 'default_variable_group'
- name: agent_pool_name
default: ""
stages:
- stage:
jobs:
- job: READ
displayName: Reading Parameters
variables:
- name: sourcePath
value: ${{parameters.db_resource_path}}
# - group: ${{parameters.variable_group}}
steps:
- script: |
echo sourcePath: ${{variables.sourcePath}}
- powershell: echo "$(sourcePath)"
Here, I just use the workingDirectory to as the test path. You can use the variables also.
Attach my build result:
Thanx, Yujun. In meantime did get it working. Apparently there must have been some typo that did block the script from executing right as the se solution looks like one of the options mentioned above.
parameters:
sourcePath: ''
steps:
- task: PowerShell#2
inputs:
filePath: 'PSRepo/Scripts/MyPsScript.ps1'
arguments: '-inputPath ''$(sourcePath)/NextFolder''
I am new to YAML and build pipelines. I am receiving the following error, can anyone advice what's wrong with the target folder.
Unhandled: Input required: TargetFolder
[warning]Directory 'D:\a\1\a' is empty. Nothing will be added to build
artifact 'drop'.
Below is my YAML file:
# Build app using Azure Pipelines
pool:
vmImage: 'vs2017-win2016'
steps:
- script: echo hello world
- task: NodeTool#0
inputs:
versionSpec: '8.x'
- task: CopyFiles#1
displayName: 'Copy Files to: $(Build.ArtifactStagingDirectory)'
inputs:
SourceFolder: '$(build.sourcesdirectory)'
Contents:
\C:\VSCodeGit\CollMod.Web\Web.config\
TartgetFolder: '$(Build.ArtifactStagingDirectory)'
condition: succeededOrFailed()
- task: PublishBuildArtifacts#1
displayName: 'Publish Artifact: drop'
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
condition: succeededOrFailed()
I think it's the contents field that looks to be invalid here.
The docs at https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/copy-files?view=vsts&tabs=yaml and further documentation on https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/file-matching-patterns?view=vsts which both give some great examples.
If you're unsure set the contents to **/* which will copy absolutely everything in the $(build.sourcesdirectory), but it will give you a feel for the shape of the directory structure so that you can change **/* into something more selective and scoped for the file(s) you want to copy.
The Source folder should be : Build.SourcesDirectory instead of '$(build.sourcesdirectory)'
This is from : https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#build-variables
I have three stages in my CI file, they all have only/except like this:
test:
only:
- tags
except:
- branches
script:
- npm run test
Seems redundant to have the only/except in three places. Is there a way to set this at the top level of the script config? Don't see anything like that in the docs.
You can use the map merging feature: https://docs.gitlab.com/ee/ci/yaml/#special-yaml-features
.job_template: &job_definition
only:
- tags
except:
- branches
test1:
<<: *job_definition
script:
- npm run test
test2:
<<: *job_definition
script:
- # ...