如何知道我 did/didn 在 JUnit 测试结束时没有执行哪些指令
How to know what instructions I did/didn't execute at the end of my test in JUnit
我是一名不错的 java 开发人员,但我对测试框架知之甚少。我刚刚创建了一个简单的方法:
public static String signOf(String str) {//expects a number as String and gives you the sing of it (positive or negative)
int number = 0;
str = str.trim();
try {
number = Integer.parseInt(str);
} catch (Exception e) {
return "NaN";
}
if (str.equals("0")) {
return "positive and negative";
}
if(str.length()==count(number)){
return "positive";
}
if(str.length()==(count(number)+1)){
if (str.charAt(0) == '+') {
return "positive";
}
if (str.charAt(0) == '-') {
return "negative";
}
}
return "NaN" ;
}
为了测试它,我创建了另一个方法(我使用 IntelliJ 作为 IDE):
@Test
public void testSignOf(){
assertEquals("positive and negative",signOf("0"),"0 is positive and negative at the same time.");
assertEquals("positive",signOf("19"),"19 is positive.");
assertEquals("negative",signOf("-0"),"-0 is negative.");
assertEquals("positive",signOf("+0"),"+0 is positive.");
assertEquals("negative",signOf("-12"),"-12 is negative.");
assertEquals("positive",signOf("+23"),"+23 is positive.");
assertEquals("NaN",signOf("1-1"),"1-1 is NaN.");
assertEquals("NaN",signOf("ad"),"ad is NaN.");
assertEquals("NaN",signOf("-"),"- is NaN.");
assertEquals("NaN",signOf("+"),"+ is NaN.");
assertEquals("NaN",signOf("+-"),"+- is NaN.");
assertEquals("NaN",signOf("--1"),"--1 is NaN.");
}
有没有办法知道我的测试是否输入了我的代码的每条指令和每一种可能的情况。通常在测试结束时,如果一切按预期进行,它会变成绿色。但是,如果您没有或确实在您测试的方法中访问 if 语句后的特定指令,它不会通知您。
这个测试让我想起了这个深刻的想法:
What I know is little.
What I know that I don't know is big.
But what I don't know that I don't know is A lot bigger.
其余代码:
public static int count(int num){
if (num==0) return 1;//Btw the test helped me to add this if
int count = 0;
while (num != 0) {
// num = num/10
num /= 10;
++count;
}
return count ;
}
如果您想知道执行了哪些代码行,这就是代码覆盖工具的用途。 Intelliij 自带一个代码覆盖插件,你可以 运行 你的代码覆盖测试,它会告诉你哪些行被覆盖,哪些没有。参见 https://www.jetbrains.com/help/idea/running-test-with-coverage.html
我是一名不错的 java 开发人员,但我对测试框架知之甚少。我刚刚创建了一个简单的方法:
public static String signOf(String str) {//expects a number as String and gives you the sing of it (positive or negative)
int number = 0;
str = str.trim();
try {
number = Integer.parseInt(str);
} catch (Exception e) {
return "NaN";
}
if (str.equals("0")) {
return "positive and negative";
}
if(str.length()==count(number)){
return "positive";
}
if(str.length()==(count(number)+1)){
if (str.charAt(0) == '+') {
return "positive";
}
if (str.charAt(0) == '-') {
return "negative";
}
}
return "NaN" ;
}
为了测试它,我创建了另一个方法(我使用 IntelliJ 作为 IDE):
@Test
public void testSignOf(){
assertEquals("positive and negative",signOf("0"),"0 is positive and negative at the same time.");
assertEquals("positive",signOf("19"),"19 is positive.");
assertEquals("negative",signOf("-0"),"-0 is negative.");
assertEquals("positive",signOf("+0"),"+0 is positive.");
assertEquals("negative",signOf("-12"),"-12 is negative.");
assertEquals("positive",signOf("+23"),"+23 is positive.");
assertEquals("NaN",signOf("1-1"),"1-1 is NaN.");
assertEquals("NaN",signOf("ad"),"ad is NaN.");
assertEquals("NaN",signOf("-"),"- is NaN.");
assertEquals("NaN",signOf("+"),"+ is NaN.");
assertEquals("NaN",signOf("+-"),"+- is NaN.");
assertEquals("NaN",signOf("--1"),"--1 is NaN.");
}
有没有办法知道我的测试是否输入了我的代码的每条指令和每一种可能的情况。通常在测试结束时,如果一切按预期进行,它会变成绿色。但是,如果您没有或确实在您测试的方法中访问 if 语句后的特定指令,它不会通知您。
这个测试让我想起了这个深刻的想法:
What I know is little.
What I know that I don't know is big.
But what I don't know that I don't know is A lot bigger.
其余代码:
public static int count(int num){
if (num==0) return 1;//Btw the test helped me to add this if
int count = 0;
while (num != 0) {
// num = num/10
num /= 10;
++count;
}
return count ;
}
如果您想知道执行了哪些代码行,这就是代码覆盖工具的用途。 Intelliij 自带一个代码覆盖插件,你可以 运行 你的代码覆盖测试,它会告诉你哪些行被覆盖,哪些没有。参见 https://www.jetbrains.com/help/idea/running-test-with-coverage.html