Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xray integration #1902 #2089

Merged
merged 2 commits into from
Aug 22, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,32 @@ private static Throwable appendSteps(List<StepResult> steps, StringBuilder sb) {
return error;
}

private static Element addCustomTags(Element testCase, Document doc, ScenarioResult sr){
//Adding requirement and test tags
Element properties = null;

if (sr.getScenario() != null){
List<String> tags = sr.getScenario().getTagsEffective().getTags();
if (tags.size() > 0 ){
properties = doc.createElement("properties");

for (String tag : tags) {
String[] innerTags = tag.split("=");
int size = innerTags.length;
Element requirement = doc.createElement("property");

if(size > 1){
requirement.setAttribute("name", innerTags[0]);
requirement.setAttribute("value", innerTags[1]);
properties.appendChild(requirement);
}
}
}
}

return properties;
}

public static File saveJunitXml(String targetDir, FeatureResult result, String fileName) {
DecimalFormat formatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US);
formatter.applyPattern("0.######");
Expand Down Expand Up @@ -188,6 +214,13 @@ public static File saveJunitXml(String targetDir, FeatureResult result, String f
} else {
stepsHolder = doc.createElement("system-out");
}

Element properties = null;
properties = addCustomTags(testCase, doc, sr);
if(properties != null && properties.getChildNodes().getLength() > 0){
testCase.appendChild(properties);
}

testCase.appendChild(stepsHolder);
stepsHolder.setTextContent(sb.toString());
xmlString.append(XmlUtils.toString(testCase)).append('\n');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

import com.intuit.karate.core.Feature;
import com.intuit.karate.core.FeatureRuntime;
import com.intuit.karate.Suite;
import com.intuit.karate.FileUtils;
import com.intuit.karate.report.ReportUtils;
import org.junit.jupiter.api.Test;
import java.io.File;
import static org.junit.jupiter.api.Assertions.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -23,4 +28,15 @@ void testReport() {
report.render("target/report-test");
}

@Test
void testCustomTags() {
String expectedCustomTags = "<properties><property name=\"requirement\" value=\"CALC-2\"/><property name=\"test_key\" value=\"CALC-2\"/></properties>";
Feature feature = Feature.read("classpath:com/intuit/karate/report/customTags.feature");
FeatureRuntime fr = FeatureRuntime.of(new Suite(), feature);
fr.run();
File file = ReportUtils.saveJunitXml("target", fr.result, null);

assertTrue(FileUtils.toString(file).contains(expectedCustomTags));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Feature: Cusotm tags

@requirement=CALC-2
@test_key=CALC-2
Scenario:
* print 'cusomt tags are present in xml'
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@
import com.intuit.karate.Runner;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.BeforeClass;

/**
*
* @author pthomas3
*/
public class KarateJunitTest {

@BeforeClass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks ! just remove these and I will merge immediately

public static void beforeClass() {
System.setProperty("custom_tags", "test, requirement");
System.setProperty("custom_xml_tags", "test_key, requirement");
}

@Test
public void testAll() {
Results results = Runner
Expand All @@ -20,5 +27,14 @@ public void testAll() {
.parallel(5);
assertEquals(results.getErrorMessages(), 0, results.getFailCount());
}

@Test
public void testCustomTags() {
Results results = Runner
.path("classpath:com/intuit/karate/junit4/customTags")
.outputJunitXml(true)
.parallel(1);
assertEquals(results.getErrorMessages(), 0, results.getFailCount());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.intuit.karate.junit4.files;

import com.intuit.karate.junit4.Karate;
import com.intuit.karate.KarateOptions;
import org.junit.runner.RunWith;

@RunWith(Karate.class)
@KarateOptions(features = "classpath:com/intuit/karate/junit4/tags/customTags.feature")
public class CustomTagsRunner {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Feature: custom tags

@requirement=CALC-2
@test_key=CALC-2
Scenario: custom tags are present in xml
* print 'xray'

@requirement=CALC-3
Scenario: xray link to requirement
* print 'xray simple requirement'


@test_key=CALC-4
Scenario: xray link to test
* print 'xray simple test'

Scenario: no tags
* print 'without additional tags'

13 changes: 13 additions & 0 deletions karate-junit5/src/test/java/karate/SampleCustomTagsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package karate;

import com.intuit.karate.junit5.Karate;
import com.intuit.karate.Results;


class SampleCustomTagsTest {

@Karate.Test
Karate testXrayTags() {
return Karate.run("classpath:karate/customTags.feature").outputJunitXml(true);
}
}
24 changes: 24 additions & 0 deletions karate-junit5/src/test/java/karate/customTags.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Feature: cusotm tags test

@first
@requirement=CALC-2
@test_key=CALC-2
Scenario: xray simple scenario
* print 'xray simple example'

@second
@requirement=CALC-3
Scenario: xray link to requirement
* print 'xray simple requirement'


@third
@test=CALC-4
Scenario: xray link to test
* print 'xray simple test'

@fourth
Scenario: no tags
* print 'without additional tags'