在Android Studio進行“簡單配置”單元測試(Android Junit)


  1. 起因
    1. 在Android studio 剛出。本人就想弄單元測試,可惜當時Android studio不知道抽什么風(准確來說,應該是我不會弄而已)。無法執行到相應的代碼。
      后來今天突然自己又抽風。又想去弄一下Android junit。
    2. 本文基於做過Eclipse開發使用過Android junit,如果Eclipse的Android Junit沒有使用過,就我沒有說過吧!
  2. 准備環境,配置
    1. 官網Demo地址:https://github.com/googlesamples/android-testing-templates.git
    2. 環境
      1. 根據demo中
        1. 單純想運行java的單元測試就引入
          // Dependencies for local unit tests
          testCompile 'junit:junit:' + rootProject.ext.junitVersion
          testCompile 'org.mockito:mockito-all:' + rootProject.ext.mockitoVersion
          testCompile 'org.hamcrest:hamcrest-all:' + rootProject.ext.hamcrestVersion
        2. 想運行Android的Junit得引入
          // Android Testing Support Library's runner and rules
          androidTestCompile 'com.android.support.test:runner:' + rootProject.ext.runnerVersion
          androidTestCompile 'com.android.support.test:rules:' + rootProject.ext.rulesVersion
      2. 最后在   defaultConfig 節點添加


        defaultConfig {
        testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
        }


      3. 以上配置要配置在“啟動項目中”build.gradle文件中,方可自動關聯上啟動項目

        1. Android Studio本來啟動項目中的測試代碼分為兩個目錄
        2. 如果是通過更改“sourceSets”的話。那就要得重新配置注明相應的文件夾(這是Eclipse轉AS導出的配置)
          sourceSets{
          sourceSets{
          main{
          java.srcDirs = ['src']
          }
          androidTest{
          java.srcDirs = ['androidTest/src']
          }
          test{
          java.srcDirs = ['test/src']
          }
          }
        3. 最后就把相應的文件放入具體目錄
          1. androidTest  “Android的Junit”
          2. test “java的單元測試”
  3. 代碼
    1. junit
      public class ExampleUnitTest {
      @Test
      public void addition_isCorrect() throws Exception {
      System.out.println("123");

      }
      }
    2. Android junit
      public class AndroidRuntimeCodeTest extends AndroidTestCase {

      public void testHello() throws Exception {
      System.out.println("testHello");
      }
      }
  4. 結論
    1. 注意事項
      1. 以前Eclipse做單元測試得往AndroidManifest.xml標明

        1. <manifest >
          <instrumentation
                  android:name="android.test.InstrumentationTestRunner"
                  android:targetPackage="com.example.viewtest" />
          </manifest>
        2. <application>
          <uses-library android:name="android.test.runner" />
          </application>
      2. Android studio就要在build.gradle
        1. 導入相應的包

        2. android{
              defaultConfig {
                  testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
              }
          }
    2. 還有一點:就是“測試使用的需要的包”遇到與“啟動項目的包”沖突時,使用
      configurations.all {
      resolutionStrategy {
      androidTestCompile 'com.android.support.test:runner:0.4.1'
      androidTestCompile 'com.android.support.test:rules:0.4.1'

      forcedModules = ['com.android.support:support-annotations:23.0.1']

      }
      }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM