我用我的代碼測試cobertura並生成一個簡單的報告。理解那裏所示的數字有點問題。Cobertura如何識別代碼中的分支?
只有一種類和內容被示出爲在圖像中。 我寫測試是如下,
public class AppTest {
@Before
public void before() {
}
@Test
public void addShouldReturnTheSumOfTwoIntegersWhenFirstIsLessThan100() {
Assert.assertTrue(65 == App.add(20, 45));
}
@Test
public void addShouldReturnMinusOneWhenFirstIsGreaterThan200() {
Assert.assertTrue(-1 == App.add(250, 45));
}
}
作爲圖像中示出有4
分支。有人可以解釋一下這個數字是多少4
?
UPDATE:
當我只有下面的測試案例,
@Test
public void addShouldReturnMinusOneWhenFirstIsGreaterThan200() {
Assert.assertTrue(-1 == App.add(250, 45));
}
輸出,
當我擁有所有3次測試,
@Test
public void addShouldReturnTheSumOfTwoIntegersWhenFirstIsLessThan100() {
Assert.assertTrue(65 == App.add(20, 45));
}
@Test
public void addShouldReturnMinusOneWhenFirstIsGreaterThan200() {
Assert.assertTrue(-1 == App.add(250, 45));
}
@Test
public void addShouldReturn200WhenFirstIsGreaterThan100AndLessThan200() {
Assert.assertTrue(200 == App.add(120, 45));
}
輸出,
當我有以下測試用例return 200
分支,
@Test
public void addShouldReturn200WhenFirstIsGreaterThan100AndLessThan200() {
Assert.assertTrue(200 == App.add(120, 45));
}
輸出,
更新2:
如果我有一個測試用例來檢查第一if
條款,
@Test
public void addShouldReturnTheSumOfTwoIntegersWhenFirstIsLessThan100() {
Assert.assertTrue(65 == App.add(20, 45));
}
輸出,
我認爲,隱藏分支未涉及的僅此測試用例。但下面的其他分支也會覆蓋這個神祕的分支。
UPDATE 3:
使用javap -c App.class
後,
Compiled from "App.java"
public class com.vnb.play_cobertura.play_cobertura.App {
public com.vnb.play_cobertura.play_cobertura.App();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":
()V
4: return
public static int add(int, int);
Code:
0: iload_0
1: bipush 100
3: if_icmpge 10
6: iload_0
7: iload_1
8: iadd
9: ireturn
10: iload_0
11: sipush 200
14: if_icmpge 21
17: sipush 200
20: ireturn
21: iconst_m1
22: ireturn
}
德編譯的輸出,
package com.vnb.play_cobertura.play_cobertura;
public class App
{
public App() {}
public static int add(int a, int b)
{
if (a < 100)
return a + b;
if (a < 200) {
return 200;
}
return -1;
}
}
嘗試增加App.add(120,45)的'測試'與預期輸出。只是一個猜測,但我想你會得到'6/6'。 –
@JimGarrison我做到了,然後它會是4/4。到目前爲止,我可以確定3個分支。我爲那些人寫了三個問題,問題是另一個問題是什麼?和其他人一樣,無論如何都是如此。 – prime
顯然你沒有測試'return 200'分支。如果你測試了所有三個,那麼請顯示實際代碼和Cobertura輸出。 –