I have an automation test that I use Cucumber, Junit and I can run with Java Application.
Runner Class:
#RunWith(Cucumber.class)
#CucumberOptions(strict = false, features = "src/main/java/FaixaVS_NaoCadastrado/FaixaVS_NaoCadastrado/FaixaVS_NaoCadastrado.feature", glue = { "StepDefinition" }, format = { "pretty",
"json:C:/Automação Receba Em Casa/Evidências/FVS_NaoCadastrado/Relatório/cucumber.json" }, tags = { "~#ignore" })
public class Runner {
}
Jar Class:
public class Jar {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(Runner.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
}
}
I can run it inside Eclipse as Java Application, but when I Export it like a Runnable jar and run it I receive the follow massage from CMD:
C:\Users\c.guiao.de.oliveira>java -jar
C:\Users\c.guiao.de.oliveira\Desktop\FVS_NaoCadastrado.jar
initializationError(FaixaVS_NaoCadastrado.FaixaVS_NaoCadastrado.Runner):
Expected a file URL:rsrc:cucumber-java-1.2.3.jar
Can you help me?
Few options:
Verify that the path you are using to get resource is correct. I
encounter tests that IntelliJ (that is not case sensitive when it
comes to resource path) executed successfully and maven clean
install failed.
Verify that cucumber dependencies are part of the jar. It can work in IntelliJ if you mistakenly added it to the classpath instead of adding it to the pom.xml file.
Related
I want to develop a stand-alone test-solution delivered as a jar that can be used in a CI/CD environment without being recompiled all the time. Therefore I packed a fat-jar from a multi-maven-module containing a few libaries, a Spring Boot application and a submodule called test-runner.
Executing the fat-jar from within GitLab CI/CD works, but I think that was only the first half of it. I want to produce a JUnit XML report to output the test-results. What I understood from my research is that I would have to implement my own reporter. Is there a more complete example out there?
The test runner
public class Runner {
SummaryGeneratingListener listener = new SummaryGeneratingListener();
public void runOne() {
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
.selectors(selectClass(MyTest.class)).build();
Launcher launcher = LauncherFactory.create();
TestPlan testPlan = launcher.discover(request);
launcher.registerTestExecutionListeners(listener);
launcher.execute(testPlan);
}
public static void resultReport(Result result) {
System.out.println("Finished. Result: Failures: " + result.getFailureCount() + ". Ignored: "
+ result.getIgnoreCount() + ". Tests run: " + result.getRunCount() + ". Time: "
+ result.getRunTime() + "ms.");
}
public static void main(String[] args) {
Runner runner = new Runner();
runner.runOne();
TestExecutionSummary summary = runner.listener.getSummary();
summary.printTo(new PrintWriter(System.out));
}
}
Background:
My test-solution is generic and uses a configuration file to parameterize the tests. All tests run in parallel versus a system-under-test. So before this attempt all gitlab-jobs called mvn test to execute the tests and generate the reports, but it recompiled everything every run. I thought about speeding things up.
To generate XML reports, you can use the LegacyXmlReportGeneratingListener with a path to save your reports to as first argument:
LegacyXmlReportGeneratingListener xmlListener = new LegacyXmlReportGeneratingListener(Paths.get("reports"), new PrintWriter(System.out));
In your runOne() method, you need to register your listener accordingly:
public void runOne() {
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
.selectors(selectClass(MyTest.class)).build();
Launcher launcher = LauncherFactory.create();
TestPlan testPlan = launcher.discover(request);
launcher.registerTestExecutionListeners(listener);
launcher.registerTestExecutionListeners(xmlListener);
launcher.execute(testPlan);
}
This will generate one XML file per test root in the folder you passed to the listener during initialization.
More information can be found in the JavaDoc
You can use Console launcher to geneate Junit5 xml reports
java -jar junit-platform-console-standalone-1.6.2.jar #junitArgs.txt --reports-dir=reports
junitArgs.txt file has following info:
-classpath fat jar path
--scan-classpath
I have built a cucumber test executable jar using the maven-assembly-plugin option and was successfully run the executable jar that run all the cucumber test within it.
However, I like to try to run the executable with optional tags specified on the command line but could not seem to do so. Any help and suggestion would be greatly appreciate.
My sample code is in github:
https://github.com/txt8888/cucumber-executable
You could use the main() method of the Main class from the io.cucumber.core.cli.Main package.
public class DreamCarMain {
public static void main(String[] args) {
Main.main(new String[] { "-g", "org.phan.kata.cucumber.integration.stepdefs", "-p", "pretty", "-t", args[0], "classpath:features" });
}
}
There is no need for the RunWith and CucumberOptions annotation. Replace the TagExpression with whatever tags u want to execute. If you want to do some action after the call then use the run() method instead of the main().
java -jar cucumber-integration-1.0.0-jar-with-dependencies.jar #TagExpression
I have created a test automation framework using maven and cucumber.
1) I want to create a jar file which includes everything (all project files)
2) Then I want to run a test from the command line using above created jar like using the command
(mvn clean test -Dcucumber.options='--tags #all')
I don't want to use the main method or anything.
java -Dcucumber.options="--tags #all" -jar your-test-jar.jar
Try this. Although I am not sure why you don't want to use the main method. If you don't use the main method it will just become too complicated.
Update:
Write a main method and run Cucumber main method from it. The arguments are what you would pass in as your Cucumber command line arguments.
public static void main(String[] args) throws Throwable {
String[] arguments = {"a", "b"};
cucumber.api.cli.Main.main(arguments);
}
If I have understood your question clearly, this might do your work.
This should help you run Cucumber from your executable.
The below code worked for me to execute the cucumber tests from runnable jar with test frame work as TestNG.
Executing jar: java -jar ProductsAutomation-0.0.1-SNAPSHOT-jar-with-dependencies.jar
import io.cucumber.core.cli.Main;
public static void main(String args[]) throws Throwable {
try {
Main.main(new String[] {
"-g","com.sadakar.cucumber.common",
"-g","com.sadakar.cucumber.runner",
"classpath:features",
"-t","#SmokeTest",
"-p", "pretty",
"-p", "json:target/cucumber-reports/cucumber.json",
"-p", "html:target/cucumber-reports/cucumberreport.html",
"-m"
}
);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Main method exception : " + e);
}
}
Gradle provides a default App.java java source file and its test class at the time of creation of project.
I created java class under main:
/src/main/java/some/package/algo/Sort.java
I would like to create a test class that corresponds to it.
/test/main/java/some/package/algo/SortTest.java
I can do that manually but is it possible that Gradle or any gradle command does it for me whenever I create a new regular Java Source file.
I am not using IDE and IDE provide such options.
Similarly any gradle command, If I have a test file than Gradle create java source file under main source.
task createTestForSource {
inputs.dir 'src/main/java'
outputs.dir 'src/test/java'
doLast {
fileTree('src/main/java').visit { FileVisitDetails fvd ->
if (!fvd.directory) {
String sourcePath = fvd.relativePath.asPath
String testPath = sourcePath.replace('.java', 'Test.java')
File testFile = file("src/test/java/$testPath")
if (!testFile.exists()) {
testFile.parentFile.mkdirs()
testFile.text = // do your magic here
}
}
}
}
}
I'm using Gradle to build my software. However, I find the output it proceduces a bit to minimal. I don't want to use --debug or --info, since that logging is much to verbose. I just want to know what the result in terms of artifacts (zip, jar, dmg, etc) of the Gradle buid is. For example, when I run 'gradle jar', I'd like to print where the jar is created.
I did that using:
jar {
doLast {
println "Jar has been created in ${archivePath}"
}
}
And it nicely prints that the jar has been created in the build/lib directory. However, when I run 'gradle distZip', the artifact is not created in the lib dir, but in the distributions directory. The above however is still printed, but I'd rather not have that: when I run the distZip, I'd like to know where I can find the output of that command, not of every step the distZip depends on.
Never mind, the following will work just nicely:
def artifacts = []
addListener(new TaskExecutionListener() {
void afterExecute(Task task, TaskState state) {
if(task in AbstractArchiveTask) {
artifacts << task.outputs.files.singleFile
}
}
void beforeExecute(Task task) { }
})
addBuildListener(new BuildAdapter() {
void buildFinished(BuildResult result) {
if(artifacts) {
println "\nOutput location: ${artifacts.last()}\n"
}
}
})
This is also available as a gist here.