本文是翻译的Test apps on Android的官方文档 Build instrumented unit tests
本文不照搬每一个单词,理解有误请跳转原文链接。
Build instrumented unit tests
构建仪器化的单元测试。
Instrumented unit tests依赖于真实设备或者模拟器运行,它可以利用Android framework API和Supporting API。
Instrumented unit tests 比 local unit test 提供了更高的保真度,但是运行速度比较慢。
因此,建议只在必须针对真实设备行为进行测试的情况下,才使用仪器化测试,AndroidX Test提供了很好的支持。
Set up your testing environment
在Android studio project中,必须将instrumented unit tests的类文件放置在module-name
/src/androidTest/java/ 中。
AndroidX Test APIs包括,JUnit 4 test runner ( AndroidJUnitRunner) 和API for functional UI tests (Espresso and UI Automator)。
在app/build.gradle中配置依赖库:
dependencies {
//AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test:rules:1.1.0'
// Optional -- Hamcrest library, assert matcher
androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'
// Optional -- UI testing with Espresso
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
// Optional -- UI testing with UI Automator
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
}
在使用Junit 4时,需要明确指定AndroidJUnitRunner为默认的 test instrumentation runner
:
android {
defaultConfig {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
}
Create an instrumented unit test class
instrumented单元测试的类和local单元测试类似,See ➡️。
创建一个基于JUnit 4的instrumented unit test,指定了AndroidJUnit4作为默认的test runner。
因此,不需要在类开头添加 @RunWith(AndroidJUnit4.class)
🌟Note:如果你是基于JUnit 3和Junit 4的混合库,那么需要加上@RunWith(AndroidJUnit4.class)
Sample code snippet:
一个用于验证Parcelable的实现是否正确的测试。
import android.os.Parcel;
import android.util.Pair;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;
import com.google.common.truth.Truth.assertThat;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
// @RunWith is required only if you use a mix of JUnit3 and JUnit4.
@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()).isEqualTo(1);
assertThat(createdFromParcelData.get(0).first).isEqualTo(TEST_STRING);
assertThat(createdFromParcelData.get(0).second).isEqaulTo(TEST_LONG);
}
}
Create a test suite
可以将一组测试类归为一个测试套件,然后一起运行这些测试。
一个测试套件类也位于测试包中,其包名通常以 .suite 结尾,例如:com.example.android.testing.mysample.suite
Sample code snippet:
import com.example.android.testing.mysample.CalculatorAddParameterizedTest;
import com.example.android.testing.mysample.CalculatorInstrumentationTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
// Runs all unit tests.
@RunWith(Suite.class)
@Suite.SuiteClasses({CalculatorInstrumentationTest.class,
CalculatorAddParameterizedTest.class})
public class UnitTestSuite {}
Run instrumented unit tests
To run your instrumented unit tests, follow these steps:
-
Be sure your project is synchronized with Gradle by clicking Sync Project in the toolbar.
-
Run your test in one of the following ways:
- To run a single test, open the Project window, and then right-click a test and click Run
- To test all methods in a class, right-click a class or method in the test file and click Run
- To run all tests in a directory, right-click on the directory and select Run tests
Android Gradle插件会编译默认目录(src/androidTest/java/)中的仪器化单元测试代码,生成一个测试APK和生产APK,并安装在所连接的设备或者模拟器上,然后运行测试。最后,Android Studio在“ Run” 窗口中显示结果。
Firebase Test Lab
emmm,we don’t f**k need it😓!