Running a single test from gradle command line - java

I have a multi-project gradle setup
RootProject
|
---- ProjectA
|
---- ProjectB
ProjectA contains around 500 unit tests. Out of which 3 are failing in a single class MyClass.
Everytime I do:
./gradlew :ProjectA:test
it runs all 500 unit tests.
I am trying to run a single failing test inside MyClass or atleast only tests contained in MyClass to reduce the build time.
I tried:
./gradlew -D:MyProject:test.single=MyClass --no-rebuild :MyProject:test
However, it still continues to run all the tests.
Trying this works
./gradlew :MyProject:test --tests '*MyClass*'
but still takes lots of time as it seems to be parsing names of every test class to find the matching name.
Trying
./gradlew :MyProject:test --tests "com.mypackage1.mypackage2.MyClass.testMethod1"
throws the following error:
> No tests found for given includes: [**/*SomeTest.class](exclude rules) [com.mypackage1.mypackage2.MyClass.testMethod1](--tests filter)
What am I missing? What am I doing wrong?
This seems to be a simple use case which should be very obvious to do.
Edit 1:
RootProject build.gradle has:
configure(subprojects.findAll {it.name != 'SomeProjToIgnore'}) {
test {
forkEvery 1
//Exclude tests
exclude '**/*SomeTest.class'
}
}
Edit 2:
./gradlew :MyProject:test --tests 'MyClass*'
is considerably faster. (notice removed '*' from the front). But still I don't know how to run a single test inside MyClass.

Related

Could not determine the dependencies of task -testone

I started learning Gradle (Java + Gradle) and created this very basic - build.gralde - file.
task testone(type: Test) {
println ("Started TestSetUp")
}
In Console when ran command -> gradlew testone -- intellij is throwing error:
> Configure project :
Started TestSetUp
FAILURE: Build failed with an exception.
* What went wrong:
Could not determine the dependencies of task ':testone'.
I think i can help you with that , By changing the type from Test to Copy , this will build perfectly , Copy is a defined type in gradle and running gradle testone -q will show your output .
I think the problem is with your defined type , by declaring the type as Test you need to add more configuration for this , because the error i got when i tried to register the task like
tasks.register testone(type: Test) {
println ("Started TestSetUp")
}
was Could not find method testone() for arguments [{type=class org.gradle.api.tasks.testing.Test} so your issue is with the Test type , i spose your missing some extra configuration .
When you’re using a JVM language plugin — such as the Java Plugin —
you will automatically get the following:
A dedicated test source set for unit tests
A test task of type Test that runs those unit tests
In "build.gradle", after adding java plugin, the task ran successfully.
plugins {
id 'java'
}
task testone(type: Test) {
println ("Task of type Test Success")
}

Include / exclude tests with Maven Surefire + JUnit5 from command line

I want to run the build for Apache James which has a huge test suite that is running very long due to tests that irrelevant to me, e.g. tests concerning RabbitMQ. Thus I'd like to exclude those and I want to do so from the command line (not by editing POMs). I'm using Maven 3.6.3 on Java 11 OpenJDK. The project uses JUnit5 and maven-surefire-plugin 2.22.2.
Now, I would expect the following to work:
For example, to run only test methods in the org.example.MyTest test
class you can execute mvn -Dtest=org.example.MyTest test from the
command line.
But it doesn't work. In fact, as soon as I set the test parameter to anything else than an empty string, all tests will be skipped. I tried some of the syntax that is supposedly supported...
mvn package -Dtest=*
mvn package -Dtest=".*"
mvn package -Dtest=\!SomethingFishy
mvn package -Dtest='!MavenHeadache'
mvn package -Dtest='!%regex[.*HelpMe.*]'
...but the result is always the same:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test
(default-test) on project testing-base: No tests were executed!
(Set -DfailIfNoTests=false to ignore this error.) -> [Help 1]
I'm running the package goal, as it does the full build, but test behaves the same. Also tried specifying -Dincludes=... / -Dexcludes=..., which had no effect at all: All tests are executed regardless and the parameters don't even show up in the output of mvn -X .... This behavior doesn't change when update maven-surefire-plugin to the latest version which is 3.0.0-M5.
Do I understand something wrong here? How can i specify inclusions/exclusions in this setup?
Update: It looks like this is caused by nested projects and/or James' project structure in particular. If I enter a "leaf project", e.g. core, then inclusions/exclusions begin to work:
cd core
mvn test -Dtest=HostTest # will only run HostTest, as expected
mvn test -Dtest=\!HostTest # will run all tests but HostTest, as expected
As suggested by RobertScholte, I have looked at the maven-surefire-plugin configuration, but couldn't find anything that seems to be related to this behavior.
The error message tells what you need to know: in project testing-base which is the first to run, there's no test matching your pattern, so it fails to ensure you won't have a false impression of success.
It then suggests to use -DfailIfNoTests=false option to let maven ignore modules that don't have any test matching the pattern (it's probably what you need).

Excluding a single test method in Gradlew

I am trying to exclude a single test method from a test file in my gradle project. I have excluded the entire test file as below which works fine.
./gradlew -PexcludeTests="com/in/Mytest*"
The above works and excludes all the test methods in the test file. However, I am looking for a command-line way where I can exclude just one test method.

Making unit test run as integration test instead

I have some projects with both unit tests and integration tests. I have them separated so that unit tests run as part of the regular build, and integration tests are only run by a specific "integTest" task. This is all working fine.
I have another project that I didn't write, and can't refactor, which has a single unit test that isn't really a unit test. I have a stock Gradle build script for this project, but I'd like to add the pieces that I put in other projects to run this test as an integration test instead. I haven't done this yet, but I think what I've done in other projects will only half work. I'm certain it will let me run that test as an integration test, but I don't yet know how to make it NOT run as a unit test.
The one test is in "src/test/java", and I'm now going to associate that with my "integTest" task (I used "src/integTest/groovy" before, and I imagine I could add "src/integTest/java" also). How do I REMOVE that directory from being considered by the default "test" task, so "test" never runs any tests?
Update:
Although the title of this posting is about running the unit test as an integration test, I really only needed to know how to exclude the existing test from the unit test pass, which was answered.
Someone seeing the title of this might want to know how to do that, so I'll add detail of how I did this.
The following shows everything that I added to the build script to make this happen:
// Without refactoring the source code of the project, this excludes the one test that looks like a unit test, but is
// actually an integration test.
test { exclude '**/*.*' }
sourceSets {
integTest {
java.srcDir file("src/test/java")
resources.srcDir file("src/test/resources")
runtimeClasspath = output + compileClasspath
}
}
dependencies {
integTestCompile sourceSets.main.output
integTestCompile configurations.testCompile
integTestCompile sourceSets.test.output
integTestRuntime configurations.testRuntime
}
task integTest(type: Test) {
testClassesDir = sourceSets.integTest.output.classesDir
classpath = sourceSets.integTest.runtimeClasspath
// This forces integration tests to always run if the task is run.
outputs.upToDateWhen { false }
}
In your build.gradle you can do:
test {
exclude '**/*.*'
}
or just disable test task by adding the line test.enabled = false

Gradle build without tests

I want to execute gradle build without executing the unit tests. I tried:
$ gradle -Dskip.tests build
That doesn't seem to do anything. Is there some other command I could use?
You should use the -x command line argument which excludes any task.
Try:
gradle build -x test
Update:
The link in Peter's comment changed. Here is the diagram from the Gradle user's guide
Try:
gradle assemble
To list all available tasks for your project, try:
gradle tasks
UPDATE:
This may not seem the most correct answer at first, but read carefully gradle tasks output or docs.
Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
You can add the following lines to build.gradle, **/* excludes all the tests.
test {
exclude '**/*'
}
The accepted answer is the correct one.
OTOH, the way I previously solved this was to add the following to all projects:
test.onlyIf { ! Boolean.getBoolean('skip.tests') }
Run the build with -Dskip.tests=true and all test tasks will be skipped.
Every action in gradle is a task, and so is test. And to exclude a task from gradle run, you can use the option --exclude-task or it's shorthand -x followed by the task name which needs to be excluded. Example:
gradle build -x test
The -x option should be repeated for all the tasks that needs to be excluded.
If you have different tasks for different type of tests in your build.gradle file, then you need to skip all those tasks that executes test. Say you have a task test which executes unit-tests and a task testFunctional which executes functional-tests. In this case, you can exclude all tests like below:
gradle build -x test -x testFunctional
Using -x test skip test execution but this also exclude test code compilation.
gradle build -x test
In our case, we have a CI/CD process where one goal is compilation and next goal is testing (Build -> Test).
So, for our first Build goal we wanted to ensure that the whole project compiles well. For this we have used:
./gradlew build testClasses -x test
On the next goal we simply execute tests:
./gradlew test
You can exclude tasks
gradle build --exclude-task test
https://docs.gradle.org/current/userguide/command_line_interface.html#sec:command_line_executing_tasks
the different way to disable test tasks in the project is:
tasks.withType(Test) {enabled = false}
this behavior needed sometimes if you want to disable tests in one of a project(or the group of projects).
This way working for the all kind of test task, not just a java 'tests'. Also, this way is safe. Here's what I mean
let's say: you have a set of projects in different languages:
if we try to add this kind of record in main build.gradle:
subprojects{
.......
tests.enabled=false
.......
}
we will fail in a project when if we have no task called tests
Reference
To exclude any task from gradle use -x command-line option. See the below example
task compile << {
println 'task compile'
}
task compileTest(dependsOn: compile) << {
println 'compile test'
}
task runningTest(dependsOn: compileTest) << {
println 'running test'
}
task dist(dependsOn:[runningTest, compileTest, compile]) << {
println 'running distribution job'
}
Output of: gradle -q dist -x runningTest
task compile
compile test
running distribution job
Hope this would give you the basic
In The Java Plugin:
$ gradle tasks
Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
testClasses - Assembles test classes.
Verification tasks
------------------
test - Runs the unit tests.
Gradle build without test you have two options:
$ gradle assemble
$ gradle build -x test
but if you want compile test:
$ gradle assemble testClasses
$ gradle testClasses
Please try this:
gradlew -DskipTests=true build

Categories

Resources