Unit testing is an important part of the software development process. It allows you to test individual code units in a controlled environment, which helps you verify that they are working as expected. This article will show you how to troubleshoot unit tests with you are trying to run with Gradle. For this lesson, we will look at the error message:
No tests found for given includes
We will discuss breaking down the message, troubleshooting, researching solutions, and debugging. Once you know how to troubleshoot one error message in Gradle, you can follow the same process to troubleshoot other error messages.
In This Article
Troubleshooting Step 1: Breakdown the error message
The error messageThe Java plugin introduces the concept of a source set. A source set is simply a group of source files which are compiled and executed together. These source files may include Java source files and resource files. Other plugins add the ability to include Groovy and Scala source files in a source set. A source set has an associated compile classpath, and runtime classpath.
One use for source sets is to group source files into logical groups which describe their purpose. For example, you might use a source set to define an integration test suite, or you might use separate source sets to define the API and implementation classes of your project.
The Java plugin defines two standard source sets, called main and test. The main source set contains your production source code, which is compiled and assembled into a JAR file. The test source set contains your test source code, which is compiled and executed using JUnit or TestNG. These can be unit tests, integration tests, acceptance tests, or any combination that is useful to you.tells you that Gradle cannot find the test associated with the code that you just tried to test.
Troubleshooting Step 2: Check that Gradle is set to scan for tests
Gradle will automatically run all tests that it finds. It does this by looking at the test classes that have been compiled. However, the test property must be true for Gradle to scan for tests.
It’s possible that someone (may you) didn’t want the test cases to run for some reason and forgot to reset the property to true. This is a good place to add a comment to document the problem in the code so that it will be easy to resolve later.
If want to turn test class detection off again, you can disable it by setting the scanForTestClasses property back to false.
The criteria Gradle uses to find tests depend on the framework you are using.
JUnit3 and JUnit 4
Gradle scans for tests that fit these 3 criteria:
- Inherits from TestCase or GroovyTestCase
- @RunWith annotation
- Contains a method with the @Test annotation, or if its superclass does
TestNG
Gradle scans for methods with the @Test annotation.
Abstract classes are not executed. Also, when it scans, Gradle looks up the inheritance tree into jar files located on the test classpath. If there are any test classes in those JARs, they will be run too.
Note: The JUnit Platform uses only includes and excludes to filter test classes so it will ignore the value for scanForTestClasses. If you first set up your test cases on JUnit and then switched to TestNG, it is possible that the scanForTestClasses property on Test was set to false. If you switch the value to true, Gradle will scan for and likely find your test cases.
After you verify that the Gradle property settings are correct, try running the unit tests again. If they execute properly, you’re done. If not, move on to Step 2below.
Troubleshooting Step 2: Check that Gradle has the correct classpaths
In Gradle, all tests are typed as tasks. The Test task type only requires two bits of information to run:
- Where to find the compiled test classes (property: Test.getTestClassesDirs())
- The execution classpath, which should include the classes under test as well as the test library that you’re using (property: Test.getClasspath())
Since Gradle cannot find the test cases, the problem may be in the property settings for Test.getTestClassesDirs() or Test.getClasspath().
- Check that Test.getTestClassesDirs() has the right path to your compiled test classes.
- Check that Test.getClasspath() has the correct path to your test library (JUnit, JUnit Platform, or TestNG).
- Verify that you have configured all dependencies correctly. For your tests to work properly, you’ll need to set the right compile-time, runtime dependencies, and any other required configuration for the test task.
This lets your code correctly find and utilize the resources it needs. Your JVM language plugin will provide this information. The JVM language plugins ensure that the task is configured with the correct execution classpath and directory for compiled test classes. In addition, these plugins also attach the test task to the check lifecycle task.
Remember that the plugin test also creates relevant dependency configurations. Start by verifying testImplementation and testRuntimeOnly. They’re linked to the plugins’ classpath by a test task.
After you verify that the Gradle property settings are correct, try running the unit tests again. If they execute properly, you’re done. If not, move on to Step 3 below.
Troubleshooting Step 3: Look for problems in the Java Plugin
The Java plugin allows projects to compile, test, and bundle Java code.
build.gradle
apply plugin: ‘java’
The Java plugin uses source sets to group files for compiling and executing. These files could be Java source files or resource files. Every source set includes two classpaths: one for compilation and one for runtime.
The error message: ‘No tests found for given includes” indicates Gradle cannot find either the test directory or the unit tests, so it could be a problem with the classpath in the source set.
So the next thing you need to do is check that each of these classpaths is correct for compiling and executing tests.
Most of the code you write will be in the main source set. This code will be compiled and stored in a JAR file. The test source set is compiled and executed with your test suite (JUnit or TestNG). Note: We are troubleshooting unit tests, but you would do the same steps to troubleshoot integration tests, acceptance tests, or performance tests.
After verifying that the Java plugin settings are correct, run the unit tests again. If they execute properly, you’re done. If not, move on to Step 4 below.
Troubleshooting Step 4: Look for dependency problems
Gradle helps you manage dependencies in your software projects. It can help you with many of the tasks you encounter in modern software development, but sometimes the automation process overreaches, and you need to check the dependencies manually
Frequently Asked Questions
Image Credit: Github.com