Gradle and spock few needed tests to run - gradle

my question is can i run by gradle a few needed for me spock tests?
I mean not packages and classes, only tests. I know the way how i can run one test:
./gradlew testTask --tests "*TestName*"
TestTask contains only include package where tests was placed.
I tried code like this:
./gradlew testTask --tests "*TestName*","*TestName2*"
It Doesn't work.Maybe someone know how i can do this?

According to the docs --tests is an option that you can repeat. So, you should be able to specifically pick multiple tests to run with:
./gradlew testTask --tests "*TestName*" --tests "*TestName2*"

For Gradle 3.x you can apply only one filtering rule to a single --tests switch. In your case I see two possible solutions:
You can create a wildcard that will satisfy tests you expect to run (and only them), e.g.
./gradlew testTask --tests "*TestName*"
this rule will satisfy both, TestName.java and TestName2.java
You can create separate test tasks and apply different filtering rule to them, e.g.:
./gradlew firstTestTask --tests "*TestName*" secondTestTask --tests "*TestName2*"
It is verbose, but you have a control over all filtering rules.
Please take a look to Gradle's documentation for more information: https://docs.gradle.org/3.3/userguide/java_plugin.html#test_filtering

Related

how to get maven to run all tests but still fail build on compile error

Suppose I have a multi-module maven project. I want to run it in such a way that:
If a test fails, I still want to compile and test the next module. This sounds like a job for --fail-never, but see below.
If there are test failures, I don't really care if the build succeeds or fails. Ideally this would be configurable, but whatever.
If there are compiler errors, then the build should fail. I think this rules out "fail-never"?
You can use surefire parameter: maven.test.failure.ignore
mvn test -Dmaven.test.failure.ignore
With this options failing test will not fail build a module and finally whole build will have success result.

What is the difference between 'mvn verify' vs 'mvn test'?

I'm bit confused with mvn verify phase. I've created a Spring Boot project (a simple project, without any explicit configurations added). I've created a few JUnit unit tests which are run with both the mvn verify and mvn test commands.
There isn't any difference observed in the mvn verify and mvn test command output.
What does mvn verify do different than mvn test?
Also some posts on Stack Overflow mentions that mvn verify runs the integration tests. If this is the case then I have few questions.
How does Maven identify a specific test as a unit test or integration test?
If mvn verify is supposed to run only the integration tests, then why are unit tests executed with it?
First of all, when you run a Maven goal, it will run any previous goal. The order of basic phases is:
Validate
Compile
Test
Package
Verify
Install
Deploy
If you run Test, Maven will execute validate, compile and test. Based on this, the first point is that verify includes test.
Based on official documentation:
TEST - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
VERIFY - run any checks on results of integration tests to ensure quality criteria are met
To run unit tests, the Surefire plugin is recommended. And Failsafe for integration tests.
The verify command executes each default lifecycle phase in order (validate, compile, package, etc.), before executing verify. In most cases the effect is the same as package. However, in case there are integration tests, these will be executed as well. And during the verify phase some additional checks can be done, e.g. if your code is written according to the predefined checkstyle rules.
Conclusion: if you want to run your integration tests and check it, use verify. If you only want to run unit tests, use test.
My personal advice: if in doubt, use verify.
How does Maven identify a specific test as a unit test or integration test?
Integrations Test always takes a name like IT.java or IT.java or *ITCase.java

Gradle Wrapper compile and build code and run a single unit test

Hey guys I am having a little trouble with the gradle wrapper.
Is there any good way to compile all my code, compile all my tests, and run a single test.
This is for a scripting command in a Jenkins pipeline.
I have used
./gradlew build, which runs all the tests
./gradlew build -x test, which compiles all main code and not allow me to run any tests
Thanks for any help
see the gradle documentation https://docs.gradle.org/current/userguide/java_testing.html#test_filtering
# Executes all tests in SomeTestClass
gradle test --tests SomeTestClass
# Executes a single specified test in SomeTestClass
gradle test --tests SomeTestClass.someSpecificMethod
gradle test --tests SomeTestClass.*someMethod*
# specific class
gradle test --tests org.gradle.SomeTestClass
# specific class and method
gradle test --tests org.gradle.SomeTestClass.someSpecificMethod

How to run only specific tests with Gradle and JUnit 5?

Using Gradle and its JUnit 4 support, I can choose a specific test using the --tests option like this:
$ ./gradlew test --tests de.mp.BarMT
This option is ineffective when using the JUnit 5 Gradle task. It is ignored silently when using the test tasks from the command line. The true work is done by junitPlatformTest anyway, and it does not support the option:
$ ./gradlew clean junitPlatformTest --tests de.mp.BarMT
…
Problem configuring task :junitPlatformTest from command line.
> Unknown command-line option '--tests'.
Does the JUnit 5 plugin support choosing specific tests?
Answering myself… A lot of time has passed, new versions came, and I can confirm that with Gradle 5.4.1 and JUnit Jupiter Platform 1.4.2, the simple configuration:
test {
useJUnitPlatform()
}
will allow running single tests as seen in https://stackoverflow.com/a/31468902/2621917, even when using weird names (from Kotlin):
gradle test --tests 'HtmlForwardInterceptorTest.should forward requests'

Exclude testng tests while building in jenkins

I have a test which consists of both junit test and testng tests. It works fine when i run 'mvn test' from parent pom, but testng tests fail while building in jenkins. I need a way to skip testng tests from running in jenkins.
You said you use jenkins so i assume that you are using maven plugin :
Have you tried to skip test cases in your Maven run? Use the code below in maven properties section of maven plugin:
maven.test.failure.ignore=true
Or
please use below code in properties section of maven plugin to skip the test cases
skipTests=true
Hope this helps
It's better to set it up via runtime in Jenkins' job configuration:
Invoke Top Level Maven -> Advanced.
Add maven.test.skip=true
Once the tests are OK, just remove this line.
You can disable test execution in runtime as well:
mvn -Dmaven.test.skip=true

Resources