Skip to content

Commit

Permalink
Fix JUnit reporter classname
Browse files Browse the repository at this point in the history
  • Loading branch information
codenirvana committed Aug 16, 2024
1 parent 901d1ec commit b4bfce5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
unreleased:
new features:
- GH-3263 Added support for HTTP/2
fixed bugs:
- GH-3231 Fixed a bug where JUnit reporter sets the wrong classname
chores:
- >-
GH-3258 Converted the deprecated `apikey` parameter in Postman API urls
Expand All @@ -20,7 +22,7 @@ unreleased:
- Updated dependencies

6.1.1:
date: 2024-02-05
date: 2024-02-05'
chores:
- Updated dependencies

Expand Down
19 changes: 15 additions & 4 deletions lib/reporters/junit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@ var _ = require('lodash'),
xml = require('xmlbuilder'),

util = require('../../util'),
JunitReporter;
JunitReporter,

/**
* Normalizes the name of a test suite to a valid class name.
*
* @private
* @param {String} name - The name of the test suite.
* @returns {String} - The class name for the test suite.
*/
getClassName = (name) => {
return _.upperFirst(_.camelCase(name).replace(/\W/g, ''));
};

/**
* A function that creates raw XML to be written to Newman JUnit reports.
Expand All @@ -27,7 +38,7 @@ JunitReporter = function (newman, reporterOptions) {
return;
}

classname = _.upperFirst(_.camelCase(collection.name).replace(/\W/g, ''));
classname = getClassName(collection.name);

root = xml.create('testsuites', { version: '1.0', encoding: 'UTF-8' });
root.att('name', collection.name);
Expand Down Expand Up @@ -126,8 +137,8 @@ JunitReporter = function (newman, reporterOptions) {
testcase.att('time', executionTime.toFixed(3));

// Set the same classname for all the tests
testcase.att('classname', _.get(testcase.up(), 'attribs.name.value',
classname));
testcase.att('classname',
getClassName(_.get(testcase.up(), 'attribs.name.value')) || classname);

if (failures && failures.length) {
failure = testcase.ele('failure');
Expand Down
12 changes: 6 additions & 6 deletions test/cli/shallow-junit-reporter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('JUnit reporter', function () {
testcase = suite.testcase[0];
expect(testcase).to.not.be.empty;

expect(testcase.$).to.have.property('classname', 'ExampleCollectionWithASingleGetRequest');
expect(testcase.$).to.have.property('classname', 'ASimpleGetRequest');

expect(suite.$).to.have.property('tests', '1');
expect(suite.$).to.have.property('failures', '0');
Expand Down Expand Up @@ -83,7 +83,7 @@ describe('JUnit reporter', function () {
testcase = suite.testcase[0];
expect(testcase).to.not.be.empty;

expect(testcase.$).to.have.property('classname', 'ExampleCollectionWithFailingTests');
expect(testcase.$).to.have.property('classname', 'StatusCodeTest');
expect(testcase.$.time).to.match(/^\d+\.\d{3}$/);
expect(testcase.failure).to.not.be.empty;
expect(testcase.failure[0]._).to.not.be.empty;
Expand Down Expand Up @@ -124,7 +124,7 @@ describe('JUnit reporter', function () {
testcase = suite.testcase[0];
expect(testcase).to.not.be.empty;

expect(testcase.$).to.have.property('classname', 'AssertionErrorTest');
expect(testcase.$).to.have.property('classname', 'FailedRequest');
expect(testcase.$.time).to.match(/^\d+\.\d{3}$/);
expect(testcase.failure).to.not.be.empty;
expect(testcase.failure[0]._).to.not.be.empty;
Expand Down Expand Up @@ -183,7 +183,7 @@ describe('JUnit reporter', function () {
testcase = suite.testcase[0];
expect(testcase).to.not.be.empty;

expect(testcase.$).to.have.property('classname', 'NestedRequestInTest');
expect(testcase.$).to.have.property('classname', 'NestedRequestTest');

expect(suite.$).to.have.property('tests', '2');
expect(suite.$).to.have.property('failures', '0');
Expand Down Expand Up @@ -224,7 +224,7 @@ describe('JUnit reporter', function () {
testcase = suite.testcase[0];
expect(testcase).to.not.be.empty;

expect(testcase.$).to.have.property('classname', 'NestedRequestInTestWithFailingTest');
expect(testcase.$).to.have.property('classname', 'NestedRequestTest');
expect(testcase.$.time).to.match(/^\d+\.\d{3}$/);
expect(testcase.failure).to.not.be.empty;
expect(testcase.failure[0]._).to.not.be.empty;
Expand All @@ -235,7 +235,7 @@ describe('JUnit reporter', function () {
testcase = suite.testcase[1];
expect(testcase).to.not.be.empty;

expect(testcase.$).to.have.property('classname', 'NestedRequestInTestWithFailingTest');
expect(testcase.$).to.have.property('classname', 'NestedRequestTest');
expect(testcase.$.time).to.match(/^\d+\.\d{3}$/);
expect(testcase.failure).to.not.be.empty;
expect(testcase.failure[0]._).to.not.be.empty;
Expand Down
6 changes: 3 additions & 3 deletions test/library/shallow-junit-reporter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('JUnit reporter', function () {
testcase = suite.testcase[0];
expect(testcase).to.not.be.empty;

expect(testcase.$).to.have.property('classname', 'ExampleCollectionWithASingleGetRequest');
expect(testcase.$).to.have.property('classname', 'ASimpleGetRequest');

expect(suite.$).to.have.property('tests', '1');
expect(suite.$).to.have.property('failures', '0');
Expand Down Expand Up @@ -91,7 +91,7 @@ describe('JUnit reporter', function () {
testcase = suite.testcase[0];
expect(testcase).to.not.be.empty;

expect(testcase.$).to.have.property('classname', 'ExampleCollectionWithFailingTests');
expect(testcase.$).to.have.property('classname', 'StatusCodeTest');
expect(testcase.$.time).to.match(/^\d+\.\d{3}$/);
expect(testcase.failure).to.not.be.empty;
expect(testcase.failure[0]._).to.not.be.empty;
Expand Down Expand Up @@ -137,7 +137,7 @@ describe('JUnit reporter', function () {
testcase = suite.testcase[0];
expect(testcase).to.not.be.empty;

expect(testcase.$).to.have.property('classname', 'AssertionErrorTest');
expect(testcase.$).to.have.property('classname', 'FailedRequest');
expect(testcase.$.time).to.match(/^\d+\.\d{3}$/);
expect(testcase.failure).to.not.be.empty;
expect(testcase.failure[0]._).to.not.be.empty;
Expand Down

0 comments on commit b4bfce5

Please sign in to comment.