Tag: junit

Android Studio + Spek集成

我正在尝试将Spek测试框架添加到我的Android Studio项目中。 按照说明在这里 ,我结束了以下添加到我的模块build.gradle : testCompile ‘org.jetbrains.spek:spek-api:1.1.5’ testCompile ‘junit:junit:4.12’ testCompile “org.junit.platform:junit-platform-runner:1.0.0” testRuntimeOnly ‘org.jetbrains.spek:spek-junit-platform-engine:1.1.5’ 然后我用@RunWith(JUnitPlatform::class)注释了我的测试。 但是,当我尝试运行测试,我得到: org.junit.platform.commons.util.PreconditionViolationException: Cannot create Launcher without at least one TestEngine; consider adding an engine implementation JAR to the classpath org.junit.platform.commons.util.PreconditionViolationException: Cannot create Launcher without at least one TestEngine; consider adding an engine implementation JAR to the classpath 任何想法我错过了什么?

未解决的参考:junit

我正在使用Kotlin和LibGDX框架编写游戏。 我新来测试。 我已经通过一些基本的教程如何创建简单的测试。 以及如何配置gradle。 我只是点了课,选择创建测试。 但是,当我尝试建立项目,我得到一个错误: e: /Users/maximternovtsi/bagel/core/src/test/test/BagelTest.kt: (1, 12): Unresolved reference: junit e: /Users/maximternovtsi/bagel/core/src/test/test/BagelTest.kt: (2, 12): Unresolved reference: junit e: /Users/maximternovtsi/bagel/core/src/test/test/BagelTest.kt: (6, 6): Unresolved reference: Test e: /Users/maximternovtsi/bagel/core/src/test/test/BagelTest.kt: (8, 9): Unresolved reference: Assertions e: /Users/maximternovtsi/bagel/core/src/test/test/BagelTest.kt: (11, 6): Unresolved reference: Test e: /Users/maximternovtsi/bagel/core/src/test/test/BagelTest.kt: (13, 9): Unresolved reference: Assertions FAILURE: Build failed with an exception. * […]

@Rule>在Kotlin Junit测试中必须是公共的ValidationError

我试图使用unit testing规则注释,Android Studio没有突出显示任何错误: @Rule val htmlManager = HtmlManager() 然而在执行测试之后,发生以下错误: org.junit.internal.runners.rules.ValidationError:@Rule“htmlManager”必须是公共的。 如何解决这个问题?

Gradle错误 – 当我在单个Kotlin文件或特定测试函数上运行unit testing时

在Android Studio ,当我只运行Kotlinunit testing文件或特定测试函数时, Gradde Build失败,但是当我运行文件的父文件夹时,一切正常。 错误: Gradle : failed to create directory d:\…app\build\generated\source\r\debug\…’ 如果我已经启用Kotlin创建一个新的项目,我可以运行测试! 谢谢

Gradle在Kotlin和JUnit 5中找不到我的测试

我已经用Kotlin和Junit 5写了一些基本的unit testing。不幸的是,当我从Intellij IDEA运行它们时,Gradle没有find这些测试。 我收到来自Gradle的消息“没有find给定的测试包括:[de.mhaug.phd.btcwallet.BasicTests]”和来自Intellij的消息“未收到测试事件”。 有趣的是,使用“./gradlew:clean:test”从命令行运行它报告一个成功的构建。 不过,我的第一个测试显然是红色的,所以这表明Gradle没有执行它。 我已经尝试运行它更详细的输出,但没有任何帮助出现。 这是一个最小的(不)工作的例子: package de.mhaug.phd.btcwallet import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertFalse import org.junit.jupiter.api.Test class BasicTests { @Test fun hey() { assertEquals(3,1+1) } @Test fun hey2() { assertFalse(3==1+1) } } 这是我的build.gradle: buildscript { ext.kotlin_version = ‘1.2.10’ repositories { mavenCentral() } dependencies { classpath “org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version” } } group ‘de.mhaug.phd’ version ‘1.0-SNAPSHOT’ apply plugin: […]

即使在androidTest包中,Kotlin类也会作为junit测试运行 – 对于Java类,它运行正常

我遇到了非常奇怪的错误。 当我尝试运行测试,在我的Kotlin类的androidTest包,他们正在运行测试junit mehthods,并出现此错误: 处理完成退出代码1未find类:“com.someampp.shoppinglistapp.SomeClassTest”空测试套件。 你可以自己尝试。 我正在使用Android Studio 3.0.1 当我在Java中创建这样的类时: @RunWith(AndroidJUnit4.class) public class SomeTestClass{ @Test public void useAppContext() throws Exception { // Context of the app under test. Context appContext = InstrumentationRegistry.getTargetContext(); assertEquals(“com.myapp.shoppinglistapp”, appContext.getPackageName()); } } 一切工作正常。 但是,当我将Java文件转换为Kotlin: @RunWith(AndroidJUnit4::class) class SomeTestClass{ @Test @Throws(Exception::class) fun useAppContext() { // Context of the app under test. val appContext = […]

使用JUnit运行时,Intellij IDEA不会停止在Kotlin断点处

当试图执行一些Kotlin代码,同时也使用JUnit时,Intellij IDEA将执行代码直到结束,而不是停在断点处。 演示: class Tester { @Test fun shouldBreakpoint() { //Line where threads should suspend: println(“Should Suspend Here”) //Breakpoint added to this line println(“Shouldn’t run this code unless I release above breakpoint”) } } 当点击“Debug Tester”或“Debug shouldBreakpoint”时,不会有断点。 控制台输出两个打印行,没有停在断点处。 如果使用Java编写相同的代码,则调试器将工作: public class Testerino { @Test public void shouldBreakpoint() { System.out.println(“Should Suspend Here”); //Breakpoint added to this […]

如何管理Kotlin中的unit testing资源,如启动/停止数据库连接或嵌入式弹性搜索服务器?

在我的Kotlin JUnit测试中,我想启动/停止嵌入式服务器,并在我的测试中使用它们。 我试过在我的测试类的方法上使用JUnit @Before注释,它工作正常,但它不是正确的行为,因为它运行每个测试用例,而不是一次。 因此,我想在一个方法上使用@BeforeClass注解,但将其添加到方法中会导致错误,说明它必须是静态方法。 Kotlin似乎没有静态的方法。 然后这同样适用于静态variables,因为我需要在测试用例中保留对嵌入式服务器的引用。 那么,如何为我的所有测试用例创建这个嵌入式数据库? class MyTest { @Before fun setup() { // works in that it opens the database connection, but is wrong // since this is per test case instead of being shared for all } @BeforeClass fun setupClass() { // what I want to do instead, but results in […]

如何在Kotlin中使用Double Epsilon / Precision调用assertEquals?

我想知道,在Kotlin中,有没有可能调用java方法的等价物: assertEquals(double expected, double actual, double precision) 因为每次我得到这个方法 assertEquals(expected: T, actual: T, message: String) 而我无法find具有精度参数的人。 调用Java的应该也可以,我猜。 我打电话给方法: assertEquals(5000.00, calculateCouponAmount(basicFaceValue, basicInterestRate, amortizationBullet, couponNumber1), 0.01) 我得到一个错误,因为0.01进入“消息”字段

我如何使用Kotlin运行单个Android测试?

我在unit testing中使用Kotlin 1.0.3进行Android开发,但是当我尝试运行单个测试时,它会运行所有类的测试。 有谁知道如何避免这种行为?