Elye
Elye

Reputation: 60081

No tests found for given includes Error, when running Parameterized Unit test in Android Studio

I have tried to run Parameterized Unit Tests in Android Studio, as shown below:

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));
    }
}

The result is an error stating No Test Run. However, if I remove the Parameterized tests, and change them to individual tests, it works.

Can anyone shed some light on why this is not working? Ar Parameterized unit tests not supported in Android development yet?

Below is the error with stack trace:

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]

Upvotes: 345

Views: 447703

Answers (30)

Gobe
Gobe

Reputation: 2717

My issue was caused by filter and not all of my modules actually having tests. The fix was to add isFailOnNoMatchingTests = false

subprojects {
  tasks.withType<Test> {
    filter {
      isFailOnNoMatchingTests = false

      excludeTestsMatching(...)
    }
  }
}

Upvotes: 0

Serhii Hrabas
Serhii Hrabas

Reputation: 464

For me this worked

  tasks.withType(Test).configureEach {
    useJUnitPlatform()
  }

Upvotes: 4

Tonnie
Tonnie

Reputation: 8112

In my case I encountered this error when trying out Jetbrains' Kotlin Test JUnit which is a Kotlin-specific testing library, an alternative to the Java's JUnit.

I encountered this error:

Test Events were not received Execution failed for task ':app:testDebugUnitTest'. No tests found for given includes: [com...TextExample](--tests filter)

To rectify the error I followed Kotlin Docs on how to set-up Jetbrains Kotlin Test JUnit on Intellij using Kotlin DSL.

I did the following on Android Studio:

Step 1 - Added this line to mydependencies: i.e. inside build.gradle.kts (app-level) file

dependencies { 
...

testImplementation(kotlin("test"))
...

}

Don't mix the above dependency with the other Kotilin-JUnit Libs to avoid ambiguity.

Step 2 (Optional) - Added the test task block i.e. inside build.gradle(.kts) (project-level) file: If for any reason you have to use this step then it has to be on the Android Project Level build.gradle file.

// Top-level build file where you can add configuration ...

    plugins {
        id("com.android.application") apply false
        ....
    }
    
    // add this block on project-level build.gradle.kts
    tasks.withType<Test> {

        useJUnitPlatform()

    }

There is also another way that worked for me by adding this dependency using the latest Kotlin Version (in Dec. 2023 it was 1.9.10)

testImplementation ("org.jetbrains.kotlin:kotlin-test-junit:1.9.10")

This will allow you to use the JetBrains Kotlin Test like this:

import kotlin.test.Test

class TextExample {

  @Test
    fun  exampleTest(){

    val result =  2 + 2
    assert(result == 4)
    }
}

I noted something very strange with the @Test annotation highlight color between the Java's and Jetbrains' Kotlin JUnit Libraries as depicted below:

enter image description here

Anyway, the color doesn't worry me as long as the tests run as intended.

PS If you come across a test error, it is advisable to first run the ExampleUnitTest that comes with the Android project and see if it will pass then fix it according to the error message.

Upvotes: 1

hrabrica
hrabrica

Reputation: 129

I accidentally created an abstract class instead of regular class and added test there so it didn't run because no subclass was created from it.

was

abstract class MyTest {

   @Test
   fun test1() {
       // ...
   }
} 

should be

class MyTest {

   @Test
   fun test1() {
       // ...
   }
} 

Upvotes: 1

hrabrica
hrabrica

Reputation: 129

In my case file and class name of the test didn't match, and that was the cause.

Eg. file name SomeTest.kt and class class SomeNewTest { ... }

Upvotes: 1

z33
z33

Reputation: 1263

In my case, it was solved by deleting .idea in the project folder.

Upvotes: 1

narges
narges

Reputation: 41

I had to change the @Test library. At first I had imporetd org.junit.Test and I was getting this exception. Then I changed it to org.junit.jupiter.api.Test and it worked.

Upvotes: 2

Xiaokun
Xiaokun

Reputation: 874

Same issue.

Caused by: org.gradle.api.GradleException: No tests found for given includes: [com.example........FibonacciTest]

Possibly that there is a class not found exception. You can check exactly the package path of the test cases, to see if there are any errors with the folder structure. It should be case-sensitive.

Upvotes: 0

dumbfingers
dumbfingers

Reputation: 7089

Encountered this issue and I want to share my experience hope it may help other SO users that may have similar experiences.

I have tried useJUnitPlatform() on gradle and however they don't work for me.

At last I noticed that I did an auto convert from Java to Kotlin on the test class, which converted some of the @Test annotation to something like:

@get:Test

Apparently by removing/correcting these annotations, all tests worked and no more errors of no tests found for given includes ...

Just remove these "incorrect" annotations and no more changes.

Upvotes: 1

comatoad
comatoad

Reputation: 23

In my case, none of these solutions worked. The issue was a problematic dependency being imported — junit.vintage.

Adding exclude group: “org.junit.vintage”, module: “junit-vintage-engine” to my build.gradle fixed this.

Upvotes: 0

Nikita Avramenko
Nikita Avramenko

Reputation: 113

I got a similar error in Intellij when using JUnit 4.

The fix was to remove from build.gradle.kts

    test {  useJUnitPlatform()  }

The result build.gradle.kts

plugins {
    kotlin("jvm") version "1.8.0"
    application
    id("org.jetbrains.kotlin.plugin.serialization") version "1.5.31"
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    implementation("com.google.code.gson:gson:2.8.8")
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.0")
    implementation ("org.apache.poi:poi-ooxml:5.1.0")
    testImplementation(kotlin("test"))
    testImplementation("junit:junit:4.13.1")
}



kotlin {
    jvmToolchain(11)
}

application {
    mainClass.set("MainKt")
}

And the tests work fine

import org.junit.Assert


class MainKtTest1 {
    @org.junit.Test
    fun firstTest() {
        Assert.assertEquals("2*2=4", 2 * 2, 4)
    }

}

Upvotes: 7

iman kazemayni
iman kazemayni

Reputation: 1343

Hard to believe but the issue in my case was the wrong name for the package that I created for my test cases. It shouldn't contain uppercase letters. For example, these cases are not correct: "PackageName" "Packagename" "packageName"

Upvotes: 5

osrl
osrl

Reputation: 7838

I needed to add robolectric dependency to resolve this issue.

Upvotes: 0

Ignacio Garcia
Ignacio Garcia

Reputation: 1153

Just in case this helps anybody, this happened to me on IntelliJ, and the case is, tests had been working before with Gradle but suddenly, next day some test stopped working giving this error.

What I did was:

  1. Go to Preferences -> Build, Execution, Deployment -> Gradle -> change "Run tests using" to "IntelliJ IDEA".
  2. Run your test.
  3. Go again to Preferences -> Build, Execution, Deployment -> Gradle -> change "Run tests using" to "Gradle (default)".
  4. Keep running your test, it is working now.

Doing this, I avoided having to change my local configuration or change Gradle files in the project.

enter image description here

Upvotes: 54

ToastyMallows
ToastyMallows

Reputation: 4273

Writing @ParameterizedTest methods in Kotlin, I had to add the following dependency in my build.gradle file:

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

Apparently JUnit 5 separates out the unit testing API and the runtime engine, and you need to include both for it to pick up your unit tests.

Upvotes: -1

K--
K--

Reputation: 734

In my case, I was running my test class in Intellij and saw No tests were found. I had marked my test methods as static - removing this fixed my issue.

Upvotes: 0

heringer
heringer

Reputation: 3188

Just adding... Maybe will help someone else...

In my case, this problem happened because of a case difference in the namespace of the unit test. One letter was upper case instead of lower case (camelCase instead of camelcase).

Upvotes: -1

Kevin Bassa
Kevin Bassa

Reputation: 213

For anyone that is getting the error: Expression 'test' cannot be invoked as a function when adding the proposed solutions, try instead

tasks.withType<Test> {
    useJUnitPlatform()
}

Works with a KMM setup in 2021.

Upvotes: 15

Anshul Singhal
Anshul Singhal

Reputation: 2201

I recently faced this issue and after deep-dive, I got to find out that I was using @Test annotation from junit library. However, my build.gradle file declared another test library.

Therefore, when I used @Test annotation from Test library present in build.gradle then it worked.

Upvotes: 12

I added one string to the gradle build dependency list:

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

Rebuild gradle and everything works.

And remember to add the Test Files:

import org.junit.jupiter.params.ParameterizedTest

Upvotes: 3

BabyishTank
BabyishTank

Reputation: 1482

My set up is Android Studio, Junit 4, test with robolectric.

The problem was I added this noverify setting to app level build.gradle in attempt to solve Java.lang.VerifyError: Bad return type from Android Studio run test with code coverage. (https://github.com/robolectric/robolectric/issues/3023)

enter image description here

Removing it fix the problem.

Upvotes: 1

Sebastian
Sebastian

Reputation: 2339

Adding

test {
    useJUnitPlatform()
}

to the top level gave me the error "> Could not find method test() for arguments". Instead I had to add it to testOptions inside the android block

android {
    ...

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

Upvotes: 77

Leila F.
Leila F.

Reputation: 17

I was using a private method. Changing the access modifier to public helped me.

Upvotes: 0

UserBSS1
UserBSS1

Reputation: 2211

Add in build.gradle,

test {
    useJUnitPlatform()
}

and use class org.junit.jupiter.api.Test for @Test, instead of org.junit.Test

Upvotes: 20

burnah-boiii
burnah-boiii

Reputation: 5914

If you're using JUnit 5+, make sure you import the @Test annotation from the correct library:

import org.junit.jupiter.api.Test

not

import org.junit.Test

Upvotes: 566

Nelliczan
Nelliczan

Reputation: 1

It helps to add: test {useJUnitPlatform()} but you also need to have recent springBootVersion for me it worked with '2.4.5'

Upvotes: -2

Zoey
Zoey

Reputation: 31

For JUnit 5, we should use import org.junit.jupiter.api.Test and also should use @ExtendWith instead of @RunWith.

Upvotes: 3

Braian Coronel
Braian Coronel

Reputation: 22867

Allow Run tests for all sub-modules using Intellij IDEA by command or UI.

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

Upvotes: 2

Anthony Naddeo
Anthony Naddeo

Reputation: 2751

I made the mistake of defining my test like this

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


        // something here that isn't Unit
    }
}

That resulted in runBlocking returning something, which meant that the method wasn't void and junit didn't recognize it as a test. That was pretty lame. I explicitly supply a type parameter now to run blocking. It won't stop the pain or get me my two hours back but it will make sure this doesn't happen again.

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


        // something here that isn't Unit
    }
}

Upvotes: 30

lsa
lsa

Reputation: 35

For me it worked when I've added @EnableJUnit4MigrationSupport class annotation.

(Of course together with already mentioned gradle libs and settings)

Upvotes: 1

Related Questions