capture failed tests

8 views
Skip to first unread message

steve.ebersole

unread,
Jul 30, 2007, 5:39:30 PM7/30/07
to testng-dev
I was wondering if there is a mechanism for handling test failures in
TestNG? In JUnit, this was achieved via overriding runTest() with a
try/catch block. Is something similar available in TestNG?

I would not mind something like the runTest() from JUnit, but I was
thinking something like the ability to annotate a method with @Failure
(or similar) would be awesome.

Alexandru Popescu ☀

unread,
Jul 30, 2007, 5:50:23 PM7/30/07
to testn...@googlegroups.com

It is not very clear what you would like to achieve. If your question is:
- how can I ignore failing tests:
a) you can disable them
b) you can add them to a special "failures" group that is excluded
- how can you deal with exceptions in @Test
use @Test(expectedExceptions={})

Can you please add some more context to your question?

bests,
./alex
--
.w( the_mindstorm )p.
TestNG co-founder
EclipseTestNG Creator

>
> >
>

steve.ebersole

unread,
Jul 30, 2007, 5:55:25 PM7/30/07
to testng-dev
On Jul 30, 4:50 pm, "Alexandru Popescu ☀"

<the.mindstorm.mailingl...@gmail.com> wrote:
> It is not very clear what you would like to achieve.
> Can you please add some more context to your question?

Absolutely. I was trying to keep it short and sweet ;)

Essentially, as part of a "before class" process I would like to build
a Hibernate SessionFactory. The SessionFactory should get closed
"after class". However, in between, if a test fails, I need to close
the current SessionFactory and create a new one (essentially
rebuilding the database schema) so that the next test after the
failing test can start fresh.

Alexandru Popescu ☀

unread,
Jul 30, 2007, 6:08:56 PM7/30/07
to testn...@googlegroups.com
On 7/31/07, steve.ebersole <steven....@gmail.com> wrote:
>

This is an interesting one!

In TestNG if a test fails the other will be skipped. However this
doesn't seem to fit your scenario. On the other hand, if you declare
the @Test(expectedException="HibernateException") then the @Test will
be considered as passed, so you will not know it.... Hmmm, I don't
think I have a solution for this. Must think more about it.

./alex
--
.w( the_mindstorm )p.
TestNG co-founder
EclipseTestNG Creator

PS: there may be an ugly one, though: considering the test instance is
statefull (as opposed to JUnit where it is stateless, meaning that for
each test method the class is re-instantiated), you can go with a
trick like:

private boolean m_failed;

@BeforeMethod
public void validateSessionFactory() {
if(m_failed) {
// m_sessionFactory =
m_failed = false
}
}

@Test
public void someTest() {
try {
//
}
catch(HibernateException hibex) {
m_failed = true;
//
Reporter.log("someTest failed: " ....
}
}

Unfortunately this has a few drawbacks as you can probably notice :-(.

>
> >
>

Cédric Beust ♔

unread,
Jul 30, 2007, 6:04:05 PM7/30/07
to testn...@googlegroups.com

How about setting a flag if a test fails and checking for this flag in an @AfterMethod?  (and if it's true, create a new session)

--
Cédric

Alexandru Popescu ☀

unread,
Jul 30, 2007, 6:18:54 PM7/30/07
to testn...@googlegroups.com

That's exactly what I've suggested too (unfortunately it has a couple
of drawbacks: mainly you are loosing the fact that the test failed).
This scenario is a bit against of TestNG principles: it wants to
continue after a failure :-).

bests,
./alex
--
.w( the_mindstorm )p.
TestNG co-founder
EclipseTestNG Creator

> --
> Cédric
>
> >
>

Cédric Beust ♔

unread,
Jul 30, 2007, 6:16:46 PM7/30/07
to testn...@googlegroups.com

As Alexandru pointed out, this would work but would be pretty tedious since you'd have to repeat this in every test method.

I can imagine introducing a new kind of annotated method that could be called every time a test method throws an exception:

@OnFailure
public void onFailure(Throwable e, ITestNGMethod method) {
}

If this method throws an exception, then the method failure is considered a "real" failure and all the other methods on the class are skipped (as is the case today), but if the @OnFailure method returns without throwing, the test method is still logged as a failure but the other methods will be invoked anyway.

Seems a bit contrived and I'd like to be convinced that there are other situations where such a feature would be useful...

--
Cédric

Alexandru Popescu ☀

unread,
Jul 30, 2007, 6:35:13 PM7/30/07
to testn...@googlegroups.com

I am thinking that having something like JUnit runTest() protocol may
be very intersting. It will probably partially solve this problem, it
will allow easier integration with mocking frameworks.

bests,
./alex
--
.w( the_mindstorm )p.
TestNG co-founder
EclipseTestNG Creator

> --
> Cédric
> >
>

steve.ebersole

unread,
Jul 31, 2007, 1:54:58 PM7/31/07
to testng-dev
I missed the point that tests stop once there is a failure.
Unfortunately, that will not work for me :(

Is there not a way to tell TestNG to "keep going" on failure?

steve.ebersole

unread,
Jul 31, 2007, 2:12:50 PM7/31/07
to testng-dev
> @OnFailure
> public void onFailure(Throwable e, ITestNGMethod method) {
>
> }
>
> If this method throws an exception, then the method failure is considered a
> "real" failure and all the other methods on the class are skipped (as is the
> case today), but if the @OnFailure method returns without throwing, the test
> method is still logged as a failure but the other methods will be invoked
> anyway.
>
This sounds perfect. I missed the "other methods will be invoked
part" in here.

Well not sure what I can do to convince you about the overall
usefulness of this. I can tell you that it is absolutely needed for
me to move Hibernate over to TestNG. We have what some would call
poorly designed test classes where lots of different types of behavior
and different types of assertions are tested in the same test
classes. The one thing in common for these test classes is that all
the test methods operate against a common database schema. Because
they test different pieces of functionality, I don't want the tests to
stop when a particular method fails. However, because they use the
same schema and I don't know if the schema was properly cleaned, I
want to recreate that schema before the next test begins.

If there is another way to achieve this given existing TestNG
features, I'm happy to learn about it and try out that approach. But
I did not see anything.

steve.ebersole

unread,
Jul 31, 2007, 2:43:34 PM7/31/07
to testng-dev
BTW, another thing that would be really useful (although this perhaps
is specific to me and my use-case) would be the ability to know which
test failed in terms of its overall position within the test class
methods. Why? So I know whether to even bother rebuilding the
SessionFactory (if it was the last test method which failed, there is
no need).

Cédric Beust ♔

unread,
Jul 31, 2007, 3:05:07 PM7/31/07
to testn...@googlegroups.com
By the way, I just remembered another thing that might be relevant here:  IHookable.

The Javadoc can be found here.

What do you think?

--
Cédric


On 7/31/07, steve.ebersole <steven....@gmail.com> wrote:

Alexandru Popescu ☀

unread,
Jul 31, 2007, 3:19:54 PM7/31/07
to testn...@googlegroups.com
On 7/31/07, Cédric Beust ♔ <cbe...@google.com> wrote:
> By the way, I just remembered another thing that might be relevant here:
> IHookable.
>
> The Javadoc can be found here.
>
> What do you think?
>

He he... we are having so many features that even us can forget about :).

I think this may be a partial solution, but I am not 100% sure it will
solve the fail - continue scenario (maybe we should add a test level
parameter for dealing with this).

./alex
--
.w( the_mindstorm )p.
TestNG co-founder
EclipseTestNG Creator

> --

steve.ebersole

unread,
Jul 31, 2007, 3:29:06 PM7/31/07
to testng-dev
Is IHookable#run called before each test method? It was not clear to
me from the javadocs whether that was the case, or if it was just
called once for the class. In the case of b4 each method, then this
certainly would fit the bill I think; otherwise I don't see how that
could work unless I can keep calling callback() after each failure.

Reply all
Reply to author
Forward
0 new messages