Skip to content

Commit b78be9b

Browse files
committed
adding JUnit report generation functionality
1 parent f424814 commit b78be9b

File tree

3 files changed

+80
-50
lines changed

3 files changed

+80
-50
lines changed

gulp/tasks/compare.js

Lines changed: 69 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,66 +4,86 @@ var paths = require('../util/paths');
44
var fs = require('fs');
55
var path = require('path');
66
var _ = require('underscore');
7-
7+
var junitWriter = new (require('junitwriter'))();
88

99
gulp.task('compare', function (done) {
10-
var compareConfig = JSON.parse(fs.readFileSync(paths.compareConfigFileName, 'utf8')).compareConfig;
11-
12-
function updateProgress() {
13-
var results = {};
14-
_.each(compareConfig.testPairs, function (pair) {
15-
if (!results[pair.testStatus]) {
16-
results[pair.testStatus] = 0;
17-
}
18-
!results[pair.testStatus]++;
19-
});
20-
if (!results.running) {
21-
console.log ('\nTest completed...');
22-
console.log ('\x1b[32m', (results.pass || 0) + ' Passed', '\x1b[0m');
23-
console.log ('\x1b[31m', (results.fail || 0) + ' Failed\n', '\x1b[0m');
10+
var compareConfig = JSON.parse(fs.readFileSync(paths.compareConfigFileName, 'utf8')).compareConfig,
11+
testSuite = paths.report && paths.report.indexOf( 'CI' ) > -1 && paths.ciReport.format === 'junit' && junitWriter.addTestsuite(paths.ci.testSuiteName),
12+
testPairsLength;
2413

25-
if (results.fail) {
26-
console.log ('\x1b[31m', '*** Mismatch errors found ***', '\x1b[0m');
27-
console.log ("For a detailed report run `npm run openReport`\n");
28-
done(new Error('Mismatch errors found.'));
29-
} else {
30-
done();
31-
}
14+
function updateProgress() {
15+
var results = {};
16+
_.each(compareConfig.testPairs, function (pair) {
17+
if (!results[pair.testStatus]) {
18+
results[pair.testStatus] = 0;
19+
}
20+
!results[pair.testStatus]++;
21+
});
22+
if (!results.running) {
23+
console.log ('\nTest completed...');
24+
console.log ('\x1b[32m', (results.pass || 0) + ' Passed', '\x1b[0m');
25+
console.log ('\x1b[31m', (results.fail || 0) + ' Failed\n', '\x1b[0m');
3226

27+
if (results.fail) {
28+
console.log ('\x1b[31m', '*** Mismatch errors found ***', '\x1b[0m');
29+
console.log ("For a detailed report run `npm run openReport`\n");
30+
if (paths.cliExitOnFail) {
31+
done(new Error('Mismatch errors found.'));
32+
}
33+
}
34+
}
3335
}
34-
}
3536

3637

37-
_.each(compareConfig.testPairs, function (pair) {
38-
pair.testStatus = "running";
38+
_.each(compareConfig.testPairs, function (pair, key) {
39+
pair.testStatus = "running";
40+
testPairsLength = !testPairsLength && compareConfig.testPairs.length;
41+
42+
43+
var referencePath = path.join(paths.backstop, pair.reference);
44+
var testPath = path.join(paths.backstop, pair.test);
3945

40-
var referencePath = path.join(paths.backstop, pair.reference);
41-
var testPath = path.join(paths.backstop, pair.test);
46+
resemble(referencePath).compareTo(testPath).onComplete(function (data) {
47+
var imageComparisonFailed = !data.isSameDimensions || data.misMatchPercentage > pair.misMatchThreshold,
48+
error,
49+
testCase;
4250

43-
resemble(referencePath).compareTo(testPath).onComplete(function (data) {
44-
var imageComparisonFailed = !data.isSameDimensions || data.misMatchPercentage > pair.misMatchThreshold;
51+
if (imageComparisonFailed) {
52+
pair.testStatus = "fail";
53+
console.log('\x1b[31m', 'ERROR:', pair.label, pair.fileName, '\x1b[0m');
54+
storeFailedDiffImage(testPath, data);
55+
} else {
56+
pair.testStatus = "pass";
57+
console.log('\x1b[32m', 'OK:', pair.label, pair.fileName, '\x1b[0m');
58+
}
4559

46-
if (imageComparisonFailed) {
47-
pair.testStatus = "fail";
48-
console.log('\x1b[31m', 'ERROR:', pair.label, pair.fileName, '\x1b[0m');
49-
storeFailedDiffImage(testPath, data);
50-
} else {
51-
pair.testStatus = "pass";
52-
console.log('\x1b[32m', 'OK:', pair.label, pair.fileName, '\x1b[0m');
53-
}
54-
updateProgress();
60+
// add testcase only when there is a design deviation
61+
if (testSuite && imageComparisonFailed) {
62+
testCase = testSuite.addTestcase(' ›› ' + pair.label, pair.selector);
63+
error = 'Design deviation ›› ' + pair.label + ' (' + pair.selector + ') component';
64+
testCase.addError(error, 'CSS component');
65+
testCase.addFailure(error, 'CSS component');
66+
}
67+
68+
if (testSuite && testPairsLength === key + 1) {
69+
junitWriter.save(path.join(paths.ci_report, 'xunit.xml'), function() {
70+
console.log('\x1b[32m', 'Regression test report file (xunit.xml) is successfully created.', '\x1b[0m');
71+
});
72+
}
73+
74+
updateProgress();
75+
});
5576
});
56-
});
5777

58-
function storeFailedDiffImage(testPath, data) {
59-
var failedDiffFilename = getFailedDiffFilename(testPath);
60-
console.log(' See:', failedDiffFilename);
61-
var failedDiffStream = fs.createWriteStream(failedDiffFilename);
62-
data.getDiffImage().pack().pipe(failedDiffStream)
63-
}
78+
function storeFailedDiffImage(testPath, data) {
79+
var failedDiffFilename = getFailedDiffFilename(testPath);
80+
console.log(' See:', failedDiffFilename);
81+
var failedDiffStream = fs.createWriteStream(failedDiffFilename);
82+
data.getDiffImage().pack().pipe(failedDiffStream)
83+
}
6484

65-
function getFailedDiffFilename(testPath) {
66-
var lastSlash = testPath.lastIndexOf(path.sep);
67-
return testPath.slice(0, lastSlash + 1) + 'failed_diff_' + testPath.slice(lastSlash + 1, testPath.length);
68-
}
85+
function getFailedDiffFilename(testPath) {
86+
var lastSlash = testPath.lastIndexOf(path.sep);
87+
return testPath.slice(0, lastSlash + 1) + 'failed_diff_' + testPath.slice(lastSlash + 1, testPath.length);
88+
}
6989
});

gulp/util/paths.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ var defaultConfigPath = '../../backstop.json';
77

88
var paths = {};
99
paths.portNumber = defaultPort;
10+
paths.ci = {
11+
format: 'junit',
12+
testSuiteName: 'BackstopJS'
13+
};
1014

1115
// BACKSTOP MODULE PATH
1216
paths.backstop = path.join(__dirname, '../..');
@@ -30,6 +34,9 @@ paths.backstopConfigFileName = getBackstopConfigFileName();
3034
paths.bitmaps_reference = paths.backstop + '/bitmaps_reference';
3135
paths.bitmaps_test = paths.backstop + '/bitmaps_test';
3236

37+
// Continuous Integration (CI) report
38+
paths.ci_report = paths.backstop + '/ci_report';
39+
3340
// COMPARE PATHS -- note: compareConfigFileName is overwritten if config files exist. see below.
3441
paths.comparePath = paths.backstop + '/compare';
3542
paths.compareConfigFileName = paths.comparePath+'/config.json';
@@ -66,12 +73,14 @@ if(fs.existsSync(paths.activeCaptureConfigPath)){
6673
paths.bitmaps_test = config.paths.bitmaps_test || paths.bitmaps_test;
6774
paths.compareConfigFileName = config.paths.compare_data || paths.compareConfigFileName;
6875
paths.casper_scripts = config.paths.casper_scripts || null;
76+
paths.ci_report = config.paths.ci_report || paths.ci_report;
6977
}
7078

7179
paths.portNumber = config.port || defaultPort;
7280
paths.casperFlags = config.casperFlags || null;
7381
paths.engine = config.engine || null;
7482
paths.report = config.report || null;
83+
paths.ciReport = config.ci || paths.ci;
7584
}
7685

7786
paths.compareReportURL = 'http://localhost:' + paths.portNumber + '/compare/';

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "backstopjs",
3-
"version": "1.2.1",
3+
"version": "1.3.1",
44
"description": "BackstopJS: Catch CSS curveballs.",
55
"scripts": {
66
"genConfig": "gulp genConfig",
@@ -37,6 +37,7 @@
3737
"gulp-json-editor": "^2.2.1",
3838
"gulp-open": "^0.3.0",
3939
"gulp-rename": "^1.2.0",
40+
"junitwriter" : "~0.3.1" ,
4041
"http-proxy": "~1.1.4",
4142
"is-running": "~1.0.5",
4243
"node-resemble-js": "0.0.4",

0 commit comments

Comments
 (0)