@mSolo
2015-06-01T11:33:02.000000Z
字数 9539
阅读 2411
Android
测试
TDD
持续集成
assertTrue()、assertFalse()、assertEquals()、assertNull()、assertNotNull()
assertSame()、assertNotSame()、fail()
testApplicationId "com.blundell.something.non.default"
testInstrumentationRunner "com.blundell.tut.CustomTestRunner"
testHandleProfiling false
testFunctionalTest true
testCoverageEnabled true
public class MainActivityTest extends TestCase {
public MainActivityTest() {
super("MainActivityTest");
}
public MainActivityTest(String name) { //
super(name);
}
@SmallTest
public void testSomething() throws Exception {
fail("Not implemented yet");
}
}
从命令行执行测试
am instrument [flags] <COMPONENT> -r -e <NAME> <VALUE> -p <FILE> -w
adb shell
am instrument -w -e class
com.blundell.tut.MainActivityTest\#testSomething
com.blundell.tut.test/android.test.InstrumentationTestRunner
am instrument -e log true
<NAME> <VALUE>
其它取值有:package、coverageTest Annotations
创建自定义 annotation
@Retention(RetentionPolicy.RUNTIME)
public @Interface VeryImportantTest {
}
// ...
@VeryImportantTest
public void testOtherStuff() {
fail("Also not implemented yet");
}
am instrument -w -e annotation com.blundell.tut.VeryImportantTest
com.blundell.tut.test/android.test.InstrumentTestRunner
assertBaselineAligned
、assertBottomAligned
assertGroupContains
、assertGroupNotContains
: This asserts that the specified group contains a specific child once and only once.assertGroupIntegrity
: This asserts the specified group’s integrity. The child count should be >= 0 and each child should be non-null.assertHasScreenCoordinates
: This asserts that a View has a particular x and y position on the visible screen.assertHorizontalCenterAligned
: This asserts that the test View is horizontally center aligned with respect to the reference view.assertLeftAligned
、assertRightAligned
: This asserts that two Views are left aligned; that is, their left edges are on the same x location. An optional margin can also be provided.assertOffScreenAbove
、assertOffScreenBelow
: This asserts that the specified view is above the visible screen.assertOnScreen
: This asserts that a View is on the screen.assertTopAligned
: This asserts that two Views are top aligned; that is, their top edges are on the same y location. An optional margin can also be specified.assertVerticalCenterAligned
: This asserts that the test View is vertically center-aligned with respect to the reference View.
public void testUserInterfaceLayout() {
int margin = 0;
View origin = mActivity.getWindow().getDecorView();
assertOnScreen(origin, editText);
assertOnScreen(origin, button);
assertRightAligned(editText, button, margin);
}
@UiThreadTest
public void testNoErrorInCapitalization() {
String msg = "capitalize this text";
editText.setText(msg);
button.performClick();
String actual = editText.getText().toString();
String notExpectedRegexp = "(?i:ERROR)";
String errorMsg = "Capitalization error for " + actual;
assertNotContainsRegex(errorMsg, notExpectedRegexp, actual);
}
public void testListScrolling() {
listView.scrollTo(0, 0);
TouchUtils.dragQuarterScreenUp(this, activity);
int actualItemPosition = listView.getFirstVisiblePosition();
assertTrue("Wrong position", actualItemPosition > 0);
}
public void assertActivityRequiresPermission(String packageName, String className, String permission)
public void testActivityPermission() {
String pkg = "com.blundell.tut";
String activity = PKG + ".MyContactsActivity";
String permission = android.Manifest.permission.CALL_PHONE;
assertActivityRequiresPermission(pkg, activity, permission);
}
public void assertReadingContentUriRequiresPermission(Uri uri, String permission)
public void assertWritingContentUriRequiresPermission(Uri uri, String permission)
public void testFollowLink() {
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_VIEW);
intentFilter.addDataScheme("http");
intentFilter.addCategory(Intent.CATEGORY_BROWSABLE);
Instrumentation inst = getInstrumentation();
ActivityMonitor monitor = inst.addMonitor(intentFilter, null, false);
TouchUtils.clickView(this, linkTextView);
monitor.waitForActivityWithTimeout(3000);
int monitorHits = monitor.getHits();
inst.removeMonitor(monitor);
assertEquals(1, monitorHits);
}
ProviderTestCase2(Class<T> providerClass, String providerAuthority)
public fina T launchActivity(String pkg, Class<T> activityCls, Bundle extras)
public final T launchActivityWithIntent(String pkg, Class<T> activityCls, Intent intent)
public void testSendKeyInts() {
requestMessageInputFocus();
sendKeys(
KeyEvent.KEYCODE_H,
KeyEvent.KEYCODE_E,
KeyEvent.KEYCODE_E,
KeyEvent.KEYCODE_E,
KeyEvent.KEYCODE_Y,
KeyEvent.KEYCODE_DPAD_DOWN,
KeyEvent.KEYCODE_ENTER);
/*
sendRepeatedKeys(
1, KeyEvent.KEYCODE_H,
3, KeyEvent.KEYCODE_E,
1, KeyEvent.KEYCODE_Y,
1, KeyEvent.KEYCODE_DPAD_DOWN,
1, KeyEvent.KEYCODE_ENTER);
*/
// sendKeys("H 3*E Y DPAD_DOWN ENTER");
String actual = messageInput.getText().toString();
assertEquals("HEEEY", actual);
}
private void requestMessageInputFocus() {
try {
runTestOnUiThread(new Runnable() {
@Override
public void run() {
messageInput.requestFocus();
}
});
} catch (Throwable throwable) {
fail("Could not request focus.");
}
instrumentation.waitForIdleSync();
}
protected void scrubClass(class<?> testCaseClass)
- invoked from the tearDown() method
- clean up class virables for prevent memory leaks for large test suites.
protected void setUp() throws Exception {
super.setUp();
// this must be called before getActivity()
// disabling touch mode allows for sending key events
setActivityInitialTouchMode(false);
activity = getActivity();
instrumentation = getInstrumentation();
linkTextView = (TextView)activity.findViewById(R.id.main_text_link);
messageInput = (EditText)activity.findViewById(R.id.main_input_message);
capitalizeButton = (Button)activity.findViewById(R.id.main_button_capitalize);
}
protected void tearDown() throws Exception {
super.tearDown();
myObject.dispose();
}
public void testQuery() {
String segment = "dummySegment";
Uri uri = Uri.withAppendedPath(MyProvider.CONTENT_URI, segment);
Cursor c = provider.query(uri, null, null, null, null);
try {
int actual = c.getCount();
assertEquals(2, actual);
} finally {
c.close();
}
}
public void testDeleteByIdDeletesCorrectNumberOfRows() {
String segment = "dummySegment";
Uri uri = Uri.withAppendedPath(MyProvider.CONTENT_URI, segment);
int actual = provider.delete(uri, "_id = ?", new String[]{"1"});
assertEquals(1, actual);
}
public ServiceTestCase(Class<T> serviceClass)
The TestSuiteBuilder.FailedToCreateTests class is a special TestCase class used to indicate a failure during the build() step.
import com.blundell.dummylibrary.Dummy;
public class MyFirstProjectActivity extends Activity {
private Dummy dummy;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText messageInput = (EditText) findViewById(R.id.main_input_message);
Button capitalizeButton = (Button) findViewById(R.id.main_button_capitalize);
capitalizeButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String input = messageInput.getText().toString();
messageInput.setText(input.toUpperCase());
}
});
dummy = new Dummy();
}
public Dummy getDummy() {
return dummy;
}
public static void methodThatShouldThrowException() throws Exception {
throw new Exception("This is an exception");
}
}
public void testDummy() {
assertNotNull(activity.getDummy());
}