日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

kotlin学习笔记——单元测试

發布時間:2024/4/15 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 kotlin学习笔记——单元测试 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Unit Test

kotlin也可以進行unit testing,如果項目中之前沒有,那么需要做一些準備工作。 首先引入依賴 testCompile 'junit:junit:4.12' 這里注意不能是androidTestCompile,否則會報錯Unresolved reference: xxxx 然后創建目錄 在src目錄下(main的同級)創建test/java目錄,創建完會發現java目錄的顏色自動為綠色,表示ide知道我們要使用unit testing模式。 在java目錄下創建package(與項目主包名一致) 創建測試代碼 在package下創建測試類編寫代碼即可,例如: import?org.junit.Test import?kotlin.test.assertTrue class?SimpleTest?{@Test?fun?unitTestingWorks()?{assertTrue(true)} } 運行即可

Instrumentation Test

與unit testing一樣,首先引入依賴 defaultConfig {...testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" }androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {exclude group: 'com.android.support', module: 'support-annotations' }) androidTestCompile ("com.android.support.test.espresso:espresso-contrib:2.2.1"){exclude group: 'com.android.support', module: 'appcompat'exclude group: 'com.android.support', module: 'support-v4'exclude group: 'com.android.support', module: 'support-annotations'exclude module: 'recyclerview-v7' }

exclude去掉一些依賴,防止重復引入 (contrib這個增加了一些額外功能,比如測試recyclerview) 然后創建目錄,與unit一樣,只不過根目錄不是test而是androidTest,其他一樣。 創建測試代碼 import?android.support.test.espresso.Espresso.onView import?android.support.test.espresso.action.ViewActions.click import?android.support.test.espresso.assertion.ViewAssertions.matches import?android.support.test.espresso.contrib.RecyclerViewActions import?android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom import?android.support.test.espresso.matcher.ViewMatchers.withId import?android.support.test.rule.ActivityTestRule import?android.support.v7.widget.RecyclerView import?android.widget.TextView import?org.junit.Rule import?org.junit.Testclass?SimpleActivityTest?{@get:Ruleval?activity?=?ActivityTestRule(MainActivity::class.java)@Test?fun?testItem(){onView(withId(R.id.recyclerview)).perform(RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(0,?click()))onView(withId(R.id.textView)).check(matches(isAssignableFrom(TextView::class.java)))} }

testitem中第一行代碼是模擬點擊recyclerview的第一個item。第二行是判斷id是textview的組件是否是TextView。

?

總結

以上是生活随笔為你收集整理的kotlin学习笔记——单元测试的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。