Here, we will analyze the structure and execution process of the android testing program based on the music testing program.
O testrunner
1. instrumentation is a basic executable unit, which is similar to activity and service. All test programs must inherit from it.
2. testsuiteprovider is an interface with an interface function gettestsuite to obtain test cases.
3. instrumentationtestrunner mainly reloads oncreate and onstart of instrumentation to associate execution threads with test cases.
4. musicplayerstresstestrunner reloads getalltests. The framework obtains test cases through getalltests.
O execution process of the Test Program
1. instrumentationtestrunner: oncreate
Call gettestsuite to obtain the test case, create the androidtestrunner object mtestrunner, and associate testrunner and testcase.
2. instrumentation: Start
Create a thread instrumentationthread and execute the start function of the thread.
3. instrumentationthread: Start
In turn, instrumentation: onstart is called. This function is actually implemented in instrumentationtestrunner.
4. instrumentationtestrunner: onstart
Call mtestrunner. runtest.
5. androidtestrunner: runtest
In a loop, call the run function of each test case.
O testcase
1. runnable is an interface with only one run function.
2. testcase also provides the setup and teardown functions to perform initialization and Cleanup before and after the test.
3. instrumentationtestcase
The instrumentation is packaged and functions such as sendkeys are provided to facilitate the use of testcase. Runtest and
Runtestonuithread functions, but I still don't know how to call them here.
4. activityinstrumentationtestcase is a template that creates a specified activity during setup.
5. testplaylist is the actual test program. Here we implement test functions such as testdeleteplaylist.
O classification of test programs
1. Suppress
Use this annotation on test classes or test methods that shocould not be encoded in a test
Suite. If the annotation appears on the class then no tests in that class will be encoded. If
The annotation appears only on a test method then only that method will be excluded.
2. largetest
Marks a test that shoshould run as part of the large tests.
3. mediumtest
Marks a test that shoshould run as part of the medium tests.
4. smalltest
Marks a test that shoshould run as part of the small tests.
5. Smoke
Marks a test that shoshould run as part of the smoke tests.
The android. Test. suitebuilder. smoketestsuitebuilder
Will run all tests with this annotation.
Add annotation before a function or class to classify it. For example:
@LargeTest
public void testDeletePlaylist() throws Exception{
boolean isEmptyPlaylist = true;
addNewPlaylist(MusicPlayerNames.DELETE_PLAYLIST_NAME);
deletePlaylist(MusicPlayerNames.DELETE_PLAYLIST_NAME);
isEmptyPlaylist = verifyPlaylist(MusicPlayerNames.DELETE_PLAYLIST_NAME);
assertFalse("testDeletePlaylist", isEmptyPlaylist);
}
In instrumentationtestrunner, the command line parameters are used to determine which type of test to run:
private Predicate<TestMethod> getSizePredicateFromArg(String sizeArg) {
if (SMALL_SUITE.equals(sizeArg)) {
return TestPredicates.SELECT_SMALL;
} else if (MEDIUM_SUITE.equals(sizeArg)) {
return TestPredicates.SELECT_MEDIUM;
} else if (LARGE_SUITE.equals(sizeArg)) {
return TestPredicates.SELECT_LARGE;
} else {
return null;
}
}
testSuiteBuilder.addRequirements(testSizePredicate);
Filter the test program in testsuitebuilder:
*/
public final TestSuite build() {
rootSuite = new TestSuite(getSuiteName());
// Keep track of current class so we know when to create a new sub-suite.
currentClassname = null;
try {
for (TestMethod test : testGrouping.getTests()) {
if (satisfiesAllPredicates(test)) {
addTest(test);
}
}
if (testCases.size() > 0) {
for (TestCase testCase : testCases) {
if (satisfiesAllPredicates(new TestMethod(testCase))) {
addTest(testCase);
}
}
}
} catch (Exception exception) {
Log.i("TestSuiteBuilder", "Failed to create test.", exception);
TestSuite suite = new TestSuite(getSuiteName());
suite.addTest(new FailedToCreateTests(exception));
return suite;
}
return rootSuite;
}
O androidmanifest. xml
Androidmanifest. XML is used to describe the entrance of each test program:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.music.tests">
<application>
<uses-library android:name="android.test.runner" />
</application>
<instrumentation android:name=".MusicPlayerLaunchPerformance"
android:targetPackage="com.android.music"
android:label="Music Launch Performance">
</instrumentation>
<instrumentation android:name=".MusicPlayerFunctionalTestRunner"
android:targetPackage="com.android.music"
android:label="Music Player Functional Test">
</instrumentation>
<instrumentation android:name=".MusicPlayerStressTestRunner"
android:targetPackage="com.android.music"
android:label="Music Player Stress Test">
</instrumentation>
</manifest>
O run the test program
1. Compile
cd packages/apps/Music/tests
mm
2. Installation
adb install ../../../../out/target/product/littleton/data/app/MusicTests.apk
3. Run
adb shell am instrument -w com.android.music.tests/.MusicPlayerFunctionalTestRunner