在Android Studio中运行参数化单元测试时,没有发现给定的测试包含错误

我尝试在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));
}
}

结果是一个声明No Test Run的错误。但是,如果我删除参数化测试,并将它们更改为单独的测试,它就可以工作。

有人能解释一下为什么这行不通吗?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]
298642 次浏览

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

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

添加到build.gradle:

test {
useJUnitPlatform()
}

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

tasks.test {
useJUnitPlatform()
}

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

import org.junit.jupiter.api.Test

import org.junit.Test

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

enter image description here

修复的源代码: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的单独文件夹中管理集成测试用例。我必须在build.gradle文件中定义一个新任务,并在添加第一行->我的问题解决了

芬兰湾的科特林DSL:添加到你的build.gradle.kts

tasks.withType<Test> {
useJUnitPlatform()
}

Gradle DSL:添加到build.gradle

test {
useJUnitPlatform()
}

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

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

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

您可以尝试重新运行--debug并搜索FAILEDorg.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日志。我没有看到任何有用的--info

我使用org.junit.Test包的@Test注释,但我有同样的问题。在build.gradle上添加testImplementation("org.assertj:assertj-core:3.10.0")后,它工作了。

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

test {
}

步骤:

    在构建中添加平台运行器。gralde
    =比;testCompile组:'org.junit. '平台',名称:'junit-platform-runner',版本:'1.7.0'
  1. 注释测试类@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: Bad return type从Android Studio运行测试与代码覆盖。(https://github.com/robolectric/robolectric/issues/3023)

enter image description here

移除它就能解决问题。

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

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

重建gradle和一切工作。

记住要添加测试文件:

import org.junit.jupiter.params.ParameterizedTest

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

因此,当我使用存在于build.gradle中的测试库中的@Test注释时,它就工作了。

对于任何在添加建议的解决方案时得到错误:Expression 'test' cannot be invoked as a function的人,请尝试替换

tasks.withType<Test> {
useJUnitPlatform()
}

适用于2021年的KMM设置。

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

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

在我的情况下,我正在Intellij中运行我的测试类,并看到No tests were found。我已经把我的测试方法标记为static -删除这个修复了我的问题。

在Kotlin中编写@ParameterizedTest方法,我必须在我的build.gradle文件中添加以下依赖项:

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

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

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

我所做的是:

  1. 转到首选项->构建,执行,部署->Gradle→更改“使用”运行测试;到“IntelliJ idea”。
  2. 运行测试。
  3. 再次点击Preferences ->构建,执行,部署->Gradle→更改“使用”运行测试;到“Gradle(默认)”。
  4. 继续运行您的测试,它现在正在工作。

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

enter image description here

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