簡體   English   中英

所有測試用例在 junit 中通過但 PIT 失敗

[英]All test cases passed in junit but PIT is failing

在運行pit和jacoco時,所有測試用例都通過並且jacoco覆蓋率很好,但是PIT突變測試失敗並出現以下錯誤。

Build Failure
All tests did not pass without mutation when calculating line coverage. Mutation testing requires a green suite.

由於我所有的測試用例都通過了,我無法弄清楚為什么 PIT 測試失敗了。 運行 PIT 測試時,我在控制台中看不到任何測試失敗。

如果是這種情況,請建議如何查找 PIT 中哪個測試用例失敗,如果不是,請告訴我 PIT 測試失敗背后的原因。

當我將故障注入我的測試套件並運行 Pitest 時,它會清楚地向我顯示哪些測試失敗了。 即使沒有啟用“詳細”日志記錄。

我在我的pom.xml中配置了 Pitest,以便為我的測試添加一些配置以正常運行。 以下塊可能位於<pluginManagement>部分或<profile>中:

            <plugin>
                <groupId>org.pitest</groupId>
                <artifactId>pitest-maven</artifactId>
                <version>1.8.0</version>
                <dependencies>
                    <dependency>
                        <groupId>org.pitest</groupId>
                        <artifactId>pitest-junit5-plugin</artifactId>
                        <version>0.16</version>
                    </dependency>
                </dependencies>
            </plugin>

現在我可以在命令行上運行mvn test-compile pitest:mutationCoverage來運行我的(有故障的)測試套件,而無需進行常規的單元測試。

在 Pitest 的控制台輸出中,我可以看到哪些測試方法失敗:

22:54:05 PIT >> INFO : Verbose logging is disabled. If you encounter a problem,
please enable it before reporting an issue.
22:54:06 PIT >> INFO : Incremental analysis reduced number of mutations by 0
22:54:06 PIT >> INFO : Created  69 mutation test units in pre scan
22:54:07 PIT >> INFO : Sending 85 test classes to minion
22:54:07 PIT >> INFO : Sent tests to minion
|22:54:17 PIT >> SEVERE : Description [testClass=com.myproject.core.MyTestClass,
  name=[engine:junit-jupiter]/[class:com.myproject.core.MyTestClass]/[method:testBrokenOne()]]
  did not pass without mutation.
/22:54:17 PIT >> SEVERE : Description [testClass=com.myproject.core.MyTestClass,
  name=[engine:junit-jupiter]/[class:com.myproject.core.MyTestClass]/[method:testMethodTwo()]]
  did not pass without mutation.
|22:54:19 PIT >> INFO : Calculated coverage in 13 seconds.
22:54:19 PIT >> SEVERE : Tests failing without mutation:
Description [testClass=com.myproject.core.MyTestClass,
  name=[engine:junit-jupiter]/[class:com.myproject.core.MyTestClass]/[method:testMethodOne()]]
Description [testClass=com.myproject.core.MyTestClass,
  name=[engine:junit-jupiter]/[class:com.myproject.core.MyTestClass]/[method:testMethodTwo()]]

(為可讀性添加了換行符)

所以我看到MyTestClass.testMethodOne()MyTestClass.testMethodTwo()即使沒有突變也無法運行。

當我在命令行上添加-Dverbose=true時,我發現測試框架的輸出如下:

23:07:37 PIT >> INFO : MINION : org.opentest4j.AssertionFailedError: expected: <25> but was: <24>

如果需要,為了進一步縮小范圍,您可能希望將每個測試方法限制為單個assert語句。 您可以添加日志記錄(到控制台或文件)以檢查測試的變量。

原因可能是您的常規測試得到的參數(也許您在命令行或 pom 中指定了某些屬性?),但在 Pitest 啟動時卻沒有。 我自己最近才了解到,在某些情況下,Pitest 會產生新的 jvm 實例,這些實例需要通過<jvmArgs>獲取參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM