您的位置:首页 > 移动开发 > Android开发

android 测试

2016-03-11 11:55 585 查看
官方:
https://developer.android.com/studio/test/index.html https://developer.android.com/training/testing/start/index.html https://github.com/dongdaqing/TestingExample
android 测试框架选择http://www.codeceo.com/article/5-android-test-framework.html
https://github.com/googlesamples/android-testing
1.android-testing-master\unit\BasicSample 第一个:

软件功能描述:

把名字,生日,email,作为一组数,用SharedPreferences保存。revert可以恢复上一次保存的。

test内容:

email格式:不能有空,"", 有com, 有.com。写法很简单:

@Test
public void emailValidator_EmptyString_ReturnsFalse() {
assertFalse(EmailValidator.isValidEmail(""));
}


保存SharedPreferences功能:主要用到mockito。
http://blog.csdn.net/sanjay_f/article/details/50178215 http://blog.csdn.net/sdyy321/article/details/38757135/ 。脱离环境,隔离测试。
http://www.oschina.net/translate/mockito-a-great-mock-framework-for-java-development
因为要测保存功能。而不是测试SharedPreferences类。我们也不关心SharedPreferences功能好不好。所以我们要模拟一个SharedPreferences功能,来测我们的保存功能。

我觉得这个比较有用啊。假设SharedPreferences是由别人完成的,功能还没完成,我们需要测试,所以要模拟一个存储功能。

今天先写到这。

D:\code\android-testing-master\unit\BasicUnitAndroidTest

似乎太简单



D:\code\android-testing-master\ui\uiautomator\BasicSample

有动的效果,ui测试。模拟点击按钮。效果很棒,很简单。

public void testChangeText_sameActivity() {
// Type text and then press the button.
mDevice.findObject(By.res(BASIC_SAMPLE_PACKAGE, "editTextUserInput"))
.setText(STRING_TO_BE_TYPED); //输入框,输入字符串
mDevice.findObject(By.res(BASIC_SAMPLE_PACKAGE, "changeTextBt")) //点击按钮
.click();

// Verify the test is displayed in the Ui
UiObject2 changedText = mDevice
.wait(Until.findObject(By.res(BASIC_SAMPLE_PACKAGE, "textToBeChanged")),
500 /* wait 500ms */); //得到输出框
assertThat(changedText.getText(), is(equalTo(STRING_TO_BE_TYPED)));
}


D:\code\android-testing-master\ui\espresso\BasicSampleBundled 有问题。

UIAutomator – is powerful and has good external OS system integration e.g. can turn WiFi on and off and access other settings during test, but lacks backward compatibility as it requires Jelly Bean or higher. But, also lacks detailed view access so one could
say it may be more of a pure black-box test. Where as Espresso has access to view internals (see below). This is recommended on developer.android.com for "Testing UI for Multiple Apps"

Espresso - is a bit more light weight compared to ui automator and supports 2.2 Froyo and up it also has a fluent api with powerful hamcrest(https://github.com/hamcrest)
integration making code more readable and extensible (it is newer than Ui automator). It does not have access to system integration tests but has access to view internals e.g. can test a webview (useful for Hybrid app testing, or webview heavy testing). Slightly
more grey-box testing compared to UI Automator. This is recommended on developer.android.com for "Testing UI for a Single App". As of Android Studio 2.2 this now offers UI test recording (like UIAutomator)


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: