Skip to content

Commit

Permalink
testng implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanbcook committed Nov 10, 2023
1 parent eaa4a39 commit 4c16a43
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/helpers/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const FORCED_ARRAY_KEYS = [
"assemblies.assembly.collection.test.traits.trait",
"testng-results",
"testng-results.suite",
"testng-results.suite.groups.group",
"testng-results.suite.groups.group.method",
"testng-results.suite.test",
"testng-results.suite.test.class",
"testng-results.suite.test.class.test-method",
Expand Down
59 changes: 52 additions & 7 deletions src/parsers/testng.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,46 @@ const TestResult = require('../models/TestResult');
const TestSuite = require('../models/TestSuite');
const TestCase = require('../models/TestCase');

function getTestCase(rawCase) {
// assemble a fully qualified test name (class.name)
function getFullTestName(raw) {
return "".concat(raw["@_class"], ".", raw["@_name"]);
}

// create a mapping between fully qualified test name and and group
function getSuiteGroups(rawSuite) {
let testCaseToGroupMap = new Map();

if (rawSuite.groups && rawSuite.groups.group.length > 0) {
let raw_groups = rawSuite.groups.group;
for (let i = 0; i < raw_groups.length; i++) {
let group_methods = raw_groups[i].method;
let groupName = raw_groups[i]["@_name"];
for (let j = 0; j < group_methods.length; j++) {
let method = group_methods[j];
let key = getFullTestName(method);
if (!testCaseToGroupMap.has(key)) {
testCaseToGroupMap.set(key, []);
}
testCaseToGroupMap.get(key).push(groupName);
}
}
}
return testCaseToGroupMap;
}

function getTestCase(rawCase, testCaseToGroupMap) {
const test_case = new TestCase();
test_case.name = rawCase["@_name"];
test_case.duration = rawCase["@_duration-ms"];
test_case.status = rawCase["@_status"];
const key = getFullTestName(rawCase);
if (testCaseToGroupMap.has(key)) {
let groups = testCaseToGroupMap.get(key);
test_case.meta_data.set("groups", groups.join(","));
groups.forEach(group => {
test_case.meta_data.set(group, "");
})
}
if (rawCase.exception) {
test_case.failure = rawCase.exception[0].message;
}
Expand All @@ -18,14 +53,18 @@ function getTestCase(rawCase) {
return test_case;
}

function getTestSuiteFromTest(rawTest) {
function getTestSuiteFromTest(rawTest, testCaseToGroupMap) {
const suite = new TestSuite();
suite.name = rawTest['@_name'];
suite.duration = rawTest['@_duration-ms'];
const rawTestMethods = [];
const rawClasses = rawTest.class;
for (let i = 0; i < rawClasses.length; i++) {
rawTestMethods.push(...rawClasses[i]['test-method'].filter(raw => !raw['@_is-config']));
let testMethods = rawClasses[i]['test-method'].filter(raw => !raw['@_is-config']);
testMethods.forEach(testMethod => {
testMethod["@_class"] = rawClasses[i]["@_name"]; // push className onto test-method
});
rawTestMethods.push(...testMethods);
}
suite.total = rawTestMethods.length;
suite.passed = rawTestMethods.filter(test => test['@_status'] === 'PASS').length;
Expand All @@ -38,7 +77,7 @@ function getTestSuiteFromTest(rawTest) {
}
suite.status = suite.total === suite.passed ? 'PASS' : 'FAIL';
for (let i = 0; i < rawTestMethods.length; i++) {
suite.cases.push(getTestCase(rawTestMethods[i]));
suite.cases.push(getTestCase(rawTestMethods[i], testCaseToGroupMap));
}
return suite;
}
Expand All @@ -49,11 +88,16 @@ function getTestSuite(rawSuite) {
suite.duration = rawSuite['@_duration-ms'];
const rawTests = rawSuite.test;
const rawTestMethods = [];
const testCaseToGroupMap = getSuiteGroups(rawSuite);
for (let i = 0; i < rawTests.length; i++) {
const rawTest = rawTests[i];
const rawClasses = rawTest.class;
for (let j = 0; j < rawClasses.length; j++) {
rawTestMethods.push(...rawClasses[j]['test-method'].filter(raw => !raw['@_is-config']));
let testMethods = rawClasses[i]['test-method'].filter(raw => !raw['@_is-config']);
testMethods.forEach(testMethod => {
testMethod["@_class"] = rawClasses[i]["@_name"]; // push className onto test-method
});
rawTestMethods.push(...testMethods);
}
}
suite.total = rawTestMethods.length;
Expand All @@ -67,7 +111,7 @@ function getTestSuite(rawSuite) {
}
suite.status = suite.total === suite.passed ? 'PASS' : 'FAIL';
for (let i = 0; i < rawTestMethods.length; i++) {
suite.cases.push(getTestCase(rawTestMethods[i]));
suite.cases.push(getTestCase(rawTestMethods[i], testCaseToGroupMap));
}
return suite;
}
Expand Down Expand Up @@ -107,12 +151,13 @@ function parse(file) {
}
} else if (suitesWithTests.length === 1) {
const suite = suitesWithTests[0];
const testCaseToGroupMap = getSuiteGroups(suite);
result.name = suite['@_name'];
result.duration = suite['@_duration-ms'];
const rawTests = suite.test;
const rawTestsWithClasses = rawTests.filter(_rawTest => _rawTest.class);
for (let i = 0; i < rawTestsWithClasses.length; i++) {
result.suites.push(getTestSuiteFromTest(rawTestsWithClasses[i]));
result.suites.push(getTestSuiteFromTest(rawTestsWithClasses[i], testCaseToGroupMap));
}
} else if (suitesWithTests.length === 0){
const suite = suites[0];
Expand Down
36 changes: 36 additions & 0 deletions tests/data/testng/groups.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<testng-results>
<suite name="Suite1" duration-ms="2000">
<groups>
<group name="group1">
<method signature="com.test.TestOne.test2()" name="test2" class="com.test.TestOne"/>
<method signature="com.test.TestOne.test1()" name="test1" class="com.test.TestOne"/>
</group>
<group name="group2">
<method signature="com.test.TestOne.test2()" name="test2" class="com.test.TestOne"/>
</group>
</groups>
<test name="test1">
<class name="com.test.TestOne">
<test-method status="FAIL" signature="test1()" name="test1" duration-ms="11"
started-at="2007-05-28T12:14:37Z" description="someDescription2"
finished-at="2007-05-28T12:14:37Z">
<exception class="java.lang.AssertionError">
<short-stacktrace>
<![CDATA[
java.lang.AssertionError
... Removed 22 stack frames
]]>
</short-stacktrace>
</exception>
</test-method>
<test-method status="PASS" signature="test2()" name="test2" duration-ms="11"
started-at="2007-05-28T12:14:37Z" description="someDescription1"
finished-at="2007-05-28T12:14:37Z">
</test-method>
<test-method status="PASS" signature="setUp()" name="setUp" is-config="true" duration-ms="15"
started-at="2007-05-28T12:14:37Z" finished-at="2007-05-28T12:14:37Z">
</test-method>
</class>
</test>
</suite>
</testng-results>
9 changes: 9 additions & 0 deletions tests/parser.testng.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1001,4 +1001,13 @@ describe('Parser - TestNG', () => {
assert.notEqual(null, result2);
});

it('assign groups to testcases in single suite', () => {
const result = parse({ type: 'testng', files: ['tests/data/testng/groups.xml'] });
assert.equal(result.suites[0].cases[0].meta_data.get("groups"), "group1");
assert.equal(result.suites[0].cases[0].meta_data.has("group1"), true);
// 2nd testcase has multiple groups
assert.equal(result.suites[0].cases[1].meta_data.get("groups"), "group1,group2");
assert.equal(result.suites[0].cases[1].meta_data.has("group1"), true);
assert.equal(result.suites[0].cases[1].meta_data.has("group2"), true);
});
});

0 comments on commit 4c16a43

Please sign in to comment.