kotlin学習ノート——セルテスト


Kotlin学習ノートシリーズ:http://blog.csdn.net/column/details/16696.html
ユニットTest
KOtlinはunit testingもできます。プロジェクトの中で前になかったら、いくつかの準備が必要です。
最初に依存を導入
testComple'junnit:junnit:4.12'
ここで注意します
android TestCompleではいけません。そうでないとUnirevoved reference:xxxを間違えます。
ディレクトリを作成します
Srcディレクトリの下でtest/javaディレクトリを作成します。作成したらJavaディレクトリの色は自動的に緑色です。ガイドがunit testingモードを使うと知っています。
javaディレクトリでpackageを作成します。
テストコードの作成
packageでテストクラスのコードを作成すればいいです。例えば、
import org.junnit.Test
import kotlin.test.astert True
クラス 
SimpleTest {
 @Test 
ふうふ 
unit TestingWorks() {
     asert True
true
 }
)
実行します
Instruction 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ではなく、android Testです。他は同じです。
テストコードの作成
import android.support.test.espress o.Espreso.onView
import android.support.test.espress.actions.Viewアクション.click
import android.support.test.espress.astertions.View Asertions.matches
import android.support.test.espress.co ntrib.RecyclerViewアクション
import android.support.test.espress.matcher.ViewMatch s.isAsignable From
import android.support.test.espress.matcher.ViewMatch.withId
import android.support.test.rule.Activity TestRule
import android.support.v 7.widget.RecyclerView
import android.widget.TextView
import org.junnit.
Rule
import org.junnit.
Test
クラス Simple ActivityTest {
   
@get:Rule
    val 
activity = ActivityTestRule(MainActivity:
クラス.
java
   
@Test ふうふ 
testItem(){
        オンビュー(withId(R.id.
recyclerview).perform(RecyclerViewアクション.actionOnItemAtPosition(
0
を選択します。 click()
        オンビュー(withId(R.id.
textView).check(matches(isAsignableFrom(TextView:
クラス.
java)
    }
)
testitemの最初の行のコードはrecyclerviewをアナログでクリックした最初のitemです。2行目はidがtextviewのコンポーネントであるかどうかを判断します。