Skip to content

Commit

Permalink
updating code
Browse files Browse the repository at this point in the history
  • Loading branch information
agracio committed Dec 9, 2024
1 parent db635a0 commit be144ce
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 39 deletions.
33 changes: 16 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

### Supported report formats

- JUnit XML Format
- JUnit/xUnit XML Format
- NUnit 3.0+ XML Format
- xUnit.net v2+ XML Format
- Visual Studio TRX Format
Expand Down Expand Up @@ -52,11 +52,10 @@ https://github.com/adamgruber/mochawesome
- Converts <skipped> test messages to Mochawesome test context values.
- Converts <properties>, <system-out> and <system-err> to Mochawesome context values.
- Converts <failure> and <error> elements to Mochawesome error stack.
- Test suites are displayed in alphabetical order by `file` and `classname` attributes.
- Tests suites without any tests are excluded from Mochawesome and JUnit.
- Attachments are not supported.

### JUnit
### JUnit

- Converts **<properties>**, **<system-out>** and **<system-err>** elements to Mochawesome test context.
- Nested test suites currently not supported.
Expand All @@ -68,7 +67,7 @@ https://github.com/adamgruber/mochawesome
- Converts **<output>** elements to JUnit **<system-out>**.
- Nested `test-suite` elements are flattened.

### xUnit
### xUnit.net

- xUnit.net v2+ XML is supported.
- Converts **<traits>** elements to to JUnit **<properties>**.
Expand Down Expand Up @@ -106,18 +105,18 @@ convert(options).then(() => console.log(`Report created: ${options.reportDir}/${

### Options

| Option | Type | Default | Description |
|:--------------------------|:--------|:--------------------------|:--------------------------------------|
| `testFile` **(required)** | string | | Path to test file for conversion |
| `testType` **(required)** | string | | Test report type |
| `reportDir` | string | ./report | Converted report output path |
| `reportFilename` | string | mochawesome.json | Mochawesome report name |
| `junit` | boolean | false | Create JUnit report? |
| `junitReportFilename` | string | `testFile.name`-junit.xml | JUnit report file name |
| `html` | boolean | false | Create Mochawesome HTML? |
| `htmlReportFilename` | string | mochawesome.html | Mochawesome HTML file name |
| `skippedAsPending` | boolean | true | Show skipped tests as pending in HTML |
| `switchClassnameAndName` | boolean | false | Switch test case classname and name |
| Option | Type | Default | Description |
|:--------------------------|:--------|:--------------------------|:---------------------------------------------|
| `testFile` **(required)** | string | | Path to test file for conversion |
| `testType` **(required)** | string | | Test report type |
| `reportDir` | string | ./report | Converted report output path |
| `reportFilename` | string | mochawesome.json | Mochawesome report name |
| `junit` | boolean | false | Create JUnit report? |
| `junitReportFilename` | string | `testFile.name`-junit.xml | JUnit report file name |
| `html` | boolean | false | Create Mochawesome HTML? |
| `htmlReportFilename` | string | mochawesome.html | Mochawesome HTML file name |
| `skippedAsPending` | boolean | true | Show skipped tests as pending in Mochawesome |
| `switchClassnameAndName` | boolean | false | Switch test case classname and name |

- `testFile` - relative or absolute path to input test file.
- `testType` - type of test report, not case-sensitive.
Expand All @@ -130,7 +129,7 @@ convert(options).then(() => console.log(`Report created: ${options.reportDir}/${

| `testType` | File Type |
|:-----------|:------------------|
| JUnit | JUnit |
| JUnit | JUnit/xUnit |
| NUnit | NUnit 3.0+ XML |
| xUnit | xUnit.net v2+ XML |
| TRX | Visual Studio TRX |
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export declare type TestReportConverterOptions = {
reportFilename? : string
junit?: boolean
junitReportFilename? : string
transformJunit: boolean
html?: boolean
htmlReportFilename? : string
skippedAsPending?: boolean
Expand Down
6 changes: 6 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function config (options) {
let htmlReportFilename = 'mochawesome.html';
let saveIntermediateFiles = false;
let junit = false;
let transformJunit = false;
let junitReportFilename = `${path.parse(options.testFile).name}-junit.xml`;

if(options.skippedAsPending === false || options.skippedAsPending === 'false'){
Expand Down Expand Up @@ -78,6 +79,10 @@ function config (options) {
junit = true;
}

if(options.transformJunit === true || options.transformJunit === 'true'){
transformJunit = true;
}

if(options.junitReportFilename){
junitReportFilename = options.junitReportFilename;
}
Expand All @@ -95,6 +100,7 @@ function config (options) {
reportPath: path.join(reportDir, reportFilename),
junit: junit,
junitReportFilename: junitReportFilename,
transformJunit: transformJunit,
html: html,
htmlReportFilename: htmlReportFilename,
saveIntermediateFiles: saveIntermediateFiles,
Expand Down
2 changes: 1 addition & 1 deletion src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async function convert(options){

switch (config.testType) {
case 'junit':
await junit.convert(config, null);
config.transformJunit ? await xsltConverter(config) : await junit.convert(config, null);
break;
case 'nunit':
case 'xunit':
Expand Down
1 change: 1 addition & 0 deletions src/interfaces.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface ConverterOptions{
reportFilename? : string
junit?: boolean
junitReportFilename? : string
transformJunit? : boolean
html?: boolean
htmlReportFilename? : string
saveIntermediateFiles?: boolean
Expand Down
3 changes: 3 additions & 0 deletions src/junit.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ function parseXml(options, xml){
}
if(json && json.testsuites && json.testsuites.length && json.testsuites.length === 0){
console.log('No test suites found, skipping Mochawesome file creation.');
return undefined;
}
else if(!json || !json.testsuites || !json.testsuites.length){
throw `\nCould not find valid <testsuites> or <testsuite> root element in converted ${options.testFile}`;
Expand All @@ -109,6 +110,7 @@ function parseXml(options, xml){

if(!json.testsuites[0].testsuite || !json.testsuites[0].testsuite.length || json.testsuites[0].testsuite.length === 0){
console.log('No test suites found, skipping Mochawesome file creation.');
return undefined;
}

// sort test suites
Expand Down Expand Up @@ -342,6 +344,7 @@ async function convert(options, suitesRoot){
if(!suitesRoot){
suitesRoot = parseXml(options, fs.readFileSync(options.testFile, 'utf8'));
}
if(!suitesRoot) return;

let testSuites = suitesRoot.testsuite.filter((suite) => suite.tests !== '0');

Expand Down
2 changes: 1 addition & 1 deletion src/xunit-junit.xslt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

<xsl:template match="collection">
<xsl:if test="test">
<testsuite name="{replace(replace(@name, 'Test collection for ', ''),'TestSuite.','')}" tests="{@total}" time="{@time}" passed="{@passed}" failed="{@failed}" skipped="{@skipped}">
<testsuite name="{replace(replace(@name, 'Test collection for ', ''),'TestSuite.','')}" tests="{@total}" time="{@time}" passed="{@passed}" failures="{@failed}" skipped="{@skipped}">
<xsl:apply-templates select="test"/>
<xsl:apply-templates select="traits"/>
<xsl:apply-templates select="output"/>
Expand Down
5 changes: 4 additions & 1 deletion tests/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ describe("Config tests", () => {
expect(result.html).toBe(false);
expect(result.htmlReportFilename).toBe('mochawesome.html');
expect(result.saveIntermediateFiles).toBe(false);
expect(result.transformJunit).toBe(false);
});

test('return correct values from assigned options', () => {
Expand All @@ -70,7 +71,8 @@ describe("Config tests", () => {
junitReportFilename: 'xunit-j.xml',
html: true,
htmlReportFilename: 'mochawesome1.html',
saveIntermediateFiles: true
saveIntermediateFiles: true,
transformJunit: true
}

let result = config.config(options)
Expand All @@ -86,6 +88,7 @@ describe("Config tests", () => {
expect(result.html).toBe(true);
expect(result.htmlReportFilename).toBe('mochawesome1.html');
expect(result.saveIntermediateFiles).toBe(true);
expect(result.transformJunit).toBe(true);

expect(fs.existsSync(result.reportDir)).toBe(true)

Expand Down
8 changes: 4 additions & 4 deletions tests/converter.junit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@ const common = require("./common");

describe("JUnit converter tests", () => {

test('convert junit-jenkins.xml', async() => {
test('junit-jenkins.xml', async() => {
let options = common.createOptions('junit-jenkins.xml', 'junit');

await converter(options);
common.compare(options);
});

test('convert junit-notestsuites.xml', async() => {
test('junit-notestsuites.xml', async() => {
let options = common.createOptions('junit-notestsuites.xml', 'junit');

await converter(options);
common.compare(options, 'junit-jenkins-mochawesome.json');
});

test('convert junit-testsuites-noattributes.xml', async() => {
test('junit-testsuites-noattributes.xml', async() => {
let options = common.createOptions('junit-testsuites-noattributes.xml', 'junit');

await converter(options);
common.compare(options, 'junit-jenkins-mochawesome.json');
});

test('convert junit-mocha-xunit.xml', async() => {
test('junit-mocha-xunit.xml', async() => {
let options = common.createOptions('junit-mocha-xunit.xml', 'junit')

await converter(options);
Expand Down
6 changes: 3 additions & 3 deletions tests/converter.nunit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ const common = require('./common');

describe("NUnit converter tests", () => {

test('convert nunit-sample.xml', async() => {
test('nunit-sample.xml', async() => {
let options = common.createOptions('nunit-sample.xml', 'nunit')
await converter(options);
common.compare(options, undefined, true);
});

test('convert nunit-short.xml', async() => {
test('nunit-short.xml', async() => {
let options = common.createOptions('nunit-short.xml', 'nunit')
await converter(options);
common.compare(options, undefined, true);
});

test('convert nunit-mudblazor.xml', async() => {
test('nunit-mudblazor.xml', async() => {
let options = common.createOptions('nunit-mudblazor.xml', 'nunit')
await converter(options);
common.compare(options, undefined, true);
Expand Down
20 changes: 10 additions & 10 deletions tests/converter.trx.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,61 +6,61 @@ const converter = require('../src/converter');

describe("TRX converter tests", () => {

test('convert trx-mstest-datadriven.trx', async() => {
test('trx-mstest-datadriven.trx', async() => {
let options = common.createOptions('trx-mstest-datadriven.trx', 'trx')
await converter(options);
common.compare(options, undefined, true);
});

test('convert trx-nunit-datadriven.trx', async() => {
test('trx-nunit-datadriven.trx', async() => {
let options = common.createOptions('trx-nunit-datadriven.trx', 'trx')
await converter(options);
common.compare(options, undefined, true);
});

test('convert trx-xunit-datadriven.trx', async() => {
test('trx-xunit-datadriven.trx', async() => {
let options = common.createOptions('trx-xunit-datadriven.trx', 'trx')
await converter(options);
common.compare(options, undefined, true);
});

test('convert trx-mstest-ignore.trx', async() => {
test('trx-mstest-ignore.trx', async() => {
let options = common.createOptions('trx-mstest-ignore.trx', 'trx')
await converter(options);
common.compare(options, undefined, true);
});

test('convert trx-nunit-ignore.trx', async() => {
test('trx-nunit-ignore.trx', async() => {
let options = common.createOptions('trx-nunit-ignore.trx', 'trx')
await converter(options);
common.compare(options, undefined, true);
});

test('convert trx-xunit-ignore.trx', async() => {
test('trx-xunit-ignore.trx', async() => {
let options = common.createOptions('trx-xunit-ignore.trx', 'trx')
await converter(options);
common.compare(options, undefined, true);
});

test('convert trx-mstest.trx', async() => {
test('trx-mstest.trx', async() => {
let options = common.createOptions('trx-mstest.trx', 'trx')
await converter(options);
common.compare(options, undefined, true);
});

test('convert trx-nunit.trx', async() => {
test('trx-nunit.trx', async() => {
let options = common.createOptions('trx-nunit.trx', 'trx')
await converter(options);
common.compare(options, undefined, true);
});

test('convert trx-xunit.trx', async() => {
test('trx-xunit.trx', async() => {
let options = common.createOptions('trx-xunit.trx', 'trx')
await converter(options);
common.compare(options, undefined, true);
});

test('convert trx-sample.trx', async() => {
test('trx-sample.trx', async() => {
let options = common.createOptions('trx-sample.trx', 'trx')
await converter(options);
common.compare(options, undefined, true);
Expand Down
4 changes: 2 additions & 2 deletions tests/converter.xunit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ const common = require('./common');

describe("xUnit.net converter tests", () => {

test('convert xunit-sample.xml', async() => {
test('xunit-sample.xml', async() => {
let options = common.createOptions('xunit-sample.xml', 'xunit')
await converter(options);
common.compare(options, undefined, true);
});

test('convert xunit-qlnet.xml', async() => {
test('xunit-qlnet.xml', async() => {
let options = common.createOptions('xunit-qlnet.xml', 'xunit')
await converter(options);
common.compare(options, undefined, true);
Expand Down

0 comments on commit be144ce

Please sign in to comment.