开发者

reusing junit tests in Android

开发者 https://www.devze.com 2023-04-06 23:17 出处:网络
I have a library whose Junit 3 tests I\'d like to reuse on Android.The problem is that these tests depend on files being present (test data).

I have a library whose Junit 3 tests I'd like to reuse on Android. The problem is that these tests depend on files being present (test data).

For Android this means that some setup code needs to call getContext().getAssets() and dump the asset files to the sdcard, where they will be picked up by the tests.

The problem is: where do I put this setup code? I can't put it in the test's setup, as it inherits from TestCase (and anyway that would mean its no longer portable). I can't put it in the AllTests suite, as that inherit开发者_如何学Cs from TestSuite. Since its a Junit project there's no Activity whose onCreate I can hook.


The solution is to make a custom InstrumentationTestRunner. Override onCreate, where custom setup logic can be stored. For example:

public class InstrumentationTestRunner extends
        android.test.InstrumentationTestRunner {

    @Override
    public void onCreate(Bundle arguments) {
        setUpTests();
        super.onCreate(arguments);      
    }

    protected void setUpTests() {
        // ....
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消