0%

Android 单元测试笔记

Getting Started With Testing

两种类型的单元测试

  1. Local unit tests
  2. Instrumented tests

Local Unit Tests

  1. 代码放在src/test/java
  2. 使用Junit 4Mockito添加依赖
dependencies {
// Required -- JUnit 4 framework
testCompile 'junit:junit:4.12'
// Optional -- Mockito framework
testCompile 'org.mockito:mockito-core:1.10.19'
}
  1. 编写测试代码

@RunWith(MockitoJUnitRunner.class)
public class UnitTestSample {

private static final String FAKE_STRING = "HELLO WORLD";

@Mock
Context mMockContext;

@Test
public void readStringFromContext_LocalizedString() {
// Given a mocked Context injected into the object under test...
when(mMockContext.getString(R.string.hello_word))
.thenReturn(FAKE_STRING);
ClassUnderTest myObjectUnderTest = new ClassUnderTest(mMockContext);

// ...when the string is returned from the object under test...
String result = myObjectUnderTest.getHelloWorldString();

// ...then the result should be the expected one.
assertThat(result, is(FAKE_STRING));
}
}

Instrumented Tests

有三种类型

  1. Building Instrumented Unit Tests
  2. Automating User Interface Tests
  3. Testing App Component Integrations

Building Instrumented Unit Tests

  1. 代码放在src/androidTest/java
  2. 添加依赖
dependencies {
androidTestCompile 'com.android.support:support-annotations:23.0.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
// Optional -- Hamcrest library
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
// Optional -- UI testing with Espresso
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
// Optional -- UI testing with UI Automator
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
}
  1. Specify AndroidJunitRunner as the default test instrumentation runner. The Testing Support Library includes a JUnit 4 test runner
android {
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
  1. 编写测试代码
@RunWith(AndroidJUnit4.class)
@SmallTest
public class LogHistoryAndroidUnitTest {

public static final String TEST_STRING = "This is a string";
public static final long TEST_LONG = 12345678L;
private LogHistory mLogHistory;

@Before
public void createLogHistory() {
mLogHistory = new LogHistory();
}

@Test
public void logHistory_ParcelableWriteRead() {
// Set up the Parcelable object to send and receive.
mLogHistory.addEntry(TEST_STRING, TEST_LONG);

// Write the data.
Parcel parcel = Parcel.obtain();
mLogHistory.writeToParcel(parcel, mLogHistory.describeContents());

// After you're done with writing, you need to reset the parcel for reading.
parcel.setDataPosition(0);

// Read the data.
LogHistory createdFromParcel = LogHistory.CREATOR.createFromParcel(parcel);
List<Pair<String, Long>> createdFromParcelData = createdFromParcel.getData();

// Verify that the received data is correct.
assertThat(createdFromParcelData.size(), is(1));
assertThat(createdFromParcelData.get(0).first, is(TEST_STRING));
assertThat(createdFromParcelData.get(0).second, is(TEST_LONG));
}
}