diff --git a/karate-core/src/main/java/com/intuit/karate/report/ReportUtils.java b/karate-core/src/main/java/com/intuit/karate/report/ReportUtils.java index 79be5feaf..1eb8d5599 100644 --- a/karate-core/src/main/java/com/intuit/karate/report/ReportUtils.java +++ b/karate-core/src/main/java/com/intuit/karate/report/ReportUtils.java @@ -154,6 +154,32 @@ private static Throwable appendSteps(List 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 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.######"); @@ -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'); diff --git a/karate-core/src/test/java/com/intuit/karate/report/ReportUtilsTest.java b/karate-core/src/test/java/com/intuit/karate/report/ReportUtilsTest.java index ea454e010..ddc747e00 100644 --- a/karate-core/src/test/java/com/intuit/karate/report/ReportUtilsTest.java +++ b/karate-core/src/test/java/com/intuit/karate/report/ReportUtilsTest.java @@ -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; @@ -23,4 +28,15 @@ void testReport() { report.render("target/report-test"); } + @Test + void testCustomTags() { + String expectedCustomTags = ""; + 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)); + } + } diff --git a/karate-core/src/test/java/com/intuit/karate/report/customTags.feature b/karate-core/src/test/java/com/intuit/karate/report/customTags.feature new file mode 100644 index 000000000..a8b4dc971 --- /dev/null +++ b/karate-core/src/test/java/com/intuit/karate/report/customTags.feature @@ -0,0 +1,6 @@ +Feature: Cusotm tags + +@requirement=CALC-2 +@test_key=CALC-2 +Scenario: +* print 'cusomt tags are present in xml' diff --git a/karate-junit4/src/test/java/com/intuit/karate/junit4/KarateJunitTest.java b/karate-junit4/src/test/java/com/intuit/karate/junit4/KarateJunitTest.java index aec5fe3c5..4d0c22737 100644 --- a/karate-junit4/src/test/java/com/intuit/karate/junit4/KarateJunitTest.java +++ b/karate-junit4/src/test/java/com/intuit/karate/junit4/KarateJunitTest.java @@ -4,6 +4,7 @@ import com.intuit.karate.Runner; import static org.junit.Assert.*; import org.junit.Test; +import org.junit.BeforeClass; /** * @@ -11,6 +12,12 @@ */ public class KarateJunitTest { + @BeforeClass + 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 @@ -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()); + } } diff --git a/karate-junit4/src/test/java/com/intuit/karate/junit4/customTags/CustomTagsRunner.java b/karate-junit4/src/test/java/com/intuit/karate/junit4/customTags/CustomTagsRunner.java new file mode 100644 index 000000000..4ffe8cb14 --- /dev/null +++ b/karate-junit4/src/test/java/com/intuit/karate/junit4/customTags/CustomTagsRunner.java @@ -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 { + +} \ No newline at end of file diff --git a/karate-junit4/src/test/java/com/intuit/karate/junit4/customTags/customTags.feature b/karate-junit4/src/test/java/com/intuit/karate/junit4/customTags/customTags.feature new file mode 100644 index 000000000..f42572951 --- /dev/null +++ b/karate-junit4/src/test/java/com/intuit/karate/junit4/customTags/customTags.feature @@ -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' + diff --git a/karate-junit5/src/test/java/karate/SampleCustomTagsTest.java b/karate-junit5/src/test/java/karate/SampleCustomTagsTest.java new file mode 100644 index 000000000..84bf6068f --- /dev/null +++ b/karate-junit5/src/test/java/karate/SampleCustomTagsTest.java @@ -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); + } +} diff --git a/karate-junit5/src/test/java/karate/customTags.feature b/karate-junit5/src/test/java/karate/customTags.feature new file mode 100644 index 000000000..96aa488a0 --- /dev/null +++ b/karate-junit5/src/test/java/karate/customTags.feature @@ -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' + +