Reputation: 1
I’m trying to run a package with many unit tests (one by one, not as a class) using reflection, So when I get all the @Test methods that needs to be run I try to do
Result result = new JUnitCore().run(Request.method(Class
.forName(packageAndClass),getTestName()));
But the class returned in packageAndClass has @Before, @BeforeClass methods (that also might be in its superclass)
So when running the code above I get all the tests running and fail(because some of their values are assigned in the @Before and @BeforeClass methods) But when running it from eclipse (select the test method name -> right click -> run as -> Junit test) They all pass (runing together or one by one) Is there an api of Request that will run the before methods?
Upvotes: 0
Views: 319
Reputation: 29520
I ran the following test with junit 4.9 :
public class RunOneTest {
public static void main(final String[] args) {
final Result result = new JUnitCore().run(Request.method(RunOneTest.class, "oneTest"));
System.out.println("result " + result.wasSuccessful());
}
@Test
public void oneTest() throws Exception {
System.out.println("oneTest");
}
@Test
public void anotherTest() throws Exception {
System.out.println("anotherTest");
}
@Before
public void before() {
System.out.println("before");
}
@BeforeClass
public static void beforeClass() {
System.out.println("beforeClass");
}
@After
public void after() {
System.out.println("after");
}
@AfterClass
public static void afterClass() {
System.out.println("afterClass");
}
}
and the output was :
beforeClass
before
oneTest
after
afterClass
result true
Are you really sure that the methods are not run ?
Upvotes: 0
Reputation: 12165
Why are you doing that? JUnit is supposed to run the tests for you!
Upvotes: 3