Junit 5 Launcher with jupiter-vintage-engine rule not honoring JUnit4 #ParametrizedTest - junit5

I am working on porting a JUnit4 based test framework that uses JUnitCore to launch unit tests with JUnit5 launcher, junit-vintage-engine and a LauncherDiscoveryRequest with ClassSelector.
It is working for all cases except #ParameterizedTest. Launcher is not honoring parameterized tests.
How do I get Launcher to honor JUnit4 parameterized tests?
I have tried adding testCompile("org.junit.jupiter:junit-jupiter-params:${junit_jupiter_version}")
package ...;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import ...TestCategory;
import ...TestType;
import ...DataScramblingRegexTest;
#TestType(...TestCategory.UNIT)
#RunWith(Parameterized.class)
public class RegexGenTest {
private static final int LOOKAROUND_ATTEMPTS = 100;
#Parameterized.Parameter(0)
public String regex;
#Parameterized.Parameters(name = "{index}: RegexGenTest: {0}")
public static Iterable<String[]> data() {
return DataScramblingRegexTest.data();
}
#Test
public void testTransformAndGenerate() {
//even though Data Scramble Random Text doesn't transform every regex,
//even if we do, and run it through Generex, it should still work.
final String transformedRegex = RegexGen.transform(this.regex);
String randomStr = RegexGen.generate(transformedRegex);
if (RegexGen.containsLookArounds(this.regex)) {
for (int i = 0; i < LOOKAROUND_ATTEMPTS; i++) {
randomStr = RegexGen.generate(transformedRegex);
if (randomStr.matches(this.regex)) {
return;
}
}
Assert.fail("Lookaround failed");
}
Assert.assertTrue("generated string does not match regex: " + randomStr, randomStr.matches(this.regex));
}
}
build.gradle
compile("junit:junit:${junit_version}")
testCompile("org.junit.vintage:junit-vintage-engine:${junit_jupiter_version}")
testCompile("org.junit.jupiter:junit-jupiter-api:${junit_jupiter_version}")
testCompile("org.junit.jupiter:junit-jupiter-params:${junit_jupiter_version}")
testCompile("org.junit.jupiter:junit-jupiter-engine:${junit_jupiter_version}")
where
junit_version=4.12
junit_jupiter_version=5.4.2
Note the proprietary TestCategory.UNIT annotation, we use this in our test discovery code and create a Launcher and a LauncherdiscoveryRequest with ClassSelector.
I expect JUnit 4 parameters to be supported by JUnit5 launcher with junit-jupiter-vintage engine and it is not supported.

Related

Threads did not exit in parallel execution in karate

Here is my code snippet for parallel execution:
package examples;
import com.intuit.karate.KarateOptions;
import com.intuit.karate.Results;
import com.intuit.karate.Runner;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
#KarateOptions(tags = {"~#ignore"})
public class ExamplesTest {
#Test
public void testParallel() {
List<String> tags = Arrays.asList("~#ignore");
List<String> features = Arrays.asList("classpath:examples/autocomment");
Results results = Runner.parallel(tags,features, 5, "target/surefire-reports");
// assertTrue(results.getErrorMessages(), results.getFailCount() == 0);
}
}
Once all feature files executed using command
mvn test -DargLine="-Dkarate.env=qa",threads did not exit and hence build never gets concluded.
Could you please let me know if I am doing anything wrong here??
`
One of your tests probably has a severe error (maybe the eval of karate-config.js). Run using the JUnit runner and see the logs.
This should be fixed in this ticket, do see if you can build from source and validate: https://github.com/intuit/karate/issues/667

Cucumber JVM cannot find the steps defined

When I run to run this test with a feature it complains it cannot find the steps. I have tried defining them in Java 8 style, and Java 7 style and using IntelliJ to generate the steps into a MyStepdefs class but it cannot find them.
I am using version 1.2.4 of cucumber-java8 and cucumber-junit.
import cucumber.api.CucumberOptions;
import cucumber.api.DataTable;
import cucumber.api.PendingException;
import cucumber.api.java8.En;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
#RunWith(Cucumber.class)
#CucumberOptions(
monochrome = true,
glue = {"com.mycom.core.agg.RunCukesTest"})
public class RunCukesTest implements En {
public RunCukesTest() {
Given("^I have PriceLevels$", (DataTable arg1) -> {
});
And("^I have a TradeRequest$", (DataTable arg1) -> {
});
Then("^I should get these LegRequests$", (DataTable arg1) -> {
});
}
}
running the test prints
Running com.mycom.core.agg.RunCukesTest
1 Scenarios (1 undefined)
3 Steps (3 undefined)
0m0.000s
You can implement missing steps with the snippets below:
Given("^I have PriceLevels$", (DataTable arg1) -> {
.. rest deleted ...
Running the feature file from IntelliJ give much the same error.
While we couldn't figure out what the cause was, create a new maven project with a minimum of dependencies "fixed" the problem. We didn't need to change the code.

Cucumber not running selenium Code

When I try to run my code, it only shows cucumber skeleton. I use a JUnit runner class as JUnit test suite.
Code is below for all three classes.
Feature is :
Feature: Check addition in Google calculator
In order to verify that google calculator work correctly
As a user of google
I should be able to get correct addition result
#Runme
Scenario: Addition
Given I open google
When I enter "2+2" in search textbox
Then I should get result as "4"
Selenium Class :
package cucumberTest;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class SeleniumTest {
private SeleniumTest()
{
}
private static WebDriver driver = null;
public static void seleniumTest() {
// Create a new instance of the Firefox driver
driver = new FirefoxDriver();
//Put a Implicit wait, this means that any search for elements on the page could take the time the implicit wait is set for before throwing exception
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//Launch the Online Store Website
driver.get("http://www.store.demoqa.com");
// Find the element that's ID attribute is 'account'(My Account)
driver.findElement(By.xpath(".//*[#id='account']/a")).click();
// Find the element that's ID attribute is 'log' (Username)
// Enter Username on the element found by above desc.
driver.findElement(By.id("log")).sendKeys("testuser_1");
// Find the element that's ID attribute is 'pwd' (Password)
// Enter Password on the element found by the above desc.
driver.findElement(By.id("pwd")).sendKeys("Test#123");
// Now submit the form. WebDriver will find the form for us from the element
driver.findElement(By.id("login")).click();
// Print a Log In message to the screen
System.out.println("Login Successfully");
// Find the element that's ID attribute is 'account_logout' (Log Out)
driver.findElement (By.xpath(".//*[#id='account_logout']/a")).click();
// Print a Log In message to the screen
System.out.println("LogOut Successfully");
// Close the driver
driver.quit();
}
}
JUnit Class:
package cucumberTest;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
//#CucumberOptions(
// features = "Feature/googleCalc.feature"
////,glue={"stepDefinition"}
// )
#CucumberOptions(
features = {"Feature/googleCalc.feature"},glue={"stepDefinition"},
plugin = {"pretty"},
tags = {"#Runme"}
)
public class TestRunner {
}
Step Definitions :
package stepDefination;
import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import cucumberTest.SeleniumTest;
public class googleCalcStepDefinition {
#Given("^I open google$")
public void i_open_google() throws Throwable {
// Write code here that turns the phrase above into concrete actions
SeleniumTest.seleniumTest();
}
#When("^I enter \"(.*?)\" in search textbox$")
public void i_enter_in_search_textbox(String arg1) throws Throwable {
// Write code here that turns the phrase above into concrete actions
SeleniumTest.seleniumTest();
}
#Then("^I should get result as \"(.*?)\"$")
public void i_should_get_result_as(String arg1) throws Throwable {
// Write code here that turns the phrase above into concrete actions
SeleniumTest.seleniumTest();
}
}
Output shown is :
Feature: Check addition in Google calculator
In order to verify that google calculator work correctly
As a user of google
I should be able to get correct addition result
#Runme
Scenario: Addition [90m# Feature/googleCalc.feature:7[0m
[33mGiven [0m[33mI open google[0m
[33mWhen [0m[33mI enter "2+2" in search textbox[0m
[33mThen [0m[33mI should get result as "4"[0m
1 Scenarios ([33m1 undefined[0m)
3 Steps ([33m3 undefined[0m)
0m0.000s
You can implement missing steps with the snippets below:
#Given("^I open google$")
public void i_open_google() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
#When("^I enter \"(.*?)\" in search textbox$")
public void i_enter_in_search_textbox(String arg1) throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
#Then("^I should get result as \"(.*?)\"$")
public void i_should_get_result_as(String arg1) throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
I found the reason it was not executing because i added Selenium test class and Junit runner class in Package cucumberTest; and skeleton was in package stepDefination; so what i did was i moved skeleton to pacakge cucumberTest; where junit runner and selenium classes are it resolved the issue for . Issue was occuring because when you place Junit class in package it will search for skeleton in that package but if you add JUnit runner class in source folder then it will be able to find the page under src folder

running selenium junit4 test suite tests multiple times

I have a test suite. I'd like to execute this entire test suite 1000 times. I have googled and looked everywhere and I can't seem to figure this out. Any help would be greatly appreciated!! If it would be easier to switch to testng, I will.
package com.bpms.tests;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
#RunWith(Suite.class)
#SuiteClasses({
InitiateBuyPlan.class,
AddItemsToBuyPlan.class,
ReviewBuyPlan.class,
ApproveBuyPlan.class,
ManageQuoteSolicitation.class,
StartQuote.class,
ReviewQuote.class,
RecordInterestInQuotes.class,
ReviewCountryBuyIn.class,
CompleteItemInfo.class,
ReviewItemInformation.class,
})
public class SuiteAllTests
{
}
You will have to build the test case programatically than via annotations
import org.junit.runners.AllTests;
import junit.framework.TestSuite;
import junit.framework.Test;
#RunWith(AllTests.class)
public final class SuiteAllTests {
public static TestSuite suite() {
TestSuite suite = new TestSuite();
for (int i= 0; i<1000; i++) {
suite.addTest(new JUnit4TestAdapter(XXX.class));
suite.addTest(new JUnit4TestAdapter(YYY.class));
}
return suite;
}
}
Just replace XXX and YYY with the classes you have in your annotations.

Restart failed test case automatically in TestNG/Selenium

I am using Selenium webdriver, in Java with TestNG to run an X amount of test cases.
What I would like, is for any test case to automatically restart (either from starting or from point of failure), as soon as it fails.
I know TestNG framework has the following method
#Override
public void onTestFailure(ITestResult tr) {
log("F");
}
but I do not know how to find out which testcase it was and then how would I restart it.
I wanted to see an example with actual code in it and found it here:
Restarting Test immediately with TestNg
Observe how the below tests will each be re-run once as soon as the failure happens.
import org.testng.Assert;
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
import org.testng.annotations.Test;
public class Retry implements IRetryAnalyzer {
private int retryCount = 0;
private int maxRetryCount = 1;
public boolean retry(ITestResult result) {
if (retryCount < maxRetryCount) {
retryCount++;
return true;
}
return false;
}
#Test(retryAnalyzer = Retry.class)
public void testGenX() {
Assert.assertEquals("james", "JamesFail"); // ListenerTest fails
}
#Test(retryAnalyzer = Retry.class)
public void testGenY() {
Assert.assertEquals("hello", "World"); // ListenerTest fails
}
}
From testng.org
Every time tests fail in a suite, TestNG creates a file called testng-failed.xml in the output directory. This XML file contains the necessary information to rerun only these methods that failed, allowing you to quickly reproduce the failures without having to run the entirety of your tests.
If you want to rerun the test exactly after the failure you need to call the method that failed. You can get that method name from ITestResult object.
If you want to rerun all the failed test cases together, then you can give the testng-failed.xml as input xml after the first execution.