gradle test: how to run one method? - testing

How to run just one method of a test case for debugging with Gradle? I have tried:
gradle test -tests example.TestFoo#testMethod1 --debug-jvm
but it ends up with following error:
No tests found for given includes: example.TestFoo#testMethod1
The test TestFoo class has testMethod1(), testMethod2(), etc.

Use . instead # in your tests filter expression to point to a method name:
gradle test --tests example.TestFoo.testMethod1 --debug-jvm
You can find more examples on filtering tests in 48.14.3. Test filtering documentation section.

Related

What is the gradle command to run scenarios with tags?

I am using Gradle 7.6, Karate 1.3.1, Java 17.0.5 and Junit 5.8.1.
I want to configure a Jenkin job for each feature to create a health check monitor. I need gradle commands to run feature files using tags #smoke, #regression, #featureName etc.,
I have tried with the following command, it worked earlier and stopped working recently.
./gradlew test -Dkarate.options="--tags #smoke" -Dtest.single=TestRunner#testTagsWithoutFeatureName
Where TestRunner is the following Java class
import com.intuit.karate.junit5.Karate;
public class TestRunner {
#Karate.Test
Karate testTagsWithoutFeatureName() {
return Karate.run().tags("#smoke").relativeTo(getClass());
}
}
My advice is use the Runner class, that is better designed for running tests in CI. The JUnit helpers are just for local-dev convenience: https://stackoverflow.com/a/65578167/143475
It should be possible to even pass a feature to karate.options as the last argument. Which might be more convenient than writing a Java class for every combinations. You should experiment.
Otherwise no suggestions, but if you feel there's a bug, follow this process: https://github.com/karatelabs/karate/wiki/How-to-Submit-an-Issue

Intellij IDE Not Running Unit Tests

I am running IntelliJ IDEA 2018.3.1 and am attempting test a class with the integrated test runner. The test seems to compile but not run.
This is a multi-module Maven project, and other modules have tests that run. However, I have not been able to find any differences between the projects. The surefire plugin is specifically defined on this project, and <skipTests> is specifically set to false. I have reimported the project several times in case the maven configuration is affecting the built-in runner.
The image below is the only output I get. Debug/Breakpoints will not stop.
If anyone can help or throw possibilities at me, I would appreciate it.
Edit:
Here's a simplified version of the test I'm attempting to run:
package com.jason;
// imports
#RunWith(BlockJUnit4TestRunner.class)
public class MyTest {
private ClassUnderTest clazz;
private DaoClass dao;
#Before
public void setUp() {
// using Mockito to mock the DaoClass
// injecting the DAO into the ClassUnderTest
}
#Test
public void testMethod() {
Assert.assertTrue(true);
}
}
I attempt to run the test by right-clicking on the method annotated with #Test and clicking run. The option to run the test DOES appear in the context menu. When I do so, all that appears is the screenshot.
I have attempted to do the following to troubleshoot the issue:
In the pom.xml file for the appropriate module, I have manually specified the surefire plugin in the <build><plugins> section. I then did a reimport to pick up the changes.
I have put breakpoints in the code and run the test in debug mode.
I have attempted to log output, both with an slf4j logger and a System.out.println()
I have attempted to find any differences in the IDEA .iml file between a module where the tests run and this module where the tests do not run.
I have written a very simple test class, with a method annotated with #Test and containing the line Assert.assertTrue(true)
Edit 2
Attempting to run mvn test -Dcontrollername produces the following output:
Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test (default-test) on project rma-svc: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test failed: The forked VM terminated without properly saying goodbye. VM crash or System.exit called?
Edit 3
I've updated my Maven surefire plugin to 2.22.2 and am not seeing the forked JVM issue any longer. However, running mvn test -DskipTests=false outputs No tests were executed!

How to run groovy based JUnit Test Suite from the command line?

How to run the JUnit test suite containing a set of test cases(groovy based) from the command line. Following is the test suite class generated by eclipse.
package com.example.testclasses;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
#RunWith(Suite.class)
#SuiteClasses({ abc.class, xyz.class })
public class AllTests {
}
The above test suite works when I run the above test suite(AllTests)as JUnit from eclipse, however, I want to run the test suite(AllTests) from the command line. How do I do this?
Info: I am using Geb(Groovy) based testing where all the test cases(example: abc, def) are groovy based(having .groovy extension).
If you wish to run your tests from the command line I would suggest using a build system. My personal choice would be to use Gradle but you could probably also get away with using Maven.
The benefit of using a build system, apart from being able to run the tests from the command line, is that it will help you manage your dependencies and it will be easier to build the project for others working on the same codebase - they won't have to manually setup all the dependencies and their versions in the IDE.
Try this:
java -cp /path/to/groovy/embeddable/groovy-all-1.8.1.jar groovy.lang.GroovyShell AllTests.groovy
where 1.8.1 should be replaced with your version of groovy-all-*.jar

how to enable UnitHelper in codeception for unit tests

I am trying to setup unit testing using codeception. I have followed the guide and have the following:
unit.suite.yml:
class_name: UnitTester
modules:
enabled: [UnitHelper, Asserts]
When I try to run codecept build, I get the following exception:
[Codeception\Exception\Configuration]
UnitHelper could not be found and loaded
UnitHelper is not a built-in class, but a helper class generated for unit suite.
You have to execute this command to create it:
codecept generate:suite unit Unit

run single integration test with gradle

I'm trying to run a single integration tests using gradle's -Dtest.single flag. I have added another source set, src/integrationTest and put the tests in there. I have an integration test task
task integrationTests(type: Test) {
dependsOn 'assemble', 'integrationTestClasses'
testClassesDir = sourceSets.integrationTest.output.classesDir
classpath = sourceSets.integrationTest.runtimeClasspath
}
This runs fine, but if I try to run a single test it tells me it cannot find a matching test. I don't want to have to run every integration test each time I am writing a new one. Is there a way to do this?
Since Gradle 1.10 you can write:
//select specific test method
gradle test --tests org.gradle.SomeTest.someFeature
//select specific test class
gradle test --tests org.gradle.SomeTest
//select all tests from package
gradle test --tests org.gradle.internal*
//select all ui test methods from integration tests by naming convention
gradle test --tests *IntegTest*ui*
//selecting tests from different test tasks
gradle test --tests *UiTest integTest --tests *WebTest*ui
Read more here
http://www.gradle.org/docs/1.10/release-notes#executing-specific-tests-from-the-command-line
The correct syntax is:
gradle testTaskName -DtestTaskName.single=...
In this case:
gradle integrationTest -DintegrationTest.single=...
Just incase anyone is coming here looking for answers. This has been removed in gradle 5.0. Look for test.single in https://docs.gradle.org/current/userguide/upgrading_version_4.html
If you still wish to use a command line option in this style you should be able to use the --tests commandline param. See https://docs.gradle.org/current/userguide/java_testing.html#simple_name_pattern
$ ./gradlew integrationTest --tests=MyTest