Kotlin androidTest:测试 运行 完成。空测试套件
Kotlin androidTest: Tests ran to completion. Empty test suite
我正在尝试将我的测试从 java 翻译成 kotlin。
简单的单元测试翻译成功,像这样:
class BindingUtilsTest {
@Test @Throws(Exception::class)
fun testConvertBooleanToVisibility_visible() {
assertEquals(BindingUtils.convertBooleanToVisibility(true), View.VISIBLE)
}
}
但是当我尝试 运行 androidTest 时它失败并显示消息:"No tests were found" 和
Test running started
Tests ran to completion.
Empty test suite.
代码在 java 中时运行完美。相关代码:
build.gradle 部分:
apply plugin: "com.android.application"
apply plugin: "com.neenbedankt.android-apt"
// for tests
apply plugin: 'kotlin-android'
// defaultConfig
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
sourceSets {
test.java.srcDirs += 'src/test/kotlin' // tests are there
androidTest.java.srcDirs += 'src/androidTest/kotlin' // and there
}
// unit tests
testApt "com.google.dagger:dagger-compiler:${daggerVer}"
// kotlin
testCompile "org.jetbrains.kotlin:kotlin-stdlib:${kotlinVer}"
testCompile "org.jetbrains.kotlin:kotlin-test-junit:${kotlinVer}"
// android tests
androidTestApt "com.google.dagger:dagger-compiler:${daggerVer}"
// kotlin
androidTestCompile "org.jetbrains.kotlin:kotlin-stdlib:${kotlinVer}"
androidTestCompile "org.jetbrains.kotlin:kotlin-test-junit:${kotlinVer}"
简单测试:
@RunWith(AndroidJUnit4::class) class MainDrawerActivityTest {
private val mQuestions = InstrumentationRegistry.getTargetContext().applicationContext as Questions
private val mTestComponentRule = TestComponentRule<Questions, AppComponentTest>(mQuestions,
DaggerAppComponentTest.builder().appModuleTest(AppModuleTest(mQuestions)).build(),
{ obj, component -> obj.setAppComponent(component) }, // set test component
{ objectToClear -> objectToClear.setAppComponent(null) }) // clear test component
private val mActivityTestRule = ActivityTestRule(
MainDrawerActivity::class.java, false, false)
// TestComponentRule needs to go first to make sure the Dagger TestComponent is set
// in the Application before any Activity is launched.
@Rule @JvmField val mRuleChain: TestRule = RuleChain.outerRule(mTestComponentRule).around(mActivityTestRule)
private var mActivity: MainDrawerActivity? = null
@Before @Throws(Exception::class)
fun setUp() {
mActivityTestRule.launchActivity(null)
mActivity = mActivityTestRule.activity
}
@Test @Throws(Exception::class)
fun testOnCreate() {
val size = mActivity!!.supportFragmentManager.fragments.size
// check if fragment instantly added
assertEquals(size.toLong(), 1)
}
}
测试组件在 Kotlin 中:
// Empty because extends ApplicationComponent
@Singleton @Component(modules = arrayOf(
AppModuleTest::class)) interface AppComponentTest : AppComponent
测试模块也在 Kotlin 中:
@Module class AppModuleTest(private val mApp: Questions) /*: AppModule*/ {
@Provides fun provideApp(): Questions {
return mApp
}
}
我什至没有看到 DaggerAppComponentTest 已构建。
为什么我使用 apt 而不是 kapt 进行测试?
因为我有一个错误,我不能在一个项目中混合使用 apt 和 kapt。我试图将 apt 切换到 kapt,但出现了数十亿个错误。
据我了解,kapt 处理 kotlin 文件并使用它生成 kotlin 代码?对于 apt: java 文件,java 代码。如何混合呢?如何解决这个问题?
解决方案
接受的解决方案有效。在那之前,我为 Kotlin 返回了 kapt。 kaptAndroidTest
和 kaptTest
.
改变
@Rule @JvmField val mRuleChain: TestRule = RuleChain.outerRule(mTestComponentRule).around(mActivityTestRule)
到
@get:Rule @JvmField var mRuleChain: TestRule = RuleChain.outerRule(mTestComponentRule).around(mActivityTestRule)
如果不行,说明mRuleChain为空,检查Dagger提供的对象。
我正在尝试将我的测试从 java 翻译成 kotlin。
简单的单元测试翻译成功,像这样:
class BindingUtilsTest {
@Test @Throws(Exception::class)
fun testConvertBooleanToVisibility_visible() {
assertEquals(BindingUtils.convertBooleanToVisibility(true), View.VISIBLE)
}
}
但是当我尝试 运行 androidTest 时它失败并显示消息:"No tests were found" 和
Test running started
Tests ran to completion.Empty test suite.
代码在 java 中时运行完美。相关代码:
build.gradle 部分:
apply plugin: "com.android.application"
apply plugin: "com.neenbedankt.android-apt"
// for tests
apply plugin: 'kotlin-android'
// defaultConfig
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
sourceSets {
test.java.srcDirs += 'src/test/kotlin' // tests are there
androidTest.java.srcDirs += 'src/androidTest/kotlin' // and there
}
// unit tests
testApt "com.google.dagger:dagger-compiler:${daggerVer}"
// kotlin
testCompile "org.jetbrains.kotlin:kotlin-stdlib:${kotlinVer}"
testCompile "org.jetbrains.kotlin:kotlin-test-junit:${kotlinVer}"
// android tests
androidTestApt "com.google.dagger:dagger-compiler:${daggerVer}"
// kotlin
androidTestCompile "org.jetbrains.kotlin:kotlin-stdlib:${kotlinVer}"
androidTestCompile "org.jetbrains.kotlin:kotlin-test-junit:${kotlinVer}"
简单测试:
@RunWith(AndroidJUnit4::class) class MainDrawerActivityTest {
private val mQuestions = InstrumentationRegistry.getTargetContext().applicationContext as Questions
private val mTestComponentRule = TestComponentRule<Questions, AppComponentTest>(mQuestions,
DaggerAppComponentTest.builder().appModuleTest(AppModuleTest(mQuestions)).build(),
{ obj, component -> obj.setAppComponent(component) }, // set test component
{ objectToClear -> objectToClear.setAppComponent(null) }) // clear test component
private val mActivityTestRule = ActivityTestRule(
MainDrawerActivity::class.java, false, false)
// TestComponentRule needs to go first to make sure the Dagger TestComponent is set
// in the Application before any Activity is launched.
@Rule @JvmField val mRuleChain: TestRule = RuleChain.outerRule(mTestComponentRule).around(mActivityTestRule)
private var mActivity: MainDrawerActivity? = null
@Before @Throws(Exception::class)
fun setUp() {
mActivityTestRule.launchActivity(null)
mActivity = mActivityTestRule.activity
}
@Test @Throws(Exception::class)
fun testOnCreate() {
val size = mActivity!!.supportFragmentManager.fragments.size
// check if fragment instantly added
assertEquals(size.toLong(), 1)
}
}
测试组件在 Kotlin 中:
// Empty because extends ApplicationComponent
@Singleton @Component(modules = arrayOf(
AppModuleTest::class)) interface AppComponentTest : AppComponent
测试模块也在 Kotlin 中:
@Module class AppModuleTest(private val mApp: Questions) /*: AppModule*/ {
@Provides fun provideApp(): Questions {
return mApp
}
}
我什至没有看到 DaggerAppComponentTest 已构建。
为什么我使用 apt 而不是 kapt 进行测试?
因为我有一个错误,我不能在一个项目中混合使用 apt 和 kapt。我试图将 apt 切换到 kapt,但出现了数十亿个错误。
据我了解,kapt 处理 kotlin 文件并使用它生成 kotlin 代码?对于 apt: java 文件,java 代码。如何混合呢?如何解决这个问题?
解决方案
接受的解决方案有效。在那之前,我为 Kotlin 返回了 kapt。 kaptAndroidTest
和 kaptTest
.
改变
@Rule @JvmField val mRuleChain: TestRule = RuleChain.outerRule(mTestComponentRule).around(mActivityTestRule)
到
@get:Rule @JvmField var mRuleChain: TestRule = RuleChain.outerRule(mTestComponentRule).around(mActivityTestRule)
如果不行,说明mRuleChain为空,检查Dagger提供的对象。