如何同时对多个函数进行junit测试
How to do junit testing for multiple functions together
我正在为一个 java 应用程序编写 Junit 测试用例。
如果我 运行 测试功能单独 运行ning 没问题。
当我尝试 运行 它们在一起时,它 运行 不正确
这是 JUnit 代码
public class CultureMachineTestCases{
CultureMachineAssignment testObj=new CultureMachineAssignment ();
HashSet<String> result =new HashSet<String>();
String answer,answer1;
int flagVal;
@Before
public void init() throws IOException{
testObj.insertDataIntoSet();
testObj.addKeywords("video1");
}
@Test
public void testVideo() throws IOException {
result=testObj.search("abcd");
answer=result.toString();
answer1=answer.replaceAll("[^a-z0-9]","");
assertEquals("video1", answer1);
}
@Before
public void initMethod() throws IOException{
testObj.insertDataIntoSet();
testObj.addKeywords("video2");
}
@Test
public void testLenth() throws IOException{
flagVal=testObj.flag;
assertEquals(1, flagVal);
}
}
此处标志在 CultureMachineAssignment 文件中设置为 1。
谁能告诉我我需要做什么,这样我才能 运行 测试文件中的所有函数一起
@Before 注解方法 (init()) 在每个测试方法之前被调用。您应该只有一种方法用 @Before 注释,以免混淆。
您在 init() 中实现的代码应移至 testVideo(),initMethod() 中的代码应移至 testLength()。
并且您的初始化方法应该如下所示,以确保测试 class 状态对于所有测试都是相同的:
@Before
public void init(){
answer = null;
answer1 = null;
flagVal = -1;
result = new HashSet<String>();
}
我正在为一个 java 应用程序编写 Junit 测试用例。 如果我 运行 测试功能单独 运行ning 没问题。 当我尝试 运行 它们在一起时,它 运行 不正确
这是 JUnit 代码
public class CultureMachineTestCases{
CultureMachineAssignment testObj=new CultureMachineAssignment ();
HashSet<String> result =new HashSet<String>();
String answer,answer1;
int flagVal;
@Before
public void init() throws IOException{
testObj.insertDataIntoSet();
testObj.addKeywords("video1");
}
@Test
public void testVideo() throws IOException {
result=testObj.search("abcd");
answer=result.toString();
answer1=answer.replaceAll("[^a-z0-9]","");
assertEquals("video1", answer1);
}
@Before
public void initMethod() throws IOException{
testObj.insertDataIntoSet();
testObj.addKeywords("video2");
}
@Test
public void testLenth() throws IOException{
flagVal=testObj.flag;
assertEquals(1, flagVal);
}
}
此处标志在 CultureMachineAssignment 文件中设置为 1。 谁能告诉我我需要做什么,这样我才能 运行 测试文件中的所有函数一起
@Before 注解方法 (init()) 在每个测试方法之前被调用。您应该只有一种方法用 @Before 注释,以免混淆。
您在 init() 中实现的代码应移至 testVideo(),initMethod() 中的代码应移至 testLength()。
并且您的初始化方法应该如下所示,以确保测试 class 状态对于所有测试都是相同的:
@Before
public void init(){
answer = null;
answer1 = null;
flagVal = -1;
result = new HashSet<String>();
}