我尝试在Android Studio中运行参数化单元测试,如下所示:

import android.test.suitebuilder.annotation.SmallTest;  

import junit.framework.TestCase;    

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;  

import java.util.Arrays;
import java.util.Collection;    

@RunWith(Parameterized.class)
@SmallTest
public class FibonacciTest extends TestCase {
    @Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][] {
                {0, 0}, {1, 1}, {2, 1}, {3, 2}, {4, 3}, {5, 5}, {6, 8}
        });
    }   

    @Parameter // first data value (0) is default
    public /* NOT private */ int fInput;    

    @Parameter(value = 1)
    public /* NOT private */ int fExpected; 

    @Test
    public void test() {
        assertEquals(fExpected, Fibonacci.calculate(fInput));
    }
}

结果是一个错误,说明没有测试运行。但是,如果我删除参数化测试,并将它们更改为单独的测试,它就可以工作。

有人能解释一下为什么这行不通吗?Android开发中还不支持参数化单元测试?

下面是堆栈跟踪的错误:

FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:testDebug'.
> No tests found for given includes: [com.example.......FibonacciTest]
* Try:
Run with --info or --debug option to get more log output.
* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:testDebug'.
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:69)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:46)
    at org.gradle.api.internal.tasks.execution.PostExecutionAnalysisTaskExecuter.execute(PostExecutionAnalysisTaskExecuter.java:35)
    at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:64)
    at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58)
    at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:42)
    at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
    at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:53)
    at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
    at org.gradle.api.internal.AbstractTask.executeWithoutThrowingTaskFailure(AbstractTask.java:310)
    at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.executeTask(AbstractTaskPlanExecutor.java:79)
    at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.processTask(AbstractTaskPlanExecutor.java:63)
    at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.run(AbstractTaskPlanExecutor.java:51)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor.process(DefaultTaskPlanExecutor.java:23)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter.execute(DefaultTaskGraphExecuter.java:88)
    at org.gradle.execution.SelectedTaskExecutionAction.execute(SelectedTaskExecutionAction.java:37)
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:62)
    at org.gradle.execution.DefaultBuildExecuter.access$200(DefaultBuildExecuter.java:23)
    at org.gradle.execution.DefaultBuildExecuter$2.proceed(DefaultBuildExecuter.java:68)
    at org.gradle.execution.DryRunBuildExecutionAction.execute(DryRunBuildExecutionAction.java:32)
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:62)
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:55)
    at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:149)
    at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:106)
    at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:86)
    at org.gradle.launcher.exec.InProcessBuildActionExecuter$DefaultBuildController.run(InProcessBuildActionExecuter.java:90)
    at org.gradle.tooling.internal.provider.runner.BuildModelActionRunner.run(BuildModelActionRunner.java:54)
    at org.gradle.launcher.exec.ChainingBuildActionRunner.run(ChainingBuildActionRunner.java:35)
    at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:41)
    at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:28)
    at org.gradle.launcher.daemon.server.exec.ExecuteBuild.doBuild(ExecuteBuild.java:49)
    at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.WatchForDisconnection.execute(WatchForDisconnection.java:37)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.ResetDeprecationLogger.execute(ResetDeprecationLogger.java:26)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.RequestStopIfSingleUsedDaemon.execute(RequestStopIfSingleUsedDaemon.java:34)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.ForwardClientInput$2.call(ForwardClientInput.java:74)
    at org.gradle.launcher.daemon.server.exec.ForwardClientInput$2.call(ForwardClientInput.java:72)
    at org.gradle.util.Swapper.swap(Swapper.java:38)
    at org.gradle.launcher.daemon.server.exec.ForwardClientInput.execute(ForwardClientInput.java:72)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.health.DaemonHealthTracker.execute(DaemonHealthTracker.java:47)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.LogToClient.doBuild(LogToClient.java:66)
    at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.EstablishBuildEnvironment.doBuild(EstablishBuildEnvironment.java:71)
    at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.health.HintGCAfterBuild.execute(HintGCAfterBuild.java:41)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.StartBuildOrRespondWithBusy$1.run(StartBuildOrRespondWithBusy.java:50)
    at org.gradle.launcher.daemon.server.DaemonStateCoordinator$1.run(DaemonStateCoordinator.java:246)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:54)
    at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:40)
Caused by: org.gradle.api.GradleException: No tests found for given includes: [com.example........FibonacciTest]
    at org.gradle.api.internal.tasks.testing.NoMatchingTestsReporter.afterSuite(NoMatchingTestsReporter.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.event.BroadcastDispatch.dispatch(BroadcastDispatch.java:87)
    at org.gradle.internal.event.BroadcastDispatch.dispatch(BroadcastDispatch.java:31)
    at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
    at com.sun.proxy.$Proxy46.afterSuite(Unknown Source)
    at org.gradle.api.internal.tasks.testing.results.TestListenerAdapter.completed(TestListenerAdapter.java:48)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.event.BroadcastDispatch.dispatch(BroadcastDispatch.java:87)
    at org.gradle.internal.event.BroadcastDispatch.dispatch(BroadcastDispatch.java:31)
    at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
    at com.sun.proxy.$Proxy45.completed(Unknown Source)
    at org.gradle.api.internal.tasks.testing.results.StateTrackingTestResultProcessor.completed(StateTrackingTestResultProcessor.java:69)
    at org.gradle.api.internal.tasks.testing.results.AttachParentTestResultProcessor.completed(AttachParentTestResultProcessor.java:52)
    at org.gradle.api.internal.tasks.testing.processors.TestMainAction.run(TestMainAction.java:51)
    at org.gradle.api.internal.tasks.testing.detection.DefaultTestExecuter.execute(DefaultTestExecuter.java:75)
    at org.gradle.api.tasks.testing.Test.executeTests(Test.java:527)
    at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:75)
    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.doExecute(AnnotationProcessingTaskFactory.java:226)
    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:219)
    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:208)
    at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:589)
    at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:572)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:80)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:61)
    ... 57 more
BUILD FAILED
Total time: 4.153 secs
No tests found for given includes: [com.example......FibonacciTest]

找到了在Android Studio中运行测试的方法。显然,使用Gradle Configuration运行它不会执行任何测试。相反,我使用JUnit Configuration。简单的方法是选择要运行的测试类,然后右键单击。然后选择“运行”。之后,您将看到2个运行选项。根据图像选择底部的一个(JUnit)

(注意:如果你找不到2 Run Configuration来选择,你需要先删除你之前使用的配置(Gradle Configuration)。这可以通过点击顶部工具栏中的“选择运行/调试配置”图标来完成。


添加到build.gradle:

test {
    useJUnitPlatform()
}

要添加到Przemek315提供的已经很好很简单的解决方案中,如果你使用Kotlin DSL,同样的配置:

tasks.test {
    useJUnitPlatform()
}

如果您使用的是JUnit 5+,请确保从正确的库中导入@Test注释:

进口org.junit.jupiter.api.Test

not

进口org.junit.Test


我正在使用JUnit 4,对我有用的是将“Gradle ->运行测试使用”的IntelliJ设置从“Gradle(默认)”更改为“IntelliJ IDEA”。

修复方法的来源:https://linked2ev.github.io/devsub/2019/09/30/Intellij-junit4-gradle-issue/


如果你正在使用intellij并且想要使用gradle,你需要将它添加到build的依赖项部分。gradle文件:

testImplementation("org.junit.jupiter:junit-jupiter-api:5.4.2")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.4.2")

对我来说,错误消息的原因

未找到针对给定包含的测试

无意中在我的src/test/kotlin测试目录下添加了一个。java测试文件。在将文件移动到正确的目录src/test/java后,测试再次按预期执行。


我也面临着同样的问题。在我的情况下,我使用的是JUnit 5和gradle 6.6。我在调用integ的单独文件夹中管理集成测试用例。我必须在构建中定义一个新任务。gradle文件和添加第一行-> useJUnitPlatform()后,我的问题得到了解决


Kotlin DSL:添加到build.gradle.kts中

tasks.withType<Test> {
        useJUnitPlatform()
}

Gradle DSL:添加到build.gradle中

test {
    useJUnitPlatform()
}

你可以检查你的方法是否是私有的。花了很多时间来纠正这个愚蠢的错误……


在我的情况下,我得到这条消息是因为Junit运行时错误,从gradle测试任务执行的输出中根本看不出来。我遇到这种情况有几个原因:

包括org.junit。platform:junit-platform-launcher依赖项,其版本与我正在使用的junit版本不匹配 在META-INF/services中有一个Junit测试监听器的条目,我已经注释掉了

您可以尝试重新运行——debug并搜索FAILED或org.gradle.api.internal.tasks.testing.TestSuiteExecutionException。在我的第二个案例中,例外是:

2020-10-20T11:34:26.517-0700 [DEBUG] [TestEventLogger]
2020-10-20T11:34:26.517-0700 [DEBUG] [TestEventLogger] Gradle Test Executor 1 STARTED
2020-10-20T11:34:26.661-0700 [DEBUG] [TestEventLogger]
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger] Gradle Test Executor 1 FAILED
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]     org.gradle.api.internal.tasks.testing.TestSuiteExecutionException: Could not complete execution for Gradle Test Executor 1.
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.stop(SuiteTestClassProcessor.java:63)
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at java.lang.reflect.Method.invoke(Method.java:498)
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33)
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94)
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at com.sun.proxy.$Proxy2.stop(Unknown Source)
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at org.gradle.api.internal.tasks.testing.worker.TestWorker.stop(TestWorker.java:132)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at java.lang.reflect.Method.invoke(Method.java:498)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:182)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:164)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:413)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at java.lang.Thread.run(Thread.java:748)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]         Caused by:
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]         java.util.ServiceConfigurationError: org.junit.platform.launcher.TestExecutionListener: Provider com.example.myproject.MyCommentedOutClass not found
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at java.util.ServiceLoader.fail(ServiceLoader.java:239)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at java.util.ServiceLoader.access$300(ServiceLoader.java:185)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:372)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at java.util.ServiceLoader$1.next(ServiceLoader.java:480)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at java.lang.Iterable.forEach(Iterable.java:74)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:94)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:67)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.processAllTestClasses(JUnitPlatformTestClassProcessor.java:97)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.access$000(JUnitPlatformTestClassProcessor.java:79)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor.stop(JUnitPlatformTestClassProcessor.java:75)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.stop(SuiteTestClassProcessor.java:61)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             ... 25 more

注意,这些是DEBUG日志。我没有看到任何有用的信息


我使用了org.junit.Test包的@Test注释,但我遇到了同样的问题。在构建时添加testimplemimplementation ("org.assertj:assertj-core:3.10.0")之后。Gradle,它成功了。


当我使用junit5时,它正在工作。但每次我执行gradle -clean时,我得到Class not found错误。然后我添加这个来构建。gradle来解决我的问题,我仍然可以使用junit4:

test {
}

步骤:

在build. gradde中添加平台运行器 => testCompile group: 'org.junit. '平台,名称:'junit-platform-runner',版本:'1.7.0' 用@RunWith注释测试类 = > @RunWith (JunitPlatform.class)


在我的案例中,问题发生在用Kotlin编写和使用IDEA 2020.3时。尽管在build.gradle.kts中有适当的条目。事实证明,问题出在用IDEA IDE (Alt + Insert)生成测试函数时。它生成以下代码:

@Test
   internal fun name () {
     TODO ("Not yet implemented")
   }

在移除“内部”修饰符后,这个问题将得到修复:

@Test
   fun name () {
     TODO ("Not yet implemented")
   }

对我来说,当我添加了@EnableJUnit4MigrationSupport类注释时,它就起作用了。

(当然还有前面提到的gradle lib和设置)


我犯了这样定义测试的错误

class MyTest {
    @Test
    fun `test name`() = runBlocking {


        // something here that isn't Unit
    }
}

这导致runBlocking返回一些东西,这意味着该方法不是void, junit不认为它是一个测试。这太差劲了。我现在显式地提供一个类型参数来运行阻塞。这不能让我停止疼痛,也不能让我恢复两小时的工作时间但它能确保这种事不会再发生。

class MyTest {
    @Test
    fun `test name`() = runBlocking<Unit> { // Specify Unit


        // something here that isn't Unit
    }
}

允许通过命令或UI使用Intellij IDEA对所有子模块运行测试。

subprojects {
    withType<Test> {
        useJUnitPlatform()
    }
}

对于JUnit 5,我们应该使用import org.junit.jupiter.api.Test,并且应该使用@ExtendWith而不是@RunWith。


它有助于添加:test {useJUnitPlatform()} 但你也需要有最近的springBootVersion对我来说,它适用于'2.4.5'


我需要添加机器人电力依赖来解决这个问题。


添加build.gradle,

test {
    useJUnitPlatform()
}

使用类org.junit.jupiter.api.Test来代替org.junit.Test


我用的是私人方法。将访问修饰符更改为public帮助了我。


添加

test {
    useJUnitPlatform()
}

到顶层给了我一个错误“>无法为参数找到方法test()”。相反,我必须将它添加到android块中的testOptions中

android {
    ...

    testOptions {
        unitTests.all {
            useJUnitPlatform()
        }
    }
}

我的设置是Android Studio, Junit 4,用robolectric测试。

问题是我将这个noverify设置添加到应用程序级构建中。gradle试图解决Java.lang.VerifyError:从Android Studio运行测试与代码覆盖的糟糕返回类型。(https://github.com/robolectric/robolectric/issues/3023)

移除它就能解决问题。


我在gradle构建依赖列表中添加了一个字符串:

dependencies {
    implementation("org.junit.jupiter:junit-jupiter-params:5.8.0")
    testImplementation(kotlin("test"))
}

重建gradle和一切工作。

记住要添加测试文件:

import org.junit.jupiter.params.ParameterizedTest

我最近遇到了这个问题,在深入研究之后,我发现我使用的是junit库中的@Test注释。然而,我的身材。Gradle文件声明了另一个测试库。

因此,当我在build中使用Test库中的@Test注释时。然后Gradle就成功了。


对于任何得到错误的人:在添加建议的解决方案时,表达式'test'不能作为函数调用,请尝试替代

tasks.withType<Test> {
    useJUnitPlatform()
}

适用于2021年的KMM设置。


只是添加…也许会帮助别人…

在我的例子中,这个问题的发生是因为单元测试的名称空间中的大小写不同。一个字母是大写而不是小写(camelCase而不是camelCase)。


在我的例子中,我在Intellij中运行我的测试类,看到没有找到测试。我已经把我的测试方法标记为静态-删除这个修正了我的问题。


在Kotlin中编写@ParameterizedTest方法时,我必须在构建中添加以下依赖项。gradle文件:

dependencies {
   testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}

显然,JUnit 5分离了单元测试API和运行时引擎,您需要包括这两者,以便它能够接受单元测试。


为了帮助大家,我在IntelliJ上遇到了这样的情况,测试之前在Gradle上工作,但突然,第二天一些测试停止工作,给出了这个错误。

我所做的是:

进入首选项->构建,执行,部署-> Gradle ->将“使用运行测试”更改为“IntelliJ IDEA”。 运行测试。 再次打开首选项->构建,执行,部署-> Gradle ->将“使用”更改为“Gradle(默认)”。 继续运行您的测试,它现在正在工作。

这样做,我避免了更改我的本地配置或更改项目中的Gradle文件。


很难相信,但在我的案例中,问题是我为测试用例创建的包的名称错误。它不应该包含大写字母。例如,这些情况是不正确的: “PackageName” “Packagename” “packageName”