{"id":774,"date":"2023-02-26T21:46:42","date_gmt":"2023-02-26T13:46:42","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=774"},"modified":"2023-04-29T16:56:54","modified_gmt":"2023-04-29T08:56:54","slug":"android-test-instrumented-unit-test","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/26\/android-test-instrumented-unit-test\/","title":{"rendered":"Android\u6d4b\u8bd5\uff085\uff09\uff1aInstrumented\u5355\u5143\u6d4b\u8bd5"},"content":{"rendered":"<p>\u539f\u6587\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/developer.android.com\/training\/testing\/unit-testing\/instrumented-unit-tests.html\">https:\/\/developer.android.com\/training\/testing\/unit-testing\/instrumented-unit-tests.html<\/a><\/p>\n<p>Instrumented \u5355\u5143\u6d4b\u8bd5\u662f\u5728\u771f\u673a\u548c\u6a21\u62df\u5668\u4e0a\u8fd0\u884c\u7684\u6d4b\u8bd5\uff0c\u5b83\u53ef\u4ee5\u5229\u7528Android\u6846\u67b6API\u548c\u652f\u6301\u7684API\uff08\u5982Android\u6d4b\u8bd5\u652f\u6301\u5e93\uff09\u3002\u5982\u679c\u6d4b\u8bd5\u9700\u8981\u8bbf\u95ee\u5de5\u5177\u4fe1\u606f\uff08\u4f8b\u5982\u76ee\u6807\u5e94\u7528\u7a0b\u5e8f\u7684<code>Context<\/code>\uff09\uff0c\u6216\u8005\u9700\u8981\u771f\u6b63\u5b9e\u73b0Android\u6846\u67b6\u7ec4\u4ef6\uff08\u5982<code>Parcelable<\/code>\u6216<code>SharedPreferences<\/code>\u5bf9\u8c61\uff09\uff0c\u5219\u5e94\u8be5\u521b\u5efaInstrumented \u5355\u5143\u6d4b\u8bd5\u3002<\/p>\n<p><!-- more --><\/p>\n<p>\u4f7f\u7528Instrumented\u5355\u5143\u6d4b\u8bd5\u8fd8\u6709\u52a9\u4e8e\u51cf\u5c11\u7f16\u5199\u548c\u7ef4\u62a4mock\u4ee3\u7801\u6240\u9700\u7684\u5de5\u4f5c\u91cf\u3002\u5982\u679c\u4f60\u613f\u610f\uff0c\u4ecd\u7136\u53ef\u4ee5\u81ea\u7531\u5730\u4f7f\u7528\u4e00\u4e2amock\u6846\u67b6\u6a21\u62df\u4efb\u4f55\u4f9d\u8d56\u5173\u7cfb\u3002<\/p>\n<h2>\u914d\u7f6e\u6d4b\u8bd5\u73af\u5883<\/h2>\n<p>\u5728Android Studio\u9879\u76ee\u4e2d\uff0c\u4f60\u5fc5\u987b\u5c06mock\u6d4b\u8bd5\u7684\u6e90\u6587\u4ef6\u5b58\u50a8\u5728module-name\/src\/androidTest\/java\/ \u4e2d\u3002 \u521b\u5efa\u65b0\u9879\u76ee\u65f6\u8be5\u76ee\u5f55\u5df2\u7ecf\u5b58\u5728\uff0c\u5e76\u5305\u542b\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u9700\u8981\u4e0b\u8f7dAndroid\u6d4b\u8bd5\u652f\u6301\u5e93\u5b89\u88c5\u7a0b\u5e8f\uff0c\u8be5\u5b89\u88c5\u7a0b\u5e8f\u63d0\u4f9b\u7684API\u53ef\u4ee5\u5feb\u901f\u6784\u5efa\u548c\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u7684\u68c0\u6d4b\u4ee3\u7801\u3002\u6d4b\u8bd5\u652f\u6301\u5e93\u5305\u62ec\u7528\u4e8e\u529f\u80fd\u6027UI\u6d4b\u8bd5\uff08Espresso\u548cUI Automator\uff09\u7684JUnit 4\u6d4b\u8bd5\u8fd0\u884c\u5668\uff08AndroidJUnitRunner\uff09\u548cAPI\u3002<\/p>\n<p>\u8fd8\u9700\u8981\u4e3a\u9879\u76ee\u914d\u7f6eAndroid\u6d4b\u8bd5\u4f9d\u8d56\u9879\uff0c\u4ee5\u4f7f\u7528\u6d4b\u8bd5\u8fd0\u884c\u7a0b\u5e8f\u548c\u6d4b\u8bd5\u652f\u6301\u5e93\u63d0\u4f9b\u7684\u89c4\u5219API\u3002\u4e3a\u4e86\u7b80\u5316\u6d4b\u8bd5\u5f00\u53d1\uff0c\u8fd8\u5e94\u8be5\u5305\u542bHamcrest\u5e93\uff0c\u5b83\u53ef\u4ee5\u8ba9\u4f60\u4f7f\u7528Hamcrest\u5339\u914d\u5668API\u521b\u5efa\u66f4\u7075\u6d3b\u7684\u65ad\u8a00\u3002<\/p>\n<p>\u5728App\u6a21\u5757\u7684build.gradle\u4e2d\u6dfb\u52a0\u4f9d\u8d56\u9879\uff1a<\/p>\n<pre><code>dependencies {\n    implementation &#039;com.android.support:support-annotations:28.0.0-alpha1&#039;\n    testImplementation &#039;junit:junit:4.12&#039;\n    androidTestImplementation &#039;com.android.support.test:runner:1.0.1&#039;\n    androidTestImplementation &#039;com.android.support.test:rules:1.0.1&#039;\n    \/\/ Optional -- Hamcrest library\n    androidTestImplementation &#039;org.hamcrest:hamcrest-library:1.3&#039;\n    \/\/ Optional -- UI testing with Espresso\n    androidTestImplementation &#039;com.android.support.test.espresso:espresso-core:3.0.1&#039;\n    \/\/ Optional -- UI testing with UI Automator\n    androidTestImplementation &#039;com.android.support.test.uiautomator:uiautomator-v18:2.1.3&#039;\n    testImplementation &#039;org.mockito:mockito-core:2.18.0&#039;\n}<\/code><\/pre>\n<blockquote>\n<p>\u8b66\u544a\uff1a\u5982\u679c\u6784\u5efa\u914d\u7f6e\u5305\u542bsupport-annotations\u5e93\u7684\u7f16\u8bd1\u4f9d\u8d56\u9879\u548cespresso-core\u5e93\u7684androidTestCompile\u4f9d\u8d56\u9879\uff0c\u5219\u7531\u4e8e\u4f9d\u8d56\u51b2\u7a81\uff0c\u6784\u5efa\u53ef\u80fd\u4f1a\u5931\u8d25\u3002\u8bf7\u6309\u7167\u4e0b\u9762\u6b65\u9aa4\u66f4\u65b0\u5bf9espresso-core\u7684\u4f9d\u8d56\u5173\u7cfb\uff1a<\/p>\n<\/blockquote>\n<pre><code>androidTestCompile(&#039;com.android.support.test.espresso:espresso-core:2.2.2&#039;, {\n    exclude group: &#039;com.android.support&#039;, module: &#039;support-annotations&#039;\n})<\/code><\/pre>\n<p>\u8981\u4f7f\u7528JUnit 4\u6d4b\u8bd5\u7c7b\uff0c\u8bf7\u786e\u4fdd\u5c06AndroidJUnitRunner\u6307\u5b9a\u4e3a\u9879\u76ee\u4e2d\u7684\u9ed8\u8ba4\u6d4b\u8bd5\u5de5\u5177\u8fd0\u884c\u5668\uff0c\u65b9\u6cd5\u662f\u5728\u5e94\u7528\u7a0b\u5e8f\u7684\u6a21\u5757\u7ea7build.gradle\u6587\u4ef6\u4e2d\u5305\u542b\u4ee5\u4e0b\u8bbe\u7f6e\uff1a<\/p>\n<pre><code>android {\n    defaultConfig {\n        testInstrumentationRunner &quot;android.support.test.runner.AndroidJUnitRunner&quot;\n    }\n}<\/code><\/pre>\n<h2>\u521b\u5efa\u4e00\u4e2aInstrumented\u7684\u5355\u5143\u6d4b\u8bd5\u7c7b<\/h2>\n<p>Instrumented\u5355\u5143\u6d4b\u8bd5\u7c7b\u5e94\u8be5\u5199\u6210JUnit 4\u6d4b\u8bd5\u7c7b\u3002\u8981\u4e86\u89e3\u6709\u5173\u521b\u5efaJUnit 4\u6d4b\u8bd5\u7c7b\u548c\u4f7f\u7528JUnit 4\u65ad\u8a00\u548c\u6ce8\u91ca\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605\u521b\u5efa\u672c\u5730\u5355\u5143\u6d4b\u8bd5\u7c7b\u3002<\/p>\n<p>\u8981\u521b\u5efa\u4e00\u4e2aInstrumented\u7684JUnit 4\u6d4b\u8bd5\u7c7b\uff0c\u5728\u6d4b\u8bd5\u7c7b\u5b9a\u4e49\u7684\u5f00\u5934\u6dfb\u52a0<a target=\"_blank\" rel=\"noopener\" href=\"mailto:code&gt;@RunWith(AndroidJUnit4.class)&lt;\/code\">code>@RunWith(AndroidJUnit4.class)<\/code<\/a>\u6ce8\u91ca\u3002 \u8fd8\u9700\u8981\u5c06Android\u6d4b\u8bd5\u652f\u6301\u5e93\u4e2d\u63d0\u4f9b\u7684AndroidJUnitRunner\u7c7b\u6307\u5b9a\u4e3a\u9ed8\u8ba4\u6d4b\u8bd5\u8fd0\u884c\u5668\u3002\u6d4b\u8bd5\u5165\u95e8\u4e2d\u5bf9\u6b64\u6b65\u9aa4\u8fdb\u884c\u4e86\u66f4\u8be6\u7ec6\u7684\u4ecb\u7ecd\u3002<\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u663e\u793a\u5982\u4f55\u7f16\u5199\u4e00\u4e2aInstrumented\u5355\u5143\u6d4b\u8bd5\uff0c\u4ee5\u786e\u4fddLogHistory\u7c7b\u6b63\u786e\u5b9e\u73b0\u4e86Parcelable\u63a5\u53e3\uff1a<\/p>\n<pre><code>import android.os.Parcel;\nimport android.support.test.filters.SmallTest;\nimport android.support.test.runner.AndroidJUnit4;\nimport android.util.Pair;\n\nimport org.hamcrest.core.Is;\nimport org.junit.Assert;\nimport org.junit.Before;\nimport org.junit.Test;\nimport org.junit.runner.RunWith;\n\nimport java.util.List;\n\n@RunWith(AndroidJUnit4.class)\n@SmallTest\npublic class LogHistoryAndroidUnitTest {\n\n    public static final String TEST_STRING = &quot;This is a string&quot;;\n    public static final long TEST_LONG = 12345678L;\n    private LogHistory mLogHistory;\n\n    @Before\n    public void createLogHistory() {\n        mLogHistory = new LogHistory();\n    }\n\n    @Test\n    public void logHistory_ParcelableWriteRead() {\n        \/\/ Set up the Parcelable object to send and receive.\n        mLogHistory.addEntry(TEST_STRING, TEST_LONG);\n\n        \/\/ Write the data.\n        Parcel parcel = Parcel.obtain();\n        mLogHistory.writeToParcel(parcel, mLogHistory.describeContents());\n\n        \/\/ After you&#039;re done with writing, you need to reset the parcel for reading.\n        parcel.setDataPosition(0);\n\n        \/\/ Read the data.\n        LogHistory createdFromParcel = LogHistory.CREATOR.createFromParcel(parcel);\n        List&lt;Pair&lt;String, Long&gt;&gt; createdFromParcelData = createdFromParcel.getData();\n\n        \/\/ Verify that the received data is correct.\n        Assert.assertThat(createdFromParcelData.size(), Is.is(1));\n        Assert.assertThat(createdFromParcelData.get(0).first, Is.is(TEST_STRING));\n        Assert.assertThat(createdFromParcelData.get(0).second, Is.is(TEST_LONG));\n    }\n}<\/code><\/pre>\n<h2>\u521b\u5efa\u4e00\u4e2a\u6d4b\u8bd5\u5957\u4ef6<\/h2>\n<p>\u8981\u7ec4\u7ec7\u6d4b\u8bd5\u5355\u5143\u6d4b\u8bd5\u7684\u6267\u884c\uff0c\u53ef\u4ee5\u5c06\u4e00\u7ec4\u6d4b\u8bd5\u96c6\u5408\u5728\u4e00\u4e2a\u6d4b\u8bd5\u5957\u4ef6\u7c7b\u4e2d\uff0c\u5e76\u5c06\u8fd9\u4e9b\u6d4b\u8bd5\u4e00\u8d77\u8fd0\u884c\u3002\u6d4b\u8bd5\u5957\u4ef6\u53ef\u4ee5\u5d4c\u5957\uff1b\u6d4b\u8bd5\u5957\u4ef6\u53ef\u4ee5\u5c06\u5176\u4ed6\u6d4b\u8bd5\u5957\u4ef6\u5206\u7ec4\uff0c\u5e76\u5c06\u6240\u6709\u7ec4\u4ef6\u6d4b\u8bd5\u7c7b\u4e00\u8d77\u8fd0\u884c\u3002<\/p>\n<p>\u6d4b\u8bd5\u5957\u4ef6\u5305\u542b\u5728\u6d4b\u8bd5\u5305\u4e2d\uff0c\u7c7b\u4f3c\u4e8e\u4e3b\u5e94\u7528\u7a0b\u5e8f\u5305\u3002\u6309\u7167\u60ef\u4f8b\uff0c\u6d4b\u8bd5\u5957\u4ef6\u5305\u540d\u901a\u5e38\u4ee5.suite\u540e\u7f00\u7ed3\u5c3e\uff08\u4f8b\u5982\uff0ccom.example.android.testing.mysample.suite\uff09\u3002<\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u663e\u793a\u5982\u4f55\u5b9e\u73b0\u540d\u4e3aUnitTestSuite\u7684\u6d4b\u8bd5\u5957\u4ef6\uff0c\u8be5\u6d4b\u8bd5\u5957\u4ef6\u5c06<code>CalculatorInstrumentationTest<\/code>\u548c<code>CalculatorAddParameterizedTest<\/code>\u6d4b\u8bd5\u7c7b\u5206\u7ec4\u5e76\u8fd0\u884c\u5728\u4e00\u8d77\u3002<\/p>\n<pre><code>import com.example.android.testing.mysample.CalculatorAddParameterizedTest;\nimport com.example.android.testing.mysample.CalculatorInstrumentationTest;\nimport org.junit.runner.RunWith;\nimport org.junit.runners.Suite;\n\n\/\/ Runs all unit tests.\n@RunWith(Suite.class)\n@Suite.SuiteClasses({CalculatorInstrumentationTest.class,\n        CalculatorAddParameterizedTest.class})\npublic class UnitTestSuite {}<\/code><\/pre>\n<h2>\u8fd0\u884cInstrumented\u5355\u5143\u6d4b\u8bd5<\/h2>\n<p>\u8981\u8fd0\u884cInstrumented\u6d4b\u8bd5\uff0c\u8bf7\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4:<\/p>\n<p>1\u3001\u901a\u8fc7\u5355\u51fb\u5de5\u5177\u680f\u4e2d\u7684\u201cSync Project\u201d\uff0c\u786e\u4fdd\u60a8\u7684\u9879\u76ee\u4e0eGradle\u540c\u6b65\u3002<\/p>\n<p>2\u3001\u4ee5\u4e0b\u5217\u5176\u4e2d\u4e00\u79cd\u65b9\u5f0f\u8fd0\u884c\u6d4b\u8bd5\uff1a<\/p>\n<ul>\n<li>\u8981\u8fd0\u884c\u5355\u4e2a\u6d4b\u8bd5\u8bf7\u6253\u5f00Project\u7a97\u53e3\uff0c\u7136\u540e\u5355\u51fb\u201cRun\u201d\u3002<\/li>\n<li>\u8981\u6d4b\u8bd5\u7c7b\u4e2d\u7684\u6240\u6709\u65b9\u6cd5\uff0c\u8bf7\u53f3\u952e\u5355\u51fb\u6d4b\u8bd5\u6587\u4ef6\u4e2d\u7684\u7c7b\u6216\u65b9\u6cd5\uff0c\u7136\u540e\u5355\u51fb\u201cRun\u201d\u3002<\/li>\n<li>\u8981\u5728\u76ee\u5f55\u4e2d\u8fd0\u884c\u6240\u6709\u6d4b\u8bd5\uff0c\u8bf7\u53f3\u952e\u5355\u51fb\u8be5\u76ee\u5f55\u5e76\u9009\u62e9\u201cRun Tests\u201d\u3002<\/li>\n<li>Gradle\u7684Android\u63d2\u4ef6\u7f16\u8bd1\u4f4d\u4e8e\u9ed8\u8ba4\u76ee\u5f55\uff08src\/androidTest\/java\/\uff09\u4e2d\u7684\u6d4b\u8bd5\u4ee3\u7801\uff0c\u6784\u5efa\u6d4b\u8bd5APK\u548c\u751f\u4ea7APK\uff0c\u5728\u8fde\u63a5\u7684\u771f\u673a\u6216\u6a21\u62df\u5668\u4e0a\u5b89\u88c5\u4e24\u4e2aAPK\uff0c\u5e76\u8fd0\u884c\u6d4b\u8bd5\u3002Android Studio\u7136\u540e\u5728\u201cRun\u201d\u7a97\u53e3\u4e2d\u663e\u793a\u6d4b\u8bd5\u6267\u884c\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<blockquote>\n<p>\u6ce8\u610f\uff1a\u5728\u8fd0\u884c\u6216\u8c03\u8bd5\u6d4b\u8bd5\u5de5\u5177\u65f6\uff0cAndroid Studio\u4e0d\u4f1a\u4e3a\u5373\u65f6\u8fd0\u884c\u6ce8\u5165\u6240\u9700\u7684\u989d\u5916\u65b9\u6cd5\uff0c\u5e76\u5173\u95ed\u8be5\u7279\u6027\u3002<\/p>\n<\/blockquote>\n<h2>\u4f7f\u7528Firebase\u6d4b\u8bd5\u5b9e\u9a8c\u5ba4\u8fd0\u884c\u6d4b\u8bd5<\/h2>\n<p>\u7565\u2026.<\/p>\n<h2>\u989d\u5916\u7684\u793a\u4f8b\u4ee3\u7801<\/h2>\n<p>\u8981\u4e0b\u8f7d\u5230\u793a\u4f8b\u5e94\u7528\u7a0b\u5e8f\uff0c\u8bf7\u53c2\u9605<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/googlesamples\/android-ActivityInstrumentation\/\" title=\"Android ActivityInstrumentation Sample\">Android ActivityInstrumentation Sample<\/a>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\uff1ahttps:\/\/developer.android.com\/training\/testing\/unit- [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[222],"tags":[],"class_list":["post-774","post","type-post","status-publish","format-standard","hentry","category-android-test"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/774","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=774"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/774\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=774"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=774"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=774"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}