Android: UnitTest

前端 未结 2 1562
死守一世寂寞
死守一世寂寞 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.

    0 讨论(0)
  • 2021-02-11 07:59

    For Android applications there is a tool for testing called Robotium. It is a UI testing tool which simulates touching, clicks, typing, and other users' actions relevant for Android applications.Try this example for Robotium and can develop for your apps,

    Robotium example pdf

    i think this may be little bit useful to your search.

    0 讨论(0)
提交回复
热议问题