Single JUnit5 test execution with Gradle - java

I tried to run only one test class from Gradle. I used these commands:
gradle -Dtest.single=Junit5Test test
gradle test --tests ru.tests.runner.Junit5Test
But it runs all of the project's tests. I need to run only one test class. Here is my class:
public class Junit5Test {
public static void main(String[] args) {
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
.selectors(
selectPackage("ru.tests.api")
)
.build();
Launcher launcher = LauncherFactory.create();
launcher.execute(request, new TestExecutionListener());
}
}
It works perfect by right-click run. I use
IntellijIDEA 2017.1.5,
Gradle,
junitPlatformVersion = '1.0.0-M6',
junitJupiterVersion = '5.0.0-M6'

For starters, your Junit5Test class is not a "test class".
Rather, it is a stand-alone application (i.e., a Java class with a main() method).
Thus, attempting to execute it as a "test" with Gradle will never work.
If you would like to run JUnit Jupiter tests (or any other tests that run on the JUnit Platform) with Gradle, you should use the JUnit Platform Gradle Plugin (that's a link to the appropriate section of the JUnit 5 User Guide).

I resolved it by replacing
gradle -Dtest.single=Junit5Test test
with
gradle -DjunitPlatformTest.single=Junit5Test test

Related

Can't run individual Cucumber scenarios using TestNG test runner

Is there anyway to run cucumber scenarios using the TestNG runner by right clicking individual scenarios in the feature files rather than using the command file or running the TestNG runner file directly?
I'm using Intellij to run cucumber scenarios in a maven testing framework. In the POM.xml file I have the Surefire plugin referencing the testNG.xml file which points to the TestNG runner class.
When I run "mvn test" from terminal it calls the TestNG testrunner,but when I right click on the scenario in the feature file to select run, it runs the io.cucumber.core.cli.main class that calls the io.cucumber.core.runner.Runner class testrunner.
I can't edit Run/Debug configuration to use the TestNG runner because the TestNG runner doesn't have a Main method.
you can create a runner class and add tags you need into CucumberOptions.
#CucumberOptions(plugin = {"pretty"}, strict = true, tags = {"#yourTag"})
public class RunTestNGTest extends AbstractTestNGCucumberTests {
}
If anybone runs into the same issue, I managed to get around the issue by creating another class with a main method and calling the mvn command to run the test from there. something like this:
String mvnCommand = "mvn test ";
ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe", "/c", mvnCommand + args);
processBuilder.start();
Not sure if there's a better solution, but I can't seem to find any other option with Cucumber for Java plugin.

What is the correct way to run GWT JUnit test when using GWT `tbroyer` maven plugin?

What is the proper approach to run a JUnit test for a GWT project that uses tbroyer maven plugin
I have created a sample application to showcase a failing test that throws
Loading inherited module 'com.todogwt.App'
[ERROR] Unable to find 'com/todogwt/App.gwt.xml' on your classpath; could be a typo, or maybe you forgot to include a classpath entry for source?
Here is the code: https://github.com/incube8r/todogwt
The test is just as very simple
public class TestHelloWorld extends GWTTestCase {
#Override
public String getModuleName() {
return "com.todogwt.App";
}
public void test() {
Window.alert("Hello world!");
Console.log("Hello world!");
delayTestFinish(1000);
finishTest();
}
}
With the test module.gwt.xml located under
src/test/module.gwt.xml
What is the correct way to run JUnit test when using GWT tbroyer maven plugin?
gwt:test by default wants you to use test suites: https://tbroyer.github.io/gwt-maven-plugin/test-mojo.html#includes; you can run with -Dtest=TestHelloWorld but you'll have to update to the latest version of the gwt-maven-plugin (1.0-rc-10 for now); or you could redefine the <includes> to match your test.
Last, but not least, src/test/module.gwt.xml is not used by anything; only src/main/module.gwt.xml is. You don't have any difference between those files though, and your problem is actually that your test class is not in the source path for the module: move it to the client subpackage to make it run (and pass).

How can I run a single Serenity test runner class (among several) in Gradle?

I'm using Serenity + JBehave in Java, run by Gradle. I have several test runner classes pointing at different sets of stories. I need to be able to specify which one I run. One runner file is below to give an example of my implementation there.
import net.serenitybdd.jbehave.SerenityStories;
public class Debug extends SerenityStories {
public Debug() {
findStoriesIn("**/ldap");
}
}
You can do following in build.gradle, so when ever you run
task runSpecificRunner(type: Test) {
include '**/**YOURRunnerName.class'
}
Then run ./gradlew runSpecificRunner aggregate, you will see only that specific runner will be executed.

Why is JUnit 4 test outcome in Eclipse dependent on previous outcome of mvn test?

I am running Eclipse 4.4.1 with JUnit 4.12 as a Maven dependency (scope = test). I'm running the tests both with Eclipse and with Maven.
I have a project with a main class, Corpus2Json, which I would like to test. I noticed that unit tests weren't failing when I expected them to, so I cut it down to the most basic test which should fail.
package path.to.package;
import static org.junit.Assert.*;
import org.junit.Test;
public class Corpus2JsonTest {
#Test
public void test() {
fail();
}
}
It didn't fail in Eclipse, but it did in Maven. I checked again in Eclipse and it failed. I removed the fail() line and the test still failed in Eclipse.
So I created a new test class with identical code to the above except named Corpus2JsonsTest, and it failed as expected in Eclipse. I removed fail(), and it no longer failed. Once I ran mvn test, Eclipse began only returning the result of the previous Maven test.
There appears to be behavior that makes it so when Eclipse runs the test, it only has the result of the latest Maven test. Unless the test file did not exist since the previous Maven test, in which case it acts normally.

How to run a Cucumber-JVM feature file from the command line

I have a file with file extension .feature. HOw do run this from the command line?
In order to make a batch file for each feature. I am using Cucumber-JVM with Java and Selenium.
Cucumber-JVM is based on JUnit so its just like running any unit tests from the command line
java -cp /path/to/junit.jar org.junit.runner.JUnitCore [test class name]
where test class name is annotated with #CucumberOptions whose features
refer to the feature file. If youre using Maven you could use
mvn test
If you are using Maven, you can run your .feature file this way:
mvn -Dcucumber.options="from/the/root/of/module/to/the/feature/MyFeature.feature" test
This will override the #CucumberOptions in the test runner class.
You can run any test runner or override default test runner by using the command line below. This means you only want to run the test named SmokeTestRunner. By the way, you are free to set surefire plugin in your pom.xml any way. For example, you can set up your surefire plugin in your pom.xml to run the regression test named RegressionTestRunner. It doesn't matter.
mvn -Dtest=**/SmokeTestRunner.java test
Your SmokeTestRunner java file should look like this. ->
#CucumberOptions(
features = "src/test/resources/features/Smoke.feature",
glue = {"stepdefinitions", "hooks"},
tags = "#chrome", // It runs only scenarios tagged with "#chrome"
plugin = {"pretty"/*you can add more cucumber plugins here if you want.*/},
monochrome = true)
public class SmokeTestRunner extends AbstractTestNGCucumberTests {}
For more details, have a look at the maven surefire plugin docs. (https://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html)

Categories