Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions java-reporter-karate/README.md
Original file line number Diff line number Diff line change
@@ -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.

<div align="center">
<img src="img/runReport.png" alt="demo report result png" style="max-width: 70%; max-height: 420px;">
</div>

Binary file added java-reporter-karate/img/properties.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added java-reporter-karate/img/runReport.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 69 additions & 0 deletions java-reporter-karate/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.testomat</groupId>
<artifactId>java-reporter-karate</artifactId>
<version>1.0.0</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>

<karate-core.version>1.5.2</karate-core.version>
<slf4j.version>2.0.9</slf4j.version>
<junit.version>5.14.3</junit.version>
<assertj.version>3.27.7</assertj.version>
<logback-classic.version>1.5.32</logback-classic.version>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>io.testomat</groupId>
<artifactId>java-reporter-karate</artifactId>
<version>0.8.17</version>
</dependency>
<dependency>
<groupId>io.karatelabs</groupId>
<artifactId>karate-core</artifactId>
<version>${karate-core.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback-classic.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<argLine>
-javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/1.9.24/aspectjweaver-1.9.24.jar"
</argLine>
</configuration>
</plugin>
</plugins>
</build>
</project>
32 changes: 32 additions & 0 deletions java-reporter-karate/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<configuration>

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/karate.log</file>

<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logs/karate.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>14</maxHistory>
</rollingPolicy>

<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger - %msg%n</pattern>
</encoder>
</appender>

<logger name="com.intuit.karate.http" level="DEBUG" additivity="false">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</logger>

<root level="INFO">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>

</configuration>
3 changes: 3 additions & 0 deletions java-reporter-karate/src/main/resources/testomatio.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
testomatio.run.title=Karate Tests
testomatio.url=https://app.testomat.io
testomatio=
25 changes: 25 additions & 0 deletions java-reporter-karate/src/test/java/KarateTest.java
Original file line number Diff line number Diff line change
@@ -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()
);
}
}
16 changes: 16 additions & 0 deletions java-reporter-karate/src/test/java/helpers/AssertStatus.java
Original file line number Diff line number Diff line change
@@ -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
);
}
}

}
61 changes: 61 additions & 0 deletions java-reporter-karate/src/test/java/hooks/MyHook.java
Original file line number Diff line number Diff line change
@@ -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 !!!");
}

}
47 changes: 47 additions & 0 deletions java-reporter-karate/src/test/resources/karateTests/posts.feature
Original file line number Diff line number Diff line change
@@ -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 <TestId>
Given path 'posts', <id>
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