What is the gradle command to run scenarios with tags? - karate

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

Related

Karate summary reports not showing all tested features after upgrade to 1.0.0

I have recently upgraded to version 1.0.0 from 0.9.6 and noticed that the generated karate-summary.html file, it doesn't display all the tested feature files in the JUnit 5 Runner unlike in 0.9.6.
What it displays instead was the last tested feature file only.
The below screenshots are from the provided SampleTest.java sample code (excluding other Tests for simplicity).
package karate;
import com.intuit.karate.junit5.Karate;
class SampleTest {
#Karate.Test
Karate testSample() {
return Karate.run("sample").relativeTo(getClass());
}
#Karate.Test
Karate testTags() {
return Karate.run("tags").relativeTo(getClass());
}
}
This is from Version 0.9.6.
And this one is from Version 1.0.0
However, when running the test below in 1.0.0, all the features are displayed in the summary correctly.
#Karate.Test
Karate testAll() {
return Karate.run().relativeTo(getClass());
}
Would anyone be kind to confirm if they are getting the similar result? It would be very much appreciated.
What it displays instead was the last tested feature file only.
This is because for each time you run a JUnit method, the reports directory is backed up by default. Look for other directories called target/karate-reports-<timestamp> and you may find your reports there. So maybe what is happening is that you have multiple JUnit tests that are all running, so you see this behavior. You may be able to over-ride this behavior by calling the method: .backupReportDir(false) on the builder. But I think it may not still work - because the JUnit runner has changed a little bit. It is designed to run one method at a time, when you are in local / dev-mode.
So the JUnit runner is just a convenience. You should use the Runner class / builder for CI execution, and when you want to run multiple tests and see them in one report: https://stackoverflow.com/a/65578167/143475
Here is an example: ExamplesTest.java
But in case there is a bug in the JUnit runner (which is quite possible) please follow the process and help the project developers replicate and then fix the issue to release as soon as possible.

How to make Karate Tests run as a service [duplicate]

I am using karate 0.9.2 with gradle. My project requires to have all karate tests inside src/main/java. So I configured the gradle dependency as ‘compile’ instead of ‘testCompile’ and also modified the sourceSets to point to main instead of test. When I ran my runner class with above configuration I got empty test suite message.
build.gradle snippet:
compile 'com.intuit.karate:karate-junit4:0.9.3'
compile 'com.intuit.karate:karate-apache:0.9.3'
sourceSets {
test {
resources {
srcDir file('src/main/java')
exclude '**/*.java'
}
}
}
Additionally, I have is to run the karate tests from the deployable project jar. Please point me the resources I can refer to achieve the same.
Not something we directly support but teams have done this in Spring Boot etc. It should be possible, see if this thread helps: https://github.com/intuit/karate/issues/520
Also you may not need JUnit also: https://github.com/intuit/karate/issues/427
And see the sample project in this ticket as an example: https://github.com/intuit/karate/issues/529
EDIT - in 1.0 onwards we hope that class-loading from spring-boot JAR files is more reliable: https://github.com/intuit/karate/issues/751

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

Run Cucumber JVM #BeforeClass Without Feature Files

The title may be a bit confusing at this point; hopefully I can clear it up.
What I Have
I'm running Cucumber JVM with Selenium WebDriver to automate our system test cases. These test cases are currently stored in JIRA using the XRay Test Management plugin. XRay also provides APIs to fetch the feature files as well as upload the results back to JIRA.
I have created a custom JIRA utility class to download the tests as feature files and upload the test results from and to JIRA - as well as demonstrated that it does work. These are run in the #BeforeClass and #AfterClass in the Cucumber Runner class respectively.
I have also demonstrated that the developed test framework does work by manually running with feature files created on my computer.
What I Want
I want to be able to (eventually) run the automation test framework automatically with our CI tools. Along with this, it would pull the defined automation tests from JIRA and push the test results back to JIRA.
I do not want the feature files stored with the code. In my opinion, this defeats the purpose of it being dynamic as the tests we execute will change over time (in number executed and the steps themselves).
What Is Happening (Or More Specifically, Not Happening)
When I try to execute the Cucumber Runner class without any feature files in the framework, Cucumber says "No features found at [src/test/resources/features/]". This is understandable since there are no feature files (yet).
However, it does not run the #BeforeClass; thus it does not download the feature files to be run. I have tried this both with and without tags in the runner class.
Code
#RunWith(Cucumber.class)
#CucumberOptions(
tags={"#smoketests"},
features= {"src/test/resources/features/"},
plugin={"json:target/reports/cucumber.json"},
monochrome=true)
public class RunCucumberTest {
#BeforeClass
public static void executeBeforeTests() {
JiraUtil.getFeatureFiles();
//String browser = "firefox";
String browser = "chrome";
//String browser = "safari";
//String browser = "edge";
//String browser = "ie";
DriverUtil.getInstance().setDriver(browser);
}
#AfterClass
public static void executeAfterTests() {
DriverUtil.getInstance().resetDriver();
JiraUtil.uploadTestResults();
}
}
Back To My Question
How can I execute the JIRA Util code so I can download the feature files?
Is it possible to achieve what I want? Or do I have to admit defeat and just have all the feature files stored with the code?
This is the expected behavior when using JUnit. A test suite will not invoke the #BeforeClass, #AfterClass or #ClassRule when there are no tests in the suite or if all tests are ignored[1]. This avoids the execution of a potentially expensive setup for naught.
This does mean you can't use a class rule to bootstrap your tests. Nor should you attempt to do so. In a build process it is a good practice to fetch all sources and resources prior to compilation.
If you are using maven could write a maven instead and attach it to the generate-test-sources phase[2]. Creating a maven plugin is a bit more involved then a JUnit Rule but not prohibitively so. Check the Guide to Developing Java Plugins.
I assume there are similar options for Gradle.