在学习使用 Evosuite 的时候,我写了一些错误的方法。例如其中一个类的方法A
直接调用 interface 的抽象方法B
,EvoSuite 生成一个 mockB
来避免 case 失败:
import org.junit.Test;
import static org.junit.Assert.*;
import static org.evosuite.shaded.org.mockito.Mockito.*;
import MockTest.A;
import MockTest.B;
import org.evosuite.runtime.EvoRunner;
import org.evosuite.runtime.EvoRunnerParameters;
import org.evosuite.runtime.ViolatedAssumptionAnswer;
import org.junit.runner.RunWith;
@RunWith(EvoRunner.class) @EvoRunnerParameters(useVNET = true, separateClassLoader = true, useJEE = true)
public class A_ESTest extends A_ESTest_scaffolding {
@Test(timeout = 4000)
public void test0() throws Throwable {
A a0 = new A();
B b0 = mock(B.class, new ViolatedAssumptionAnswer());
doReturn((String) null).when(b0).get(anyInt());
String string0 = a0.test(b0);
assertNull(string0);
}
}
对于可能发生被零除的方法,EvoSuite 生成的测试用例甚至会捕获异常以确保测试用例通过:
import org.junit.Test;
import static org.junit.Assert.*;
import static org.evosuite.runtime.EvoAssertions.*;
import org.evosuite.runtime.EvoRunner;
import org.evosuite.runtime.EvoRunnerParameters;
import org.junit.runner.RunWith;
import test.Case2;
@RunWith(EvoRunner.class) @EvoRunnerParameters(useVNET = true, separateClassLoader = true, useJEE = true)
public class Case2_ESTest extends Case2_ESTest_scaffolding {
@Test(timeout = 4000)
public void test8() throws Throwable {
Case2 case2_0 = new Case2();
// Undeclared exception!
try {
case2_0.add(6, 0);
fail("Expecting exception: ArithmeticException");
} catch(ArithmeticException e) {
//
// / by zero
//
verifyException("test.Case2", e);
}
}
}
EvoSuite 自动生成的测试用例是否总是通过,就像这些示例一样?如何让 EvoSuite 生成可能失败的测试用例,从而让开发人员直接使用?