Android, Junit and testPreconditions()

2019-08-07 18:06发布

问题:

I'm trying to do some tests on my application's database. I only got one activity in my application ("Home") so my test class is
public class HomeTest extends ActivityInstrumentationTestCase2 {..}

My troubles :

1) In Android developers I've seen that testPreconditions() method is supposed to be launch before all tests, but in my app, it's acting like a normal test ...

2) I'd like to fill my database before all other tests to begin. But to do so, I need a reference on my Activity, but I can't call "this.getActivity" in the tests constructor ... Is there a way to do something after the constructor ? (The setUp() method isn't appropriate, because it is called before each test, not just one time before all)

Thanks

回答1:

  1. The order in which the test are run is not guaranteed to run before other tests, as junit 3 uses reflection to find the tests.

  2. Use the setUp() method, this also assure that your tests run with a fresh database