I have added the below steps to run unit test in Azure pipelines for React UI.
Added a file , File name:jestTrxProcessor.js. The content:
var builder = require("jest-trx-results-processor/dist/testResultsProcessor");
var builder = require("jest-trx-results-processor");
var processor = builder({
outputFile: "jestTestresults.trx",
});
module.exports = processor;
In package.json I entered the below code:
"scripts": {
....
"test": "jest"
},
devdependencies{
...
"jest": "^23.4.1",
"jest-trx-results-processor": "0.0.7",
"jsdom": "^11.12.0"
},
"jest": {
"testResultsProcessor": "./__tests__/jestTrxProcessor.js",
"reporters": [
"default",
[
"jest-trx-results-processor",
{
"outputFile": "./__tests__/jestTestresults.trx",
}
]]},
3.In the yaml file I added the below script:
- script: |
npm install
npm install jest-trx-results-processor --save-dev
yarn add --dev jest-trx-results-processor
npm run build
# npm run test
displayName: 'npm install and build'
- task: PublishTestResults#2
inputs:
testResultsFormat: 'VSTest'
testResultsFiles: './__tests__/jestTestresults.trx'
testRunTitle: 'FrontEnd Test'
I am getting the below error:
Failed to parse result files when VSTest is run Azure pipelines
According to the error message:
Publish Test Results Failed to parse result files:
System.Xml.XmlException: Data at the root level is invalid. Line 1,
position 1
It means that you have attempted to parse something that is not an XML document or there is a issue with the generated trx file.
To troubleshooting this issue, please try to update the devdependencies jest jest-trx-results-processor:
"devDependencies": {
"jest": "^26.6.3",
"jest-trx-results-processor": "~2.0.0"
},
And try to select JUnit in TestResultsFormat:
Besides, the Jest testResultsProcessor property is deprecated, please try to use the jest-junit package for test reports:
Please check this thread for some more details.
Related
I'm trying to cache the cypress installation in my build pipeline.
I have this task setup:
- task: Cache#2
inputs:
key: 'cypress | $(Agent.OS) | package-lock.json'
path: 'C:\npm\prefix\node_modules\cypress'
I've run the build pipeline multiple times but I'm always getting the same error:
There is a cache miss
This is the previous build:
As you can see it's the same fingerprint, so why is the caching not working?
One more thing - In order that cache task works as expected all task must succeed.
Set variable system.debug to true to get more information.
You would check the path to see whether it is correct on the agent machine (you are using self-hosted agent, correct?)
Usually, on the first run after the task is added, the cache step will report a "cache miss" since the cache identified by this key does not exist. As you always got "cache miss" issue, I suspect the cache is not created or uploaded correctly. You may try to do some modification for package-lock.json to recache and re-generated one new key, to see how's the result.
Check the "cache post-job task" results and see if the keys are different.
In my case I had to use npm install --no-save so that the package-lock.json file wasn't regenerated during the pipeline. This ensured the "cache post-job task" was using the same cache key when caching node_modules.
edit
Here is an exported example of what we currently use in our pipeline to cache npm modules.
(You must make sure that your package-lock.json is checked in to your code repository)
steps:
- task: Cache#2
displayName: 'Npm Install Cache'
inputs:
key: '"npm" | "$(Agent.OS)" | my-project/package-lock.json'
path: 'my-project/node_modules'
cacheHitVar: NpmInstallCached
- task: Npm#1
displayName: 'Npm Install'
inputs:
command: custom
workingDir: my-project/
verbose: false
customCommand: 'install --no-save'
condition: ne(variables['NpmInstallCached'], 'true')
I have some postman collections and I would like to run them in azure devops,
for whatever the reason my tests are not published.
What Am I missing?
If I remove "reporters: junit i can see the result in the step. I was expecting a tab "test" next to summary
resources:
- repo: self
clean: true
queue:
name: Hosted VS2017
demands: npm
steps:
- task: Npm#1
displayName: 'npm install'
inputs:
verbose: false
customCommand: 'install newman -g'
- task: NewmanPostman#4
displayName: 'Postman tests'
inputs:
collectionFileSource: 'my.postman_collection.json'
environmentSourceType: none
reporters: junit
ignoreRedirect: false
bail: false
sslInsecure: false
htmlExtraDarkTheme: false
htmlExtraLogs: false
htmlExtraTestPaging: false
- task: PublishTestResults#2
inputs:
testResultsFormat: 'JUnit' # Options: JUnit, NUnit, VSTest, xUnit
testResultsFiles: '**/*.xml'
What Am I missing? If I remove "reporters: junit i can see the result in the step. I was expecting a tab "test" next to summary
Based on my test, I could reproduce your issue.
Here are the following points, you need to check:
The XML file path in Publish Test Result task.
By default, the Junit xml file will be saved at newman folder.
You could set the file path with the following path:
- task: PublishTestResults#2
displayName: 'Publish Test Results **/*/*.xml'
inputs:
testResultsFiles: '**/*/*.xml'
You can also change the Junit file output path: reporterJUnitExport field in NewmanPostman task
Example:
- task: NewmanPostman#4
displayName: 'Newman - Postman'
inputs:
collectionFileSource: '$(build.sourcesdirectory)'
Contents: 'kevintest123.postman_collection.json'
environmentSourceType: none
ignoreRedirect: false
bail: false
sslInsecure: false
reporters: junit
htmlExtraDarkTheme: false
htmlExtraLogs: false
htmlExtraTestPaging: false
reporterJUnitExport: '$(Build.sourcesdirectory)\Results\junitReport.xml'
You need to make sure that the Json file contain the test. When you Export the files in Postman, you need to add the content in test tab:
Here is a doc about Add test in Postman.
Then you could publish the test result to the test tab successfully via Publish test Restlt task.
Or you will get the following issue and the test result Junit xml file doesn't contain the test result:
Update:
Cannot find "continue on error" anywhere.anyideas
Yaml Editor
- task: PublishTestResults#2
displayName: 'Publish Test Results **/*/*.xml'
inputs:
testResultsFiles: '**/*/*.xml'
continueOnError: true
Classic editor
Junit report is generated in the newman folder in the current directory. You change this location as:
--reporter-junit-export a.xml
--reporter-junit-export folder/a.xml
this will generate report as a.xml or a.xml in the folder/directory you specified.
you have to publish that using publishtest result , if you are not using --reporter-junit-export , then make sure workingdirectory for all the steps are same.
else give full path as :
--reporter-junit-export c:/folder/a.xml
and
in step give the full path of report:
- task: PublishTestResults#2
inputs:
testResultsFormat: 'JUnit' # Options: JUnit, NUnit, VSTest, xUnit
testResultsFiles: 'c:/folder/a.xml'
I put a shell script file in a folder on my repo root and tried to run that in my devops pipeline but it says that cannot find the scriptPath:
[error]Not found scriptPath: /home/vsts/work/1/s/pipelines/databricks-cli-config.sh
I am simply creating a task to run the shell script, like this:
- task: ShellScript#2
inputs:
scriptPath: 'pipelines/databricks-cli-config.sh'
args: '$(databricks_host) $(databricks_token)'
displayName: "Install and configure the Databricks CLI"
Any idea?
Make sure you checkout your code and you are on correct level. So if you are on regular job please add working directory:
- task: ShellScript#2
inputs:
scriptPath: 'pipelines/databricks-cli-config.sh'
args: '$(databricks_host) $(databricks_token)'
cwd: '$(System.DefaultWorkingDirectory)'
displayName: "Install and configure the Databricks CLI"
and if you use it on deployment job, by default code is not being checked out there. So you need you need to publish this script as artifact and then download it in deployment job (deployment jobs download artifact by default) or add
- checkout: self
step do download code on deployment job.
I assumed that you use YAML.
In my Azure DevOps build task I run a Cypress test. If the test fails the build is canceled. But I want to run another task after Cypress the publishes the test results.
I've tried it with this task in my pipeline.yml file:
- task: PowerShell#2
inputs:
targetType: "inline"
script: "yarn test:cypress"
errorActionPreference: "continue"
displayName: "start server and run cypress"
But this doesn't seem to have any effect.
I've tried add -ErrorAction 'Continue' to to script"
"start": "npm-run-all -s build:shared-web run:shell",
"cy:run": "cypress run -ErrorAction 'Continue'",
"test:cypress": "start-server-and-test start http://localhost:3000 cy:run"
But this fails with:
error: unknown option: -E
It looks like Cypress is seeing the ErrorAction as a Cypress parameter.
So what's the correct way of continuing a build if a task fails?
You can add to the task this:
continueOnError: true
Now the build will keep running even the tests will fail.
If you want to fail the build if the tests failed but you want to run only one task to publish the results, you can add this to the publish tests task:
condition: always()
Now the publish task will be running always, even the tests are failed and the build canceled/failed.
Added a Publish test results task in Azure DevOpsCI/CD pipeline, test were successfull, but after running the test it complaints about ##[warning]No test result files matching **/test-*.xml were found. Could someone please advise on how can we resolve similar problem ?
Publish Test Results task : configuration
Test result format= JUnit
Test results files= **/test-*.xml
Search folder = $(System.DefaultWorkingDirectory)
Test results title = Cypress Test Results
note: I have try adding the search folder path as follows: C:\agent_work\r5\a\drop\ui-tests\cypress
package.json to run the tests
"scripts": {
"test": "cypress run --record --key <key value here>"
}
My directory path in server:
C:\agent_work\r5\a\drop\ui-tests\cypress
My friend, I was facing the same issue on Azure DevOps.
In my case, the folder where the xml files were generated was reports on the root of the repo, that depends on how you got configured Junit on your cypress.json file
So In my case, the solution was changing this on azure-pipelines.yml
testResultsFiles: "results/*.xml"
searchFolder: $(System.DefaultWorkingDirectory)
So that's the entire setup of the testing pipeline
# Node.js
# Build a general Node.js project with npm.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/javascript
trigger:
- master
pool:
vmImage: 'ubuntu-latest'
steps:
- task: NodeTool#0
inputs:
versionSpec: '12.x'
displayName: 'Install Node.js'
- script: "npm i"
displayName: "Install project dependencies"
- script: "npm run cy:verify"
displayName: "Cypress Verify"
- script: "source cypress.env" # comment this script to run tests against production
displayName: "Using env variables to change url to test against development branch"
- script: "npm run cy:run-report"
displayName: "Run Cypress Tests"
- task: PublishBuildArtifacts#1
displayName: "Publish Artifact: cypress-azure-devops screenshots"
inputs:
PathtoPublish: cypress/screenshots
ArtifactName: "CypressAzureDevopsTestRunScreenshots"
condition: failed()
- task: PublishTestResults#2
displayName: "Publish Test Results"
condition: succeededOrFailed()
inputs:
testResultsFormat: "JUnit"
testResultsFiles: "results/*.xml"
searchFolder: $(System.DefaultWorkingDirectory)
mergeTestResults: true
testRunTitle: 'Test Results'
continueOnError: true
Saludos desde Argentina 🇦🇷