I want to filter tests in different classes based on junit tags and try to execute tests using the Gradle command line argument.
-packageA
class TestCase0
#Test1
public void scenario(){
}
class TestCase1
#Test1
public void scenario(){
}
class TestCase2
#Test1
public void scenario(){
}
Tried the below command but it's not working:
./gradlew test --tests "packageA/*.java" -DincludeTags='Test1'
I have Junit5Runner class which starts Junit5 test programmatically.
It's not a maven project now, it's a simple java project with no any framework.
I need to switch to maven project, but in a maven project test classes are located inside test folder and aren't accessable from src folder.
How can I get access to test class from test folder in the main class from src folder?
Here is my Junit5Runner code.
Here I get access to CalculatorTest class.
public class Junit5Runner {
SummaryGeneratingListener listener = new SummaryGeneratingListener();
public void runOne() {
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
.selectors(selectClass(CalculatorTest.class)) //I NEED TO GET THIS CLASS FROM TEST FOLDER
.build();
Launcher launcher = LauncherFactory.create();
TestPlan testPlan = launcher.discover(request);
launcher.registerTestExecutionListeners(listener);
launcher.execute(request);
}
public static void main(String[] args) {
final Junit5Runner runner = new Junit5Runner();
runner.runOne();
TestExecutionSummary summary = runner.listener.getSummary();
for (TestExecutionSummary.Failure failure : summary.getFailures()) {
System.out.println(failure.getTestIdentifier().getDisplayName());
System.out.println(failure.getException());
}
}
}
The simplest solution is to put your production code int src/main/java/<package> and your unit tests into src/test/java/<packageName> and name your unit tests like *Test.java. If you follow that you can execute your unit tests (assumed you have added the appropriate dependencies for JUnit Jupiter in your pom file) via
mvn clean test
Full setup for JUnit Jupiter and example project can be found here.
I'm trying to run Cucumber's feature files in parallel using Cucumber's CLI Runner and I'm currently stuck trying to figure out how to make JUnit #BeforeClass hook to work with the CLI Runner.
At the moment, my working Runner class looks like this:
#RunWith(Cucumber.class)
#CucumberOptions(
plugin = {
"pretty",
"html:target/reports/basic/report.html",
"json:target/reports/cluecumber/cucumber.json",
"timeline:target/reports/timeline"
},
tags = "#RegressionTests",
snippets = SnippetType.CAMELCASE,
stepNotifications = true,
features = "classpath:features",
glue = "my.steps.package")
public class RegressionTestsIT {
#BeforeClass
public static void setup() {
ContextHolder.setupTestContext();
}
}
And my CLI command looks like this:
java -cp "target/test-jar-with-dependencies.jar" io.cucumber.core.cli.Main -p "pretty" -p "html:target/reports/basic/report.html" -p "json:target/reports/cluecumber/cucumber.json" -p "timeline:target/reports/timeline" --threads 10 -g "my.steps.package" target/test-classes/features
What happens is that I get a NullPointerException at the tests because TestContext was not properly set up as the hook was not executed.
I tried to include both the Runner's package and the Runner class itself as glue and it didn't work.
Also tried to make my Runner extend io.cucumber.core.cli.Main and then execute my Runner in the CLI and not surprisingly it did not work either, sadly still getting NPE.
Although this issue is related to the CLI Runner use, I'm content with any answer that might help me run multiple feature files in parallel whatever the method.
Using JUnit Rules
Cucumber supports JUnit's #ClassRule, #BeforeClass, and #AfterClass annotations. These will be executed before and after all scenarios. Using these is not recommended as it limits portability between different runners; they may not execute correctly when using the command line, IntelliJ IDEA, or Cucumber-Eclipse. Instead it is recommended to use Cucumber's hooks.
When using the CLI, JUnit is not involved at all so you can not use any of JUnit annotations. However since Cucumber v7 you can use #BeforeAll and #AfterAll to declare methods that executed before and after all scenarios.
package io.cucumber.example;
import io.cucumber.java.AfterAll;
import io.cucumber.java.BeforeAll;
public class StepDefinitions {
#BeforeAll
public static void beforeAll() {
// Runs before all scenarios
}
#AfterAll
public static void afterAll() {
// Runs after all scenarios
}
}
JUnit #BeforeClass didn't work for me. Since I'm kinda in a hurry with this, I didn't bother keep on trying to make it work. I don't really need to run the command in a pipeline at the moment, so I was completely fine in running it on IntelliJ as long as it was running in parallel.
My solution was creating a custom CLI Runner that runs the context configuration before Cucumber's CLI run method.
public class CLIRunner {
public static void main(String[] args) {
ContextHolder.setupTestContext();
io.cucumber.core.cli.Main.run(
new String[] {
"-p", "pretty",
"-p", "html:target/reports/basic/report.html",
"-p", "json:target/reports/cluecumber/cucumber.json",
"-p", "timeline:target/reports/timeline",
"-g", "my.steps.package",
"classpath:features",
"--threads", "10"
}, Thread.currentThread().getContextClassLoader());
}
}
I am using a suite file in TestNG to define the tests I want to run. Those suites are triggered with a jenkins job and now I need to make it optional to exclude a specific group or not.
I thought about adding an additional build parameter in jenkins and add a flag to the system properties if this parameter is set like so -DexcludeMyGroup=true. In some #BeforeSuite or #BeforeTest method in my base test I want to check for the property and its value. Depending on that I want to exclude that group from my suite.
I have tried
#BeforeTest
public void beforeTest(XmlTest test) {
if (!Boolean.parseBoolean(System.getProperty("excludeMyGroup"))) {
test.addExcludedGroup("myGroup");
}
}
as well as
#BeforeSuite
public void beforeSuite(ITestContext context) {
if (!Boolean.parseBoolean(System.getProperty("excludeMyGroup"))) {
cont.getSuite().getXmlSuite().addExcludedGroup("myGroup");
}
}
but both do not work.
I have tried to use the second approach to modify other parameters such as thread count and this works fine using cont.getSuite().getXmlSuite().setThreadCount(10) but I have not yet found a way to exclude a specific group besides the suite file. Is there a possibility to exclude this afterwards?
I found a couple ways to do this:
You can also run a TestNG suite programmatically in the main method, and use command line strings to define what groups to exclude (http://static.javadoc.io/org.testng/testng/6.11/org/testng/TestNG.html#setExcludedGroups-java.lang.String-):
public static void main(String[] args) {
TestNG tng = new TestNG();
tng.setExcludedGroups("excludedGroup1, excludedGroup2");
tng.run();
}
Then you could run the class file from the terminal and simply do
$ java <classfilename> excludedgroup1 excludedgroup2
and write the main function as the below:
public static void main(String[] args) {
TestNG tng = new TestNG();
tng.setExcludedGroups(args[0] + ", " + args[1]);
tng.run();
}
TestNG has a command-line switch called -excludegroups that will take a comma separated list of the groups you want to exclude, if you run the testng.xml file from the command line: http://testng.org/doc/documentation-main.html#running-testng.
Run it through Maven's surefire plugin. Go to the "excluded groups" part of this page -- you can define them in the pom.xml this way.
I am triyng to write testng listeners for my testing project cucumber, testng, selenium and java.
I have created Listeners extends TestListenerAdapter and implemented all the methods and included in testng.xml
`<listeners> <listener class-name="TestNGListeners.Listeners"></listener>
`<test name="Smoke">
`<packages> <package name="cucumber.runner.*"> </package>
`</test>
This xml calling a testrunner class RunCukesTest
`#CucumberOptions(features ="classpath:features",
`glue ="stepDefinitions",
tags="#tag_Login2",
plugin={"pretty", "html:target/cucumber-html-report",
"json:target/cucumber-report.json"}
)
`public class RunCukesTest {
`#Test()
`public void run_cukes () throws IOException {
`System.out.println("Run Cuke is started..");
`TestNGCucumberRunner tr = new TestNGCucumberRunner(getClass());
`tr.runCukes(); }
`}
which is responsible for running all cucumber tests.
After running it I am not getting any response from my listener methods. Please help me to find the solution.
Example -: Whenever I want to run suite or runner these method should work so that I can write my functionality :
public void onTestSuccess(ITestResult tr){
if( ITestResult.SUCCESS== tr.SUCCESS)
{
System.out.println("Test result PASS..");
}
}
I would suggest rather use the service hooks of cucmber api to achieve what ever you want to achieve from listner classes. They possibly wint work with the cucumber runner engine.