javascript - Import Angular unit test report into Sonarqube - Stack Overflow

admin2025-04-22  0

Context

In an Angular5 project using karma and jasmine for unit testing, I am generating this report file for my unit tests using karma-sonarqube-unit-reporter.

<testExecutions version="1">
  <file path="src/app/appponent.spec.ts">
    <testCase name="app/appponent.spec.ts should create the app" duration="314"/>
    <testCase name="app/appponent.spec.ts should have as title 'Projet de test Angular5'" duration="104"/>
    <testCase name="src/app/appponent.spec.ts should render title in a h1 tag" duration="114"/>
  </file>
  <file path="src/app/client-add/client-addponent.spec.ts">
    <testCase name="client-add/client-addponent.spec.ts should create" duration="107"/>
  </file>
  <file path="app/client.service.spec">
    <testCase name="app/client.service.spec #getClients should return an Observable&lt;Client[]>" duration="38"/>
  </file>
  <file path="src/app/message.service">
    <testCase name="app/message.service should be created" duration="35"/>
  </file>
</testExecutions>

But when I import it into Sonarqube (v6.7) (via Jenkins), I get the following

INFO: Sensor Generic Test Executions Report
WARN: Property 'sonar.genericcoverage.unitTestReportPaths' is deprecated. Please use 'sonar.testExecutionReportPaths' instead.
INFO: Parsing C:\Users\adminpp\.jenkins\workspace\ELIS JustElis Beta WEB\reports\ut_report.xml
INFO: Imported test execution data for 0 files
INFO: Test execution data ignored for 4 unknown files, including:
src/app/appponent.spec.ts
src/app/client-add/client-addponent.spec.ts
src/app/client.service.spec
src/app/message.service
INFO: Sensor Generic Test Executions Report (done) | time=110ms

Here are my sonar properties for excluding and including sources and test codes.

sonar.sourceEncoding=UTF-8
sonar.sources=.
sonar.exclusions=**/node_modules/** 
sonar.test.inclusions = **/*.spec.ts
sonar.import_unkown_files=true
sonar.typescript.lcov.reportPaths=coverage/lcov.info
sonar.testExecutionReportPaths=reports/ut_report.xml

I checked and I have my *.spec.ts files where they are supposed to be.

Questions

Why Sonarqube doesn't find my files?

Have I missed a property or an important step?

(Is this property sonar.test.inclusions = **/*.spec.ts important?)

Context

In an Angular5 project using karma and jasmine for unit testing, I am generating this report file for my unit tests using karma-sonarqube-unit-reporter.

<testExecutions version="1">
  <file path="src/app/app.ponent.spec.ts">
    <testCase name="app/app.ponent.spec.ts should create the app" duration="314"/>
    <testCase name="app/app.ponent.spec.ts should have as title 'Projet de test Angular5'" duration="104"/>
    <testCase name="src/app/app.ponent.spec.ts should render title in a h1 tag" duration="114"/>
  </file>
  <file path="src/app/client-add/client-add.ponent.spec.ts">
    <testCase name="client-add/client-add.ponent.spec.ts should create" duration="107"/>
  </file>
  <file path="app/client.service.spec">
    <testCase name="app/client.service.spec #getClients should return an Observable&lt;Client[]>" duration="38"/>
  </file>
  <file path="src/app/message.service">
    <testCase name="app/message.service should be created" duration="35"/>
  </file>
</testExecutions>

But when I import it into Sonarqube (v6.7) (via Jenkins), I get the following

INFO: Sensor Generic Test Executions Report
WARN: Property 'sonar.genericcoverage.unitTestReportPaths' is deprecated. Please use 'sonar.testExecutionReportPaths' instead.
INFO: Parsing C:\Users\adminpp\.jenkins\workspace\ELIS JustElis Beta WEB\reports\ut_report.xml
INFO: Imported test execution data for 0 files
INFO: Test execution data ignored for 4 unknown files, including:
src/app/app.ponent.spec.ts
src/app/client-add/client-add.ponent.spec.ts
src/app/client.service.spec
src/app/message.service
INFO: Sensor Generic Test Executions Report (done) | time=110ms

Here are my sonar properties for excluding and including sources and test codes.

sonar.sourceEncoding=UTF-8
sonar.sources=.
sonar.exclusions=**/node_modules/** 
sonar.test.inclusions = **/*.spec.ts
sonar.import_unkown_files=true
sonar.typescript.lcov.reportPaths=coverage/lcov.info
sonar.testExecutionReportPaths=reports/ut_report.xml

I checked and I have my *.spec.ts files where they are supposed to be.

Questions

Why Sonarqube doesn't find my files?

Have I missed a property or an important step?

(Is this property sonar.test.inclusions = **/*.spec.ts important?)

Share Improve this question edited Mar 28, 2018 at 16:14 G. Ann - SonarSource Team 22.9k4 gold badges46 silver badges92 bronze badges asked Mar 28, 2018 at 15:58 A. GorinA. Gorin 871 silver badge10 bronze badges 3
  • 1 can you try to add property sonar.tests=. (value set to current directory) – Tibor Blenessy Commented Mar 28, 2018 at 17:08
  • @TiborBlenessy I added this property but I got ERROR: Error during SonarQube Scanner execution ERROR: File .angular-cli.json can't be indexed twice. Please check that inclusion/exclusion patterns produce disjoint sets for main and test files Maybe because I added sonar.tests=. AND sonar.sources=. which makes Sonar index files twice (one for test and one for source)? – A. Gorin Commented Mar 29, 2018 at 7:14
  • @TiborBlenessy My bad, I deleted this property sonar.test.inclusions=**/*.spec.ts since I thought it would not be necessary anymore and that's what gave me an error. Now that I have these two properties, sonar finds my files. Do you know how these two are related and why i need both? – A. Gorin Commented Mar 29, 2018 at 7:29
Add a ment  | 

2 Answers 2

Reset to default 4

can you try to add property sonar.tests=. (value set to current directory) – Tibor Blenessy

Adding this property in the sonar config worked for me.

I got an error when I had the property sonar.tests set but not sonar.test.inclusions though but with the two, everything works fine.

I fix it with adding prefix of absolute path and hope it can help you.

sonarQubeUnitReporter: {
      sonarQubeVersion: "9.x",
      outputFile: "./coverage/unit-test-result.xml",
      overrideTestDescription: true,
      testFilePattern: ".spec.ts",
      useBrowserName: false,
      prependTestFileName: __dirname
  }

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745276682a293913.html

最新回复(0)