Skip to content

Instantly share code, notes, and snippets.

@black-silence
Created April 10, 2019 13:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save black-silence/35b958fe92c704de551a3ca4ea082b87 to your computer and use it in GitHub Desktop.
Save black-silence/35b958fe92c704de551a3ca4ea082b87 to your computer and use it in GitHub Desktop.
quick-dirty phpunit xml result patcher so sonarphp doesn't choke
#!/usr/bin/env python3
import xml.etree.ElementTree as ET
et = ET.parse('logfile.xml')
root = et.getroot()
for mastersuites in root:
for suite in mastersuites:
if not "file" in suite.attrib:
continue
filename = suite.attrib['file']
for subsuite in suite:
if subsuite.tag != "testsuite":
continue
if not "file" in subsuite.attrib:
subsuite.attrib['file'] = filename
et.write('logfile.xml')
@kostajh
Copy link

kostajh commented May 8, 2019

Thanks for writing this! It sort of works, but I get a different error now, that SonarQube can't assign the same metric to the same file.

@kostajh
Copy link

kostajh commented May 8, 2019

@kostajh
Copy link

kostajh commented May 9, 2019

With this gist, I get the following error with sonar-scanner:

java.lang.UnsupportedOperationException: Can not add the same measure twice on tests/phpunit/MentorTest.php: DefaultMeasure[component=tests/phpunit/MentorTest.php,metric=Metric[id=<null>,key=skipped_tests,description=Number of skipped unit tests,type=INT,direction=-1,domain=Coverage,name=Skipped Unit Tests,qualitative=true,userManaged=false,enabled=true,worstValue=<null>,bestValue=0.0,optimizedBestValue=true,hidden=false,deleteHistoricalData=false,decimalScale=<null>],value=0,fromCore=false]
	at org.sonar.scanner.sensor.DefaultSensorStorage.saveMeasure(DefaultSensorStorage.java:172)
	at org.sonar.scanner.sensor.DefaultSensorStorage.store(DefaultSensorStorage.java:132)
	at org.sonar.api.batch.sensor.measure.internal.DefaultMeasure.doSave(DefaultMeasure.java:95)
	at org.sonar.api.batch.sensor.internal.DefaultStorable.save(DefaultStorable.java:45)
	at org.sonar.plugins.php.phpunit.TestFileReport.saveTestMeasures(TestFileReport.java:58)
	at org.sonar.plugins.php.phpunit.TestResultImporter.importReport(TestResultImporter.java:39)
	at org.sonar.plugins.php.phpunit.SingleFileReportImporter.lambda$importReport$0(SingleFileReportImporter.java:54)
	at java.util.Optional.ifPresent(Optional.java:159)
	at org.sonar.plugins.php.phpunit.SingleFileReportImporter.importReport(SingleFileReportImporter.java:52)
	at org.sonar.plugins.php.phpunit.SingleFileReportImporter.importReport(SingleFileReportImporter.java:44)
	at org.sonar.plugins.php.PHPSensor.processTestsAndCoverage(PHPSensor.java:136)
	at org.sonar.plugins.php.PHPSensor.execute(PHPSensor.java:124)
	at org.sonar.scanner.sensor.AbstractSensorWrapper.analyse(AbstractSensorWrapper.java:48)
	at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:85)
	at org.sonar.scanner.sensor.ModuleSensorsExecutor.lambda$execute$1(ModuleSensorsExecutor.java:59)
	at org.sonar.scanner.sensor.ModuleSensorsExecutor.withModuleStrategy(ModuleSensorsExecutor.java:77)
	at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:59)
	at org.sonar.scanner.scan.ModuleScanContainer.doAfterStart(ModuleScanContainer.java:82)
	at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:136)
	at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:122)
	at org.sonar.scanner.scan.ProjectScanContainer.scan(ProjectScanContainer.java:360)
	at org.sonar.scanner.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:355)
	at org.sonar.scanner.scan.ProjectScanContainer.doAfterStart(ProjectScanContainer.java:318)
	at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:136)
	at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:122)
	at org.sonar.scanner.bootstrap.GlobalContainer.doAfterStart(GlobalContainer.java:131)
	at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:136)
	at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:122)
	at org.sonar.batch.bootstrapper.Batch.doExecute(Batch.java:73)
	at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:67)
	at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:46)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:60)
	at com.sun.proxy.$Proxy0.execute(Unknown Source)
	at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(EmbeddedScanner.java:185)
	at org.sonarsource.scanner.api.EmbeddedScanner.execute(EmbeddedScanner.java:137)
	at org.sonarsource.scanner.cli.Main.execute(Main.java:111)
	at org.sonarsource.scanner.cli.Main.execute(Main.java:75)
	at org.sonarsource.scanner.cli.Main.main(Main.java:61

The relevant part of the junit.xml file:

    <testsuite assertions="22" errors="0" failures="0" file="/workspace/src/extensions/GrowthExperiments/tests/phpunit/MentorTest.php" name="GrowthExperiments\Tests\MentorTest" skipped="0" tests="13" time="16.141278">
      <testsuite assertions="10" errors="0" failures="0" file="/workspace/src/extensions/GrowthExperiments/tests/phpunit/MentorTest.php" name="GrowthExperiments\Tests\MentorTest::testNewFromMenteeInvalidMentorList" skipped="0" tests="5" time="0.737262">

where testNewFromMenteeInvalidMentorList is using a data provider.

@macghriogair
Copy link

Hello @kostajh,

I adapted this script slightly to move the testcase nodes into the parent suite instead. This fixed the Sonarqube Exception "Can not add the same measure twice" for me.
Maybe it helps in your case too. See https://gist.github.com/macghriogair/4976b8e6ea6d20a61cdeb95effb73364

(It is actually the same fix suggested already and implemented here: Codeception/Codeception#5004 (comment))

@Whax
Copy link

Whax commented Aug 24, 2019

Thanks, worked for me with phpunit dataprovider ! :)

@kostajh
Copy link

kostajh commented Aug 27, 2019

@macghriogair does this result in importing coverage for tests using data providers, or are coverage metrics for those tests just discarded?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment