diff --git a/java-reporter-karate/README.md b/java-reporter-karate/README.md new file mode 100644 index 0000000..9fd4a11 --- /dev/null +++ b/java-reporter-karate/README.md @@ -0,0 +1,50 @@ +# Java reporter integration with Karate + +## Overview + +This simple demo shows how Testomat.io Java reporter works in your project. + +- Some will fail on purpose and other will be disabled for demo. + +## Installation + +1. Clone the repository + +```sh + git clone https://github.com/testomatio/examples.git + ``` +2. Change the directory + +```sh + cd java-reporter-karate +``` +3. Install dependencies with test skip + +```sh + mvn clean install -DskipTests +``` + +## Configurations + +**By default, the library runs with properties default values except `testomatio.api.key` and `testomatio.listening`** + +![properties image](img/properties.png) + +Add your project API key to the `testomatio.properties` file ad `testomatio.api.key` + +## Run + +Run tests with + +```bash + mvn test -Dtestomatio=tstmt_key #if you did not provide it in the `testomatio.properties` file +``` + +where `tstmt_key` is your Testomat.io key from a particular project. + +As a result, you will see a run report in your Project tab -> Runs on Testomat.io. + +
+ demo report result png +
+ diff --git a/java-reporter-karate/img/properties.png b/java-reporter-karate/img/properties.png new file mode 100644 index 0000000..52328ef Binary files /dev/null and b/java-reporter-karate/img/properties.png differ diff --git a/java-reporter-karate/img/runReport.png b/java-reporter-karate/img/runReport.png new file mode 100644 index 0000000..00a950e Binary files /dev/null and b/java-reporter-karate/img/runReport.png differ diff --git a/java-reporter-karate/pom.xml b/java-reporter-karate/pom.xml new file mode 100644 index 0000000..72f832e --- /dev/null +++ b/java-reporter-karate/pom.xml @@ -0,0 +1,69 @@ + + + 4.0.0 + + io.testomat + java-reporter-karate + 1.0.0 + + + 11 + 11 + + 1.5.2 + 2.0.9 + 5.14.3 + 3.27.7 + 1.5.32 + + UTF-8 + + + + + io.testomat + java-reporter-karate + 0.8.17 + + + io.karatelabs + karate-core + ${karate-core.version} + provided + + + org.junit.jupiter + junit-jupiter + ${junit.version} + test + + + org.assertj + assertj-core + ${assertj.version} + test + + + ch.qos.logback + logback-classic + ${logback-classic.version} + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.2.2 + + + -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/1.9.24/aspectjweaver-1.9.24.jar" + + + + + + \ No newline at end of file diff --git a/java-reporter-karate/src/main/resources/logback.xml b/java-reporter-karate/src/main/resources/logback.xml new file mode 100644 index 0000000..40b854d --- /dev/null +++ b/java-reporter-karate/src/main/resources/logback.xml @@ -0,0 +1,32 @@ + + + + + %d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n + + + + + logs/karate.log + + + logs/karate.%d{yyyy-MM-dd}.log + 14 + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger - %msg%n + + + + + + + + + + + + + + diff --git a/java-reporter-karate/src/main/resources/testomatio.properties b/java-reporter-karate/src/main/resources/testomatio.properties new file mode 100644 index 0000000..3ab68b3 --- /dev/null +++ b/java-reporter-karate/src/main/resources/testomatio.properties @@ -0,0 +1,3 @@ +testomatio.run.title=Karate Tests +testomatio.url=https://app.testomat.io +testomatio= diff --git a/java-reporter-karate/src/test/java/KarateTest.java b/java-reporter-karate/src/test/java/KarateTest.java new file mode 100644 index 0000000..3284689 --- /dev/null +++ b/java-reporter-karate/src/test/java/KarateTest.java @@ -0,0 +1,25 @@ +import com.intuit.karate.Results; +import com.intuit.karate.Runner; +import hooks.MyHook; +import io.testomat.karate.hooks.KarateHookFactory; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +class KarateTest { + + @Test + void testParallel() { + + Results results = Runner.path("classpath:karateTests") + .hookFactory(KarateHookFactory.create(MyHook::new)) + .outputCucumberJson(true) + .outputJunitXml(true) + .parallel(4); + + Assertions.assertEquals( + 0, + results.getFailCount(), + results.getErrorMessages() + ); + } +} \ No newline at end of file diff --git a/java-reporter-karate/src/test/java/helpers/AssertStatus.java b/java-reporter-karate/src/test/java/helpers/AssertStatus.java new file mode 100644 index 0000000..022e740 --- /dev/null +++ b/java-reporter-karate/src/test/java/helpers/AssertStatus.java @@ -0,0 +1,16 @@ +package helpers; + +import io.testomat.core.annotation.Step; + +public class AssertStatus { + + @Step("Actual code is {0}\nExpected code is {1}") + public static void checkStatusCode(int actual, int expected) { + if (actual != expected) { + throw new AssertionError( + "Expected HTTP status " + expected + " but was " + actual + ); + } + } + +} diff --git a/java-reporter-karate/src/test/java/hooks/MyHook.java b/java-reporter-karate/src/test/java/hooks/MyHook.java new file mode 100644 index 0000000..5c60752 --- /dev/null +++ b/java-reporter-karate/src/test/java/hooks/MyHook.java @@ -0,0 +1,61 @@ +package hooks; + +import com.intuit.karate.RuntimeHook; +import com.intuit.karate.Suite; +import com.intuit.karate.core.FeatureRuntime; +import com.intuit.karate.core.ScenarioRuntime; +import com.intuit.karate.core.Step; +import com.intuit.karate.core.StepResult; +import com.intuit.karate.http.HttpRequest; +import com.intuit.karate.http.Response; + +public class MyHook implements RuntimeHook { + + public boolean beforeScenario(ScenarioRuntime sr) { + System.out.println("!!! Before Scenario !!!"); + return true; + } + + public void afterScenario(ScenarioRuntime sr) { + System.out.println("!!! After Scenario !!!"); + } + + public void afterScenarioOutline(ScenarioRuntime sr) { + System.out.println("!!! After Scenario Outline !!!"); + } + + public boolean beforeFeature(FeatureRuntime fr) { + System.out.println("!!! Before Feature !!!"); + return true; + } + + public void afterFeature(FeatureRuntime fr) { + System.out.println("!!! After Feature !!!"); + } + + public void beforeSuite(Suite suite) { + System.out.println("!!! Before Suite !!!"); + } + + public void afterSuite(Suite suite) { + System.out.println("!!! After Suite !!!"); + } + + public boolean beforeStep(Step step, ScenarioRuntime sr) { + System.out.println("!!! Before Step !!!"); + return true; + } + + public void afterStep(StepResult result, ScenarioRuntime sr) { + System.out.println("!!! After Step !!!"); + } + + public void beforeHttpCall(HttpRequest request, ScenarioRuntime sr) { + System.out.println("!!! Before HttpCall !!!"); + } + + public void afterHttpCall(HttpRequest request, Response response, ScenarioRuntime sr) { + System.out.println("!!! After HttpCall !!!"); + } + +} diff --git a/java-reporter-karate/src/test/resources/karateTests/posts.feature b/java-reporter-karate/src/test/resources/karateTests/posts.feature new file mode 100644 index 0000000..46ec574 --- /dev/null +++ b/java-reporter-karate/src/test/resources/karateTests/posts.feature @@ -0,0 +1,47 @@ +Feature: Posts API + + Background: + * url 'https://jsonplaceholder.typicode.com' + * def assertStatus = Java.type('helpers.AssertStatus') + + @Title:Get_all_posts @TestId:Tpost0001 @attachments:logs/karate.log + Scenario: Get all posts + Given path 'posts' + When method get + Then eval assertStatus.checkStatusCode(responseStatus, 200) + And match response[0].id != null + + @Title:Get_single_post @TestId:Tpost0002 + Scenario: Get single post + Given path 'posts', 1 + When method get + Then eval assertStatus.checkStatusCode(responseStatus, 200) + And match response.id == 1 + + @Title:Get_comments_for_post @TestId:Tpost0003 + Scenario: Get comments for post + Given path 'posts', 1, 'comments' + When method get + Then eval assertStatus.checkStatusCode(responseStatus, 200) + And match response[0].postId == 1 + + @Title:Validate_post_titles + Scenario Outline: Validate post titles + Given path 'posts', + When method get + Then eval assertStatus.checkStatusCode(responseStatus, 200) + And match response.title != null + + Examples: + | id | TestId | + | 1 | Tpost0041 | + | 2 | Tpost0042 | + | 3 | Tpost0043 | + + @Title:Create_post @TestId:Tpost0005 + Scenario: Create post + Given path 'posts' + And request { title: 'foo', body: 'bar', userId: 1 } + When method post + Then eval assertStatus.checkStatusCode(responseStatus, 200) + And match response.id != null