2

I'm implementing a Test automation tool and I have a class which extends InstrumentationTestCase. For example:

public class BaseTests extends InstrumentationTestCase {

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        Log.d(TAG, "setUp()");
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
        Log.d(TAG, "tearDown()");
    }

    public void test_one() {
        Log.d(TAG, "test_one()");
    }

    public void test_two() {
        Log.d(TAG, "test_two()");
    }
}

When I run the tests of BaseTests, the setUp() method is called 2 times. One time before executing test_one() and another after test_two(). The same happens with the tearDown(), it is called after executing each of both two methods.

What I would like to do here is to call setUp() and tearDown() methods only one time for the execution of all BaseTests tests. So the order of the method call would be like:

1) setUp()

2) test_one()

3) test_two()

4) tearDown()

Is there a way to do such thing?

Felipe Mosso
  • 3,907
  • 11
  • 38
  • 61
  • I would rather not change this behaviour, but you could have a boolean that indicates whenever or not the first pair of setUp/tearDown has been called. Those callbacks are called to let you setUp the environment before each test starts and to clean it up after the test ends – Blackbelt Jul 22 '14 at 17:02

2 Answers2

2

I resolve this problem by using:

@BeforeClass
public static void setUpBeforeClass() throws Exception {
}

and:

@AfterClass
public static void tearDownAfterClass() throws Exception {
}

instead of setUp() and tearDown(). So in your case it would be:

import org.junit.AfterClass;
import org.junit.BeforeClass;
public class BaseTests extends InstrumentationTestCase {

@BeforeClass
protected static void setUp() throws Exception { 
    //do your setUp
    Log.d(TAG, "setUp()");
}

@AfterClass
protected static void tearDown() throws Exception {
    //do your tearDown
    Log.d(TAG, "tearDown()");
}

public void test_one() {
    Log.d(TAG, "test_one()");
}

public void test_two() {
    Log.d(TAG, "test_two()");
}
}

The annotations @BeforeClass and @AfterClass assure that it will run only one time before and after the test runs respectively

cristianorbs
  • 670
  • 3
  • 9
  • 16
  • Thanks for the answer. But adding such annotations made my build break. should I import anything else? – Felipe Mosso Jul 22 '14 at 17:13
  • Yes, I forgot the imports. You should import: import org.junit.AfterClass; import org.junit.BeforeClass; – cristianorbs Jul 22 '14 at 17:16
  • I could build now, thanks for reminder me the imports.. But it is still printing alternately the setUp and tearDown for each test.. I don't know if this is happening because i'm extending InstrumentationTestCase – Felipe Mosso Jul 22 '14 at 17:33
  • I guess android.test.InstrumentationTestCase does not have these annotations stuff like JUnit – Felipe Mosso Jul 22 '14 at 17:35
  • It also depends on how you're running these tests – cristianorbs Jul 22 '14 at 17:42
  • In which way you mean? I'm running through Eclipse.. Run as Android JUnit Test – Felipe Mosso Jul 22 '14 at 17:47
  • Why don't you use just JUnit 4.x instead of using InstrumentationtestCase? Or does your problem require specifically to use InstrumentationtestCase. If it doesn't you can do using just JUnit – cristianorbs Jul 22 '14 at 17:47
  • unfortunately I have to use InstrumentationtestCase :( – Felipe Mosso Jul 22 '14 at 17:53
  • I think this answer will solve your problem: check it out http://stackoverflow.com/questions/7208593/how-can-i-get-beforeclass-and-afterclass-equivalent-in-junit3 – cristianorbs Jul 22 '14 at 18:02
  • Thanks for providing us this link.. but this hasn't helped me so much.. I think I will end up doing the beforeClass and afterClass by myself.. by following this: https://android.googlesource.com/platform/frameworks/base/+/9db3d07b9620b4269ab33f78604a36327e536ce1/test-runner/android/test/PerformanceTestBase.java – Felipe Mosso Jul 22 '14 at 18:59
0

I ended up following the idea of @beforeClass and @afterClass.

However I couldn't use the annotations itself. Instead, I implemented them (by using counters) on a base class and my test suites inherits from this base class.

Here's the link I based myself to do so:

https://android.googlesource.com/platform/frameworks/base/+/9db3d07b9620b4269ab33f78604a36327e536ce1/test-runner/android/test/PerformanceTestBase.java

I hope this could help someone else!

Felipe Mosso
  • 3,907
  • 11
  • 38
  • 61