Is it possible to override the classpath declared in Runner.path() with the command-line? - karate

I'm having a karate test project where my folder structure would be like below, I have organized the folder (hunting-skills) based on the type. I have configured cucumber reporting and parallel execution with 1 thread, below is the code for parallel execution and cucumber reporting
import com.intuit.karate.Results;
import com.intuit.karate.Runner;
import net.masterthought.cucumber.Configuration;
import net.masterthought.cucumber.ReportBuilder;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import static org.junit.jupiter.api.Assertions.*;
public class FeatureTests {
#Test
public void testAll() {
Results results = Runner.path("classpath:hunting-skills").tags("~#ignore").parallel(1);
generateReport(results.getReportDir());
assertEquals(0, results.getFailCount(), results.getErrorMessages());
}
public static void generateReport(String karateOutputPath) {
Collection<File> jsonFiles = FileUtils.listFiles(new File(karateOutputPath), new String[] {"json"}, true);
ArrayList<String> jsonPaths = new ArrayList<>(jsonFiles.size());
jsonFiles.forEach(file -> jsonPaths.add(file.getAbsolutePath()));
Configuration config = new Configuration(new File("target"), "someTests");
ReportBuilder reportBuilder = new ReportBuilder(jsonPaths, config);
reportBuilder.generateReports();
}
}
src/test/java
|
+--karate-config.js
+--FeatureTests.java
|
\-- hunting-skills
\-- animals
|
+-- lionTest.feature
+-- tigerTest.feature
|
\-- birds
|
+-- valtureTest.feature
+-- eagleTest.feature
|
The complete suite ran when the following command ./gradlew test -Dkarate.options="--tags ~#ignore" -Dtest=FeatureTests is run in terminal (say git bash or Linux terminal) which is expected.
But at times I want to run a specific folder or *.feature file. I have got commands from the karate GitHub page https://github.com/intuit/karate
(to run specific folder) ./gradlew test "-Dkarate.options=--tags ~#ignore classpath:hunting-skills/animals Dtest=FeatureTests
(to run specific file) ./gradlew test "-Dkarate.options=--tags ~#ignore classpath:hunting-skills/animals/lionTest.feature Dtest=FeatureTests
When I run the command for a specific file, the complete suite ran. Please help

Write another test entry point that does not set the path() or read system properties manually and use Java code and if-then conditions. Don't waste your time trying to "re use".

Related

Cucumber+Appium - 'CukesRunner' class is not running and displays exit code 0 in console

I have setup a Gradle project to automate an Android application using Cucumber and Appium with Java. I also wanted to use POM to capture the elements page-wise. The project structure looks like:
I wants to add Extent Report to get testcase execution status. Now when i am running CukesRunner file:
package runners;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
#RunWith(Cucumber.class)
#CucumberOptions(
plugin = {
"json:target/cucumber.json",
"html:target/default-html-reports",
"rerun:target/rerun.txt"
},
features = {"src//test//resources//Features"},
glue = {"stepdefinition"},
dryRun = false,
tags = "#paybackTest"
)
public class CukesRunner {
}
It displays the exit code 0 error with some unknown error:
My appium server is running on 127.0.0.1:4723 and also an Android device connected through USB (i cross-checked it with 'adb devices').

Junit 5 suite is not running tests in command line

I am trying to run TestSuite using Junit 5. Individual file run fine. But TestSuite is not running when executed from command line. I am using junit-platform-console-standalone-1.6.0.jar to run tests.
My Test classes are:
package demo;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
public class TestDemoClass1 {
#Test
public void test1() throws Exception {
System.out.println("Test 1 from DemoClass 1");
}
#Test
public void test2() throws Exception {
System.out.println("Test2 from DemoClass 1");
}
}
My Suite calss is:
package demo;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;
#Suite
#SelectClasses({ TestDemoClass1.class })
public class TestSuite {
}
The command I am usign to run from command lines:
java -jar target/junit-platform-console-standalone-1.6.0.jar -cp .;target/test-classes/ -c demo.TestSuite
This is resolved ":https://github.com/junit-team/junit5/issues/2813.
The issue was not adding set of jars while runnign command needed for console launcher.
Additional info in above GitHub issue link but in summary you need to add following jars to classpath:
junit-platform-console-standalone-1.8.2.jar;
junit-platform-suite-api-1.8.2.jar;
junit-platform-suite-commons-1.8.2.jar;
junit-platform-suite-engine-1.8.2.jar; \

How to configure kotlin script project with dependency on current module?

I'd like to have:
directory with .kts files inside
classic src/main/kotlin/my/package module with classes
And have usage like:
kotlin my-script.main.kts
Where script looks like my-script.main.kts this from the example
#!/usr/bin/env kotlin
#file:Repository("https://jcenter.bintray.com")
#file:DependsOn("org.jetbrains.kotlinx:kotlinx-html-jvm:0.6.11")
import kotlinx.html.*; import kotlinx.html.stream.*; import kotlinx.html.attributes.*;
import my.package.*;
val addressee = args.firstOrNull() ?: "World"
print(createHTML().html {
body {
h1 { +"Hello, $addressee! from ${my.package.Greater.hello()}" }
}
})
Restrictions:
no jar dependencies, at first publishing jar to maven repo then run script
clean design, it should be extendable and as not hello world project
How to configure kotlin script project with dependency on current module?
What are the best practices so far?

Getting org.junit.runner.RunWith is not an annotation type with Cucumber

I'm trying to set-up a Selenium + Cucumber project in Java (I'm a beginner with Java) in Intellij. This is my class that uses the Cucumber JUnit runner:
package runtest;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
#RunWith(Cucumber.class)
#CucumberOptions (
features = "C:\\Users\\my-feature.feature",
)
public class RunTest {
}
I'm getting these errors:
Error:(8, 1) java: annotation #org.junit.runner.RunWith is missing default values for elements annotationType,<init>
Error:(8, 2) java: org.junit.runner.RunWith is not an annotation type
I don't know how to resolve this or what is happening. Please help.
I had multiple RunWith classes in my project path. After pruning the project tree, it works as expected.
You don't have to provide the absolute path of the feature file if you opt to place it within your project space. The name of the feature file folder would be enough.
As an example, if you are having the following structure:
Package: Q43966680
Class: Runner
Feature File Folder: q43966680
Snapshot:
As per your usecase within #CucumberOptions you just need to mention:
#RunWith(Cucumber.class)
#Cucumber.Options (features="q43966680")
public class RunTest {
}

Grails (JUnit) test suite

Do JUnit test suites work in Grails?
I am writing a Groovy/Grails(2.1.2) project using IntelliJ.
Is it possible to run Spock tests as a JUnit test suite? I have tried:
package com.foo
import com.foo.functional.SampleFunctionalSpec
import org.junit.experimental.categories.Categories
import org.junit.runner.RunWith
import org.junit.runners.Suite
#RunWith(Categories.class)
#Categories.IncludeCategory(Category1.class)
#Suite.SuiteClasses( {SampleFunctionalSpec.class} )
class SampleTestSuite {
}
and
package com.foo.functional
import com.foo.*
import org.junit.experimental.categories.Category;
#Category(Category1.class)
class SampleFunctionalSpec extends BloomGebSpecCase {
def "Sample test 1 with Category1"() {
given:
when:
println "when"
then:
println "Sample test 1 with Category1"
}
}
and
package com.foo
interface Category1 { }
and I get "Unable to attach test reporter to test framework..." when I run grails test-app functional SampleTestSuite from within IntelliJ.
EDIT: Also, just running a test suite with no categories gives same "Unattached" problem:
#RunWith(Suite.class)
#Suite.SuiteClasses( SampleFunctionalSpec.class )
class SampleTestSuite { }