-
-
Save black-silence/35b958fe92c704de551a3ca4ea082b87 to your computer and use it in GitHub Desktop.
#!/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') |
Also linking https://community.sonarsource.com/t/sonarphp-doesnt-analyze-php-unit-tests-with-dataprovider/2775/5 for context for those who come across this gist.
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.
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))
Thanks, worked for me with phpunit dataprovider ! :)
@macghriogair does this result in importing coverage for tests using data providers, or are coverage metrics for those tests just discarded?
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.