intuit / judo

Judo is an easy-to-use Command Line Interface (CLI) Integration Testing Framework, driven from a simple yaml file that also contains assertions.
Other
51 stars 21 forks source link

Write results in xUnit format #19

Closed crshnburn closed 5 years ago

crshnburn commented 5 years ago

Tested using xunit-viewer

Screenshot 2019-04-01 at 22 29 06
ejfrancis commented 5 years ago

(original comment deleted since I used the wrong GitHub account)

This is awesome, thank you @crshnburn!

Looking ahead to the possibility of supporting multiple reporters since this isn't the first time they've been brought up, I think we could make this a bit more generic. Perhaps something like a reporter interface. This is just something I put together in a couple minutes off the top of my head, I'm certainly open to other suggestions you may have that can help prepare for more reporters in the future.

src/reporters/ReporterInterface

class ReporterInterface {
   /**
   *   @param {Object} params
   *   @param {Array} params.stepResults          list of step results after completion of tests
   *   @param {type} params.type                      type of this reporter
   *   @param {String} params.outputFile           file path that report is written to
   */
   constructor({ stepResults, type, outputFile ) {
     this.stepResults = stepResults;
     this.type = type;
     this.outputFile= outputFile
  }
  /**
  *
  * @returns {String} report file contents
  */
  generateReport() {
    throw new Error(`writeReport() not implemented for reporter ${this.type}`);
  }
}

then implement your new reporter adhering to that

src/reporter/XunitReporter.js

class XunitReporter extends ReporterInterface {
   constructor({ stepResults }){ 
      super({ 
         stepResults,
         type: 'XUnit',
         outputFile: './junit.xml'
       })
    }
   writeReport() {
         let previousStepFilePath = '';

        let xml = '';
        xml += `<testsuites name="Judo Tests">\n`;
        this.stepResults.forEach(stepResult => {
            if (stepResult.getStepFilePath() !== previousStepFilePath) {
            if (previousStepFilePath !== '') {
                xml += `   </testsuite>\n`;
            }
            xml += `   <testsuite name="${stepResult.getStepFilePath()}">\n`;
            previousStepFilePath = stepResult.getStepFilePath();
            }
            xml += `      <testcase name="${stepResult.getStepName()}" time="${truncateAfterDecimal(stepResult.getDuration() / 1000, 5)}">\n`;
            if (!stepResult.getPassed()) {
            xml += `         <failure message="${stepResult.getErrorMessage()}"></failure>\n`;
            }
            xml += `      </testcase>\n`;
        });
        xml += `   </testsuite>\n</testsuites>`;
        return xml;
   }
}

then in src/reporters/get-reporter.js

const getReporter = ({ options, stepResults }) => {
   if (options.junitReport) {
      return new XunitReporter({ stepResults });
   } else {
     return null;
   }
}
export {
  getReporter
}

then finally in judo.js

if (numStepFilesComplete === numStepFiles) {
    const reporter = getReporter({ options });
    if (reporter) {
       fs.writeFileSync(reporter.outputFile, reporter.writeReport())
    }
}
crshnburn commented 5 years ago

@ejfrancis Definitely like that idea, have implemented it and updated the tests.

ejfrancis commented 5 years ago

hey @hipstersmoothie it looks like I was removed from the judo-admin team so I'm unable to attach the appropriate labels anymore 🙁 could you add me back?

@crshnburn this repo uses intuit/auto to manage releases via the labels on PRs when they're merged. could you add the "minor" and "release" labels to this so that once it's merged it will perform a minor version bump and release it?

crshnburn commented 5 years ago

Sorry I don’t have permission to add labels either

ejfrancis commented 5 years ago

hey @crshnburn I was able to get permissions again to add labels. Sorry it took awhile, I no longer work at Intuit so I can't just run down the hall anymore

ejfrancis commented 5 years ago

looks like build failed due to test coverage

Jest: "global" coverage threshold for branches (75%) not met: 71.26%

I should really add a pre-push git hook so that we can't push up a branch if tests aren't meeting the threshold. I can add that in another PR and add a couple tests to increase our coverage

EDIT: they were working at one point apparently, but it looks like they may not be anymore. I'll investigate, it seems like the prepush script is there but the husky dependency for running them as git hooks isn't there anyomre