Android: UnitTest

前端 未结 2 1564
死守一世寂寞
死守一世寂寞 2021-02-11 06:57

Need some advice. I have an App. And I need do write some UnitTest. But I really don\'t know what to test. I already test Settings and all preferences, that\'s easy. What else u

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-11 07:33

    Android provides test classes for you to be able to be as exhaustive in your unit testing as you want to be. You can unit test your java components, your android-architecture dependant components, and your workflow as a whole.

    In your case, to test sequences between activities, you can use the InstrumentationTestCase class and extend it, and then in your tests, you should use the following methods:

    // Prepare a monitor for your activity
    Instrumentation instrumentation = getInstrumentation()
    Instrumentation.ActivityMonitor monitor = instrumentation.addMonitor(YourClass.class.getName(), null, false);
    
    // Start your activity manually
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setClassName(instrumentation.getTargetContext(), YourClass.class.getName());
            instrumentation.startActivitySync(intent);
    
    // Get the started Activity
    Activity currentActivity = getInstrumentation().waitForMonitor(monitor);
    

    Then you can remove the monitor and add another monitor for the next activity you expect to catch in the workflow sequence. With this sequence you can programatically interact and verify sequences of actions that go through multiple activities.

提交回复
热议问题