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 b37c0b0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
2 changes: 2 additions & 0 deletions 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 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

1 comment on commit b37c0b0

@SamLimpic
Copy link

@SamLimpic SamLimpic commented on b37c0b0 Aug 21, 2024

Choose a reason for hiding this comment

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

This does not appear to resolve the issue introduced in 3231

For instance this file contains 2 nested test directories:

{
  "info": {
    "_postman_id": "123",
    "name": "Unique Collection"
  },
  "item": [
    {
      "name": "Positive Test Cases",
      "item": [
        {
          "name": "Features",
          "item": [
            {
              "name": "Request",
              "event": [
                {
                  "listen": "test",
                  "script": {
                    "exec": [
                      "pm.test(`Testing unique Feature`, () => {});"
                    ]
                  }
                }
              ]
            }
          ]
        }
      ]
    },
    {
      "name": "Negative Test Cases",
      "item": [
        {
          "name": "Validations",
          "item": [
            {
              "name": "Request",
              "event": [
                {
                  "listen": "test",
                  "script": {
                    "exec": [
                      "pm.test(`Testing unique Validation`, () => {});"
                    ]
                  }
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

This single collection contains 2 tests, following this directory hierarchy:

PositiveTestCases > Features > Request --> "Testing unique Feature"
NegativeTestCases > Validations > Request --> "Testing unique Validation"

I would expect the JUnit report to output these tests on the report, as well as the collection the tests came from:

UniqueCollection.Testing unique Feature
UniqueCollection.Testing unique Validation

Instead, I get this:

PositiveTestCasesFeaturesRequest.Testing unique Feature
NegativeTestCasesValidationsRequest.Testing unique Validation

As a result, when multiple collections are run they no longer display their respective Collection in the runner
Instead they list a series of directories without separation or punctuation, ending with the name of the failing request & test

This is much less helpful from a diagnostic perspective, where dozens of results for multiple collections are displayed simultaneously, with no additional indication of where each individual failure came from

PositiveTestCasesFeaturesRequest.Testing unique Feature
NegativeTestCasesValidationsRequest.Testing unique Validation
PositiveTestCasesFeaturesRequest.Testing unique Feature
NegativeTestCasesValidationsRequest.Testing unique Validation
PositiveTestCasesFeaturesRequest.Testing unique Feature
NegativeTestCasesValidationsRequest.Testing unique Validation
PositiveTestCasesFeaturesRequest.Testing unique Feature
NegativeTestCasesValidationsRequest.Testing unique Validation

vs

UnitTests.Testing unique Feature
UnitTests.Testing unique Validation
IntegrationTests.Testing unique Feature
IntegrationTests.Testing unique Validation
RegressionTests.Testing unique Feature
RegressionTests.Testing unique Validation
AcceptanceTests.Testing unique Feature
AcceptanceTests.Testing unique Validation

I would guess that this is the updated attribs.name.value displayed in place of the original formatted collection.name, but the impact of this change seems to be farther reaching than intended

Please sign in to comment.