我有3个测试类如下:
package com.junittest.testcases;
Test3课程:
import junit.framework.TestCase;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import annotations.DataSetRule;
import annotations.DataSets;
import com.junittest.test.MyClass;
public class Test3 extends TestCase{
MyClass m = new MyClass();
String group = "user";
@Rule
public TestRule dataSetRule = new DataSetRule();
@Test
public void test5() {
System.out.println("In Test 5");
//assertEquals(5, m.add(2,3));
}
@Test
public void test6() throws Exception {
org.junit.Assume.assumeTrue(group.equals("user"));
System.out.println("In Test 6");
//assertEquals(-2, m.sub(2,3));
}
@DataSets({"dataset","dataset1"})
@Test
public void test7() {
System.out.println("In Test 7");
}
@Ignore
@Test
public void test8() {
System.out.println("in test8");
}
}
Test2课程:
package com.junittest.testcases;
import org.junit.Test;
import singletons.SingletonClass;
import com.junittest.test.MyClass;
import junit.framework.TestCase;
public class Test2 extends TestCase{
MyClass m = new MyClass();
public void setUp() {
SingletonClass sc = SingletonClass.getInstance();
System.out.println(SingletonClass.callCount);
}
@Test
public void test3() {
assertEquals(5, m.add(2,3));
}
@Test
public void test4() {
assertEquals(-2, m.sub(2,3));
}
}
Test1课程:
package com.junittest.testcases;
import org.junit.Test;
import singletons.SingletonClass;
import com.junittest.test.MyClass;
import junit.framework.TestCase;
public class Test1 extends TestCase{
MyClass m = new MyClass();
public void setUp() {
SingletonClass sc = SingletonClass.getInstance();
System.out.println(SingletonClass.callCount);
}
@Test
public void test1() {
assertEquals(4, m.add(2,3));
}
@Test
public void test2() {
assertEquals(-1, m.sub(2,3));
}
}
还有一个TestSuite类:
package com.junittest.testcases;
import rules.SomeTest;
import singletons.SingletonClass;
import junit.framework.Test;
import junit.framework.TestSuite;
public class TestSuite1 {
public static Test suite() {
SingletonClass sc = SingletonClass.getInstance();
System.out.println(SingletonClass.callCount);
TestSuite suite = new TestSuite(TestSuite.class.getName());
suite.addTestSuite(Test1.class);
suite.addTestSuite(Test2.class);
suite.addTestSuite(Test3.class);
return suite;
}
}
问题是它在从Eclipse运行来自JUnit的TestSuite1类时忽略了Test3类中的@Rule标记,因此输出不是预期的。为什么忽略@Rule标记。有没有办法执行所有这三种情况?
答案 0 :(得分:5)
您正在混合使用JUnit 3(junit.framework.*
)和JUnit 4(org.junit.*
)代码。如果您仅使用JUnit 4,问题应该会消失。
extends TestCase
(不需要它们,因为您的测试使用@Test
注释。@Before
注释添加到setUp()
方法。