Skip to content

Commit

Permalink
improving XSLT files, adding JUnit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
agracio committed Nov 18, 2024
1 parent c1d5304 commit 3120ebf
Show file tree
Hide file tree
Showing 40 changed files with 4,649 additions and 1,043 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 19 additions & 9 deletions src/nunit-junit.xslt
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,31 @@
</xsl:template>

<xsl:template match="test-case">
<testcase name="{@name}" classname="{@fullname}" time="{@duration}" status="{replace(@result,'Ignored','Skipped')}">
<testcase name="{@name}" classname="{@fullname}" time="{@duration}" status="{replace(replace(replace(@result,'Inconclusive','Failed'),'Error','Failed'),'Ignored','Skipped')}">
<xsl:if test="@result = 'Skipped' or @result = 'Ignored'">
<xsl:choose>
<xsl:when test="./reason/message">
<skipped message="{./reason/message}"/>
</xsl:when>
<xsl:otherwise>
<skipped/>
</xsl:otherwise>
</xsl:choose>
<skipped message="{./reason/message}"/>
</xsl:if>
<xsl:if test="@result = 'Failed'">
<failure message="{./failure/message}">
<xsl:value-of select="./failure/stack-trace"/>
</failure>
<xsl:if test="output/text()">
<system-out>
<xsl:value-of select="output/text()"/>
</system-out>
</xsl:if>
</xsl:if>
<xsl:if test="@result='Inconclusive' or @result='Error'">
<xsl:element name="error">
<xsl:choose>
<xsl:when test="reason and reason/message/text()">
<xsl:attribute name="message" select="reason/message" />
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="message">Inconclusive test</xsl:attribute>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:if>
<xsl:apply-templates select="properties"/>
<xsl:apply-templates select="output"/>
Expand Down
15 changes: 10 additions & 5 deletions src/trx-junit.xslt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
xmlns:vs="http://microsoft.com/schemas/VisualStudio/TeamTest/2010" >
<xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:variable name="numberOfTests" select="count(//vs:UnitTestResult/@testId)"/>
<xsl:variable name="numberOfFailures" select="count(//vs:UnitTestResult/@outcome[.='Failed'])" />
<xsl:variable name="numberOfErrors" select="count(//vs:UnitTestResult[not(@outcome)])" />
<xsl:variable name="numberSkipped" select="count(//vs:UnitTestResult/@outcome[.!='Passed' and .!='Failed'])" />
<xsl:variable name="numberOfTests" select="//vs:TestRun/ResultSummary/Counters/@total"/>
<xsl:variable name="numberOfFailures" select="//vs:TestRun/ResultSummary/Counters/@failed" />
<xsl:variable name="numberOfErrors" select="//vs:TestRun/ResultSummary/Counters/@error + //vs:TestRun/ResultSummary/Counters/@timeout" />
<xsl:variable name="numberSkipped" select="//vs:TestRun/ResultSummary/Counters/@inconclusive + //vs:TestRun/ResultSummary/Counters/@total - //vs:TestRun/ResultSummary/Counters/@executed" />
<xsl:variable name="duration" select="xs:dateTime(//vs:Times/@finish) - xs:dateTime(//vs:Times/@start)" />
<xsl:variable name="totalDuration" select="hours-from-duration($duration)*3600 + minutes-from-duration($duration)*60 + seconds-from-duration($duration)" />
<testsuites
Expand Down Expand Up @@ -80,7 +80,7 @@
<testcase
classname="{$className}"
name="{$name}"
status="{replace(replace($outcome,'Error','Failed'),'NotExecuted','Skipped')}"
status="{replace(replace(replace($outcome,'Error','Failed'),'NotExecuted','Skipped'), 'Inconclusive', 'Failed')}"
time="{$testDuration}"
>
<xsl:if test="contains($outcome, 'Failed')">
Expand All @@ -93,6 +93,11 @@
<xsl:value-of select="$stacktrace" />
</error>
</xsl:if>
<xsl:if test="contains($outcome, 'Inconclusive')">
<error message="{$message}">
<xsl:value-of select="$stacktrace" />
</error>
</xsl:if>
<xsl:if test="contains($outcome, 'NotExecuted')">
<xsl:if test="$message != ''">
<skipped message="{$message}"/>
Expand Down
23 changes: 16 additions & 7 deletions src/xunit-junit.xslt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:csl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>

<!-- <xsl:template match="/assemblies">-->
Expand All @@ -9,16 +10,24 @@
<!-- <xsl:variable name="failed" select="sum(assembly/@failed)"/>-->
<!-- <xsl:variable name="skipped" select="sum(assembly/@skipped)"/>-->
<!-- <testsuites tests="{$tests}" time="{$time}" errors="{$errors}" failures="{$failed}" skipped="{$skipped}">-->
<!-- <xsl:apply-templates/>-->
<!-- <xsl:for-each select="assembly">-->
<!-- <xsl:sort select="name"/>-->
<!-- <xsl:variable name="assemblyFileName">-->
<!-- <xsl:call-template name="substring-after-last">-->
<!-- <xsl:with-param name="string" select="@name" />-->
<!-- <xsl:with-param name="delimiter" select="'\'" />-->
<!-- </xsl:call-template>-->
<!-- </xsl:variable>-->
<!-- <xsl:apply-templates select="collection"/>-->
<!-- </xsl:for-each>-->
<!-- </testsuites>-->
<!-- </xsl:template>-->

<!-- <xsl:template match="assembly">-->
<!-- <xsl:apply-templates/>-->
<!-- </xsl:template>-->

<xsl:template match="assemblies/assembly">
<testsuites name="{@name}" classname="{@fullname}" tests="{@total}" time="{@time}" errors="{@errors}" failures="{@failed}" skipped="{@skipped}" timestamp="{@start-time}">
<xsl:variable name="timeStamp">
<xsl:value-of select="concat(@run-date, 'T', translate(@run-time, ',', '.'))" />
</xsl:variable>
<testsuites name="{@name}" classname="{@fullname}" tests="{@total}" time="{@time}" errors="{@errors}" failures="{@failed}" skipped="{@skipped}" timestamp="{$timeStamp}">
<xsl:apply-templates/>
</testsuites>
</xsl:template>
Expand Down
14 changes: 12 additions & 2 deletions tests/setup.js → tests/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,29 @@ function createOptions(file, type){
testType: type,
reportDir: outDir,
reportFilename: getFilename(file),
junit: true
junit: true,
junitReportFilename: `${path.parse(file).name}-junit.xml`,
}
}

/**
* @param {TestReportConverterOptions} options
* @param {string?} reportFilename
* @param {Boolean?} compareJunit
*/
function compare(options, reportFilename){
function compare(options, reportFilename, compareJunit){

let createdReport = fs.readFileSync(path.join(outDir, options.reportFilename), 'utf8');
let report = fs.readFileSync(path.join(reportDir, reportFilename ?? options.reportFilename), 'utf8');

expect(createdReport.replaceAll(/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/g,'')).toBe(report.replaceAll(/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/g,''));

if(compareJunit){
let junitCreatedReport = fs.readFileSync(path.join(outDir, options.junitReportFilename), 'utf8');
let junitReport = fs.readFileSync(path.join(reportDir, options.junitReportFilename), 'utf8');

expect(junitCreatedReport).toBe(junitReport);
}
}

exports.outDir = outDir;
Expand Down
33 changes: 9 additions & 24 deletions tests/converter.junit.test.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,37 @@
const fs = require("fs");

const test = require('@jest/globals').test;
const beforeAll = require('@jest/globals').beforeAll;
const afterAll = require('@jest/globals').afterAll;
const describe = require('@jest/globals').describe;

const converter = require('../src/converter');
const setup = require("./setup");
const common = require("./common");

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

const outDir= './tests/data/tmp';
const reportDir= './tests/data/result';

beforeAll(() => {
setup.removeTempDir();
});

// afterAll(() => {
// setup.removeTempDir();
// });

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

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

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

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

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

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

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

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

});
26 changes: 7 additions & 19 deletions tests/converter.nunit.test.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,27 @@
const fs = require("fs");

const test = require('@jest/globals').test;
const beforeAll = require('@jest/globals').beforeAll;
const afterAll = require('@jest/globals').afterAll;
const describe = require('@jest/globals').describe;

const converter = require('../src/converter');
const setup = require('./setup');
const common = require('./common');

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

beforeAll(() => {
setup.removeTempDir();
});

// afterAll(() => {
// setup.removeTempDir();
// });

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

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

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

});
50 changes: 19 additions & 31 deletions tests/converter.trx.test.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,63 @@
const fs = require("fs");

const test = require('@jest/globals').test;
const beforeAll = require('@jest/globals').beforeAll;
const afterAll = require('@jest/globals').afterAll;
const describe = require('@jest/globals').describe;

const setup = require('./setup');
const common = require('./common');
const converter = require('../src/converter');

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

beforeAll(() => {
setup.removeTempDir();
});

// afterAll(() => {
// setup.removeTempDir();
// });

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

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

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

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

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

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

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

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

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

});
Loading

0 comments on commit 3120ebf

Please sign in to comment.