From b37c0b07166237611f8f9f6b5b02ee35a583a71a Mon Sep 17 00:00:00 2001 From: Udit Vasu Date: Fri, 16 Aug 2024 19:58:19 +0530 Subject: [PATCH] Fix JUnit reporter classname --- CHANGELOG.yaml | 2 ++ lib/reporters/junit/index.js | 19 +++++++++++++++---- test/cli/shallow-junit-reporter.test.js | 12 ++++++------ test/library/shallow-junit-reporter.test.js | 6 +++--- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.yaml b/CHANGELOG.yaml index 1cb0dffd8..b978c0079 100644 --- a/CHANGELOG.yaml +++ b/CHANGELOG.yaml @@ -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 diff --git a/lib/reporters/junit/index.js b/lib/reporters/junit/index.js index 447da1bc1..5e0f0d34c 100644 --- a/lib/reporters/junit/index.js +++ b/lib/reporters/junit/index.js @@ -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. @@ -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); @@ -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'); diff --git a/test/cli/shallow-junit-reporter.test.js b/test/cli/shallow-junit-reporter.test.js index db2f7e19e..f6bcc5fee 100644 --- a/test/cli/shallow-junit-reporter.test.js +++ b/test/cli/shallow-junit-reporter.test.js @@ -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'); @@ -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; @@ -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; @@ -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'); @@ -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; @@ -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; diff --git a/test/library/shallow-junit-reporter.test.js b/test/library/shallow-junit-reporter.test.js index e209bc670..811aa4866 100644 --- a/test/library/shallow-junit-reporter.test.js +++ b/test/library/shallow-junit-reporter.test.js @@ -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'); @@ -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; @@ -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;