Wujek Srujek
2010-02-20 15:44:44 UTC
Hi Cedril,
I have a test hierarchy:
public class AbstractTest {
@BeforeClass
public void beforeAbstractTestClass() {
System.out.println("<<<<<>>>>>
AbstractTest.beforeAbstractTestClass() for class " + getClass().getName());
throw new RuntimeException("just testing stuff");
}
}
public class TestOne extends AbstractTest {
@Test
public void test() {
System.out.println("<<<<<>>>>> TestOne.test()");
}
}
public class TestTwo extends AbstractTest {
@Test
public void test() {
System.out.println("<<<<<>>>>> TestTwo.test()");
}
}
So TestOne and TestTwo extend a common superclass that defines a
@BeforeClass method. When I invoke the tests, I get this output (excerpt
from eclipse, latest plugin, the same on the command line):
<<<<<>>>>> AbstractTest.beforeAbstractTestClass() for class com.test.TestOne
FAILED CONFIGURATION: @BeforeClass beforeAbstractTestClass
java.lang.RuntimeException: just testing stuff
at com.test.AbstractTest.beforeAbstractTestClass(AbstractTest.java:13)
... Removed 22 stack frames
SKIPPED CONFIGURATION: @BeforeClass beforeAbstractTestClass
SKIPPED: test
SKIPPED: test
As you can see, the @BeforeClass is called only once, and it fails, and the
other test is ignored not called. So, the result of @BeforeClass of one test
subclass impacts whether the other subclass is called.
This might be ok for this simple case, as the method fails always, so it is
actually smart to not bother calling it again. Hovewer, you can never be
sure if this invariant is true. Consider a possibility that a the
@BeforeClass method actually does some stuff and it's result varies depends
on some variables:
if (getClass().getName().contains("TestOne")) throw new RuntimeException();
(Please suppose for the purpose of this example that TestOne always precedes
TestTwo.)
Sure, it is a stupid example, but what I mean by this is: TestOne is invoked
first, and the @BeforeClass method throws an exception, so its tests are
skipped - all good. Then, the method would be called for TestTwo, and it
would finish normally, so its test should be executed. This doesn't happen.
To make the example more concrete: in the common superclass I am reading
annotations on the test class / its members and if it is fine, the tests are
executed; if there is some problem, it is a configuration problem and the
method throws an exception are skipped. I noticed that some tests were
skipped entirely, not even their @BeforeClass was called, as described, and
hence I started investigating this, and produced this simplest case.
Is this supposed to work this way? Maybe I simply need to change my
@BeforeClass and throw some other kind of exception? I tried TestNGException
and SkipException, with the difference that the latter causes the first
invocation of the method be classified "skipped" instead of "failed".
Please tell me if I am doing things wrong here.
Best regards,
Wujek
I have a test hierarchy:
public class AbstractTest {
@BeforeClass
public void beforeAbstractTestClass() {
System.out.println("<<<<<>>>>>
AbstractTest.beforeAbstractTestClass() for class " + getClass().getName());
throw new RuntimeException("just testing stuff");
}
}
public class TestOne extends AbstractTest {
@Test
public void test() {
System.out.println("<<<<<>>>>> TestOne.test()");
}
}
public class TestTwo extends AbstractTest {
@Test
public void test() {
System.out.println("<<<<<>>>>> TestTwo.test()");
}
}
So TestOne and TestTwo extend a common superclass that defines a
@BeforeClass method. When I invoke the tests, I get this output (excerpt
from eclipse, latest plugin, the same on the command line):
<<<<<>>>>> AbstractTest.beforeAbstractTestClass() for class com.test.TestOne
FAILED CONFIGURATION: @BeforeClass beforeAbstractTestClass
java.lang.RuntimeException: just testing stuff
at com.test.AbstractTest.beforeAbstractTestClass(AbstractTest.java:13)
... Removed 22 stack frames
SKIPPED CONFIGURATION: @BeforeClass beforeAbstractTestClass
SKIPPED: test
SKIPPED: test
As you can see, the @BeforeClass is called only once, and it fails, and the
other test is ignored not called. So, the result of @BeforeClass of one test
subclass impacts whether the other subclass is called.
This might be ok for this simple case, as the method fails always, so it is
actually smart to not bother calling it again. Hovewer, you can never be
sure if this invariant is true. Consider a possibility that a the
@BeforeClass method actually does some stuff and it's result varies depends
on some variables:
if (getClass().getName().contains("TestOne")) throw new RuntimeException();
(Please suppose for the purpose of this example that TestOne always precedes
TestTwo.)
Sure, it is a stupid example, but what I mean by this is: TestOne is invoked
first, and the @BeforeClass method throws an exception, so its tests are
skipped - all good. Then, the method would be called for TestTwo, and it
would finish normally, so its test should be executed. This doesn't happen.
To make the example more concrete: in the common superclass I am reading
annotations on the test class / its members and if it is fine, the tests are
executed; if there is some problem, it is a configuration problem and the
method throws an exception are skipped. I noticed that some tests were
skipped entirely, not even their @BeforeClass was called, as described, and
hence I started investigating this, and produced this simplest case.
Is this supposed to work this way? Maybe I simply need to change my
@BeforeClass and throw some other kind of exception? I tried TestNGException
and SkipException, with the difference that the latter causes the first
invocation of the method be classified "skipped" instead of "failed".
Please tell me if I am doing things wrong here.
Best regards,
Wujek
--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng-users-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to testng-users+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng-users-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to testng-users+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.