我使用Kotlin作为开发语言,我使用过模仿来编写单元测试用例。当我运行测试时,测试按预期通过。但是,当我尝试使用代码覆盖率运行测试时,我无法在android studio中查看任何代码覆盖率报告。它显示消息为“'作用域中的所有类中都没有覆盖率'”。
以下是我的Gradle设置:
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
testCoverageEnabled true
}
}
dependencies {
def lifecycle_version = "1.1.1"
// ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:$lifecycle_version"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
// required if you want to use Mockito for unit tests
testImplementation 'org.mockito:mockito-core:2.24.5'
// required if you want to use Mockito for Android tests
androidTestImplementation 'org.mockito:mockito-android:2.24.5'
}
我的TestCase类如下:
package com.example.testcasesdemo
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.mockito.MockitoAnnotations
import org.mockito.junit.MockitoJUnitRunner
@RunWith(JUnit4::class)
class LoginViewModelTest {
lateinit var loginViewModel: LoginViewModel
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
this.loginViewModel = LoginViewModel()
}
@Test
fun validatePasswordTest() {
loginViewModel.validatePassword("abc")
assertNotNull(loginViewModel.isPassWordValid)
assertEquals("Enter Valid Password",loginViewModel.isPassWordValid?.get().toString())
loginViewModel.validatePassword("abcdefghij")
assertNotNull(loginViewModel.isPassWordValid)
assertEquals("",loginViewModel.isPassWordValid?.get().toString())
}
@Test
fun validateUserEmailTest(){
loginViewModel.validateEmail("a@b.com")
assertNotNull(loginViewModel.isuserEmailValid)
assertEquals("Enter Valid Email",loginViewModel.isuserEmailValid?.get().toString())
loginViewModel.validateEmail("a@bcdfefghi.com")
assertNotNull(loginViewModel.isuserEmailValid)
assertEquals("",loginViewModel.isuserEmailValid?.get().toString())
}
}
我的ViewModel如下:
package com.example.testcasesdemo
import android.arch.lifecycle.MutableLiveData
import android.arch.lifecycle.ViewModel
import android.databinding.ObservableBoolean
import android.databinding.ObservableField
import android.util.Log
class LoginViewModel:ViewModel() {
var password: ObservableField<String>? = null
var isPassWordValid:ObservableField<String>? = null
var userEmail: ObservableField<String>? = null
var isuserEmailValid:ObservableField<String>? = null
var btnSelected: ObservableBoolean? = null
init {
password = ObservableField("")
isPassWordValid = ObservableField("")
userEmail = ObservableField("")
isuserEmailValid = ObservableField("")
btnSelected = ObservableBoolean(false)
}
fun onPasswordTextChanged(s: CharSequence,start: Int,before : Int,
count :Int){
validatePassword(s)
}
fun validatePassword(s:CharSequence){
if(s.length == 10){
password?.set(s.toString())
isPassWordValid?.set("")
btnSelected?.set(true)
}else{
password?.set(s.toString())
isPassWordValid?.set( "Enter Valid Password")
}
}
fun onEmailTextChanged(s: CharSequence,start: Int,before : Int,
count :Int){
validateEmail(s)
}
fun validateEmail(s:CharSequence){
if(s.length > 10){
userEmail?.set(s.toString())
isuserEmailValid?.set("")
btnSelected?.set(false)
}else{
userEmail?.set(s.toString())
isuserEmailValid?.set( "Enter Valid Email")
}
}
}
我还对EditConfiguration进行了更改,以配置我的TestClass。
谁能帮我生成代码覆盖率?