Reputation: 87
I'm having some troubles with testing in Android.
For each test I do, I have a common set of instructions to do before the test and after the test. So, the idea would be define a setUp()
and tearDown()
method.
So far so good. The problem is that, apparently, the tearDown()
method is not invoked when the test "fails" (that means when the tests throws an Exception
).
Is there a method that can be invoked when a test throws an Exception or a "tricky" way to do this?
Upvotes: 0
Views: 62
Reputation: 4319
Test like this:
try{
setUp();
/* do some testing*/
}catch(Exception ex){
/* do something with the exception */
/* let the test fail */
}finally{
tearDown();
}
It's probably the easiest solution for your problem, but from the viewpoint of good design practice this should be avoided.
edit:
There are annotations in jUnit: Different teardown for each @Test in jUnit
edit2:
The setUp() method is invoked before every test. You use it to initialize variables and clean up from previous tests. You can also use the JUnit tearDown() method, which runs after every test method. The tutorial does not use it.
http://developer.android.com/tools/testing/activity_test.html
If you have different tearDown()
Methods for each test, I think you need to change them.
Upvotes: 2