Skip to content

Commit

Permalink
Adding tests for JSON and HTML reporting
Browse files Browse the repository at this point in the history
Fix the JSON parser issue by reading file from .json
Adding assertions for JSON output for the test
grunt test
  • Loading branch information
Kushang Gajjar committed Jun 10, 2016
1 parent 3fe136a commit 20afb9e
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 86 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ node_modules
npm-debug.log
tmp
.DS_Store
.idea/
.idea/
test/report/
test/report/screenshot/
31 changes: 17 additions & 14 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,20 @@
*/

'use strict';
var report = require('./test/assert/report');

module.exports = function(grunt) {
var options = {
formats: ['html', 'pretty'],
templateDir: 'templates/bootstrap',
output: 'test/report/features_report.html',
saveJson: true,
theme: 'bootstrap'
};

function assertReport() {
report.assert(options.output);
}

// Project configuration.
grunt.initConfig({
Expand All @@ -26,22 +38,13 @@ module.exports = function(grunt) {

// Before generating any new files, remove any previously-created files.
clean: {
tests: ['tmp']
tests: ['test/report/*.json', 'test/report/*.html', 'test/report/screenshot/*.png']
},

// Configuration to be run (and then tested).
cucumberjs: {

options: {
steps: '',
tags: '',
templateDir: 'templates/simple',
output: 'tmp/features_report.html',
format: 'html',
cucumber: '',
failFast:false
},
features: []
options: options,
src: ['test/features']
},
jsbeautifier: {
src: ['<%= jshint.all %>']
Expand All @@ -55,8 +58,8 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-jsbeautifier');

grunt.registerTask('assertReport', assertReport);
// By default, lint and run all tests.
grunt.registerTask('default', ['jshint', 'jsbeautifier', 'cucumberjs']);
grunt.registerTask('test', ['jshint', 'jsbeautifier', 'clean', 'cucumberjs', 'assertReport']);

};
36 changes: 0 additions & 36 deletions features/step_definitions/test.js

This file was deleted.

13 changes: 0 additions & 13 deletions features/test.feature

This file was deleted.

25 changes: 8 additions & 17 deletions lib/processHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ var commondir = require('commondir');
module.exports = function HandleUsingProcess(grunt, options, commands, callback) {
var version = grunt.file.readJSON('./package.json').version;
var projectPkg = grunt.file.readJSON('package.json');

try {
var cucumberVersion = require('../../cucumber/package.json').version;
}catch(e) {
Expand Down Expand Up @@ -80,37 +79,31 @@ module.exports = function HandleUsingProcess(grunt, options, commands, callback)
cucumber.on('close', function(code) {
if (options.isHtml) {

var featureJsonOutput,
featureOutput;
var featureJsonOutput;
var jsonOutput;

if (options.executeParallel) {
featureOutput = JSON.stringify(jsonFile.readFileSync(options.output + '.json'));
jsonOutput = JSON.stringify(jsonFile.readFileSync(options.output + '.json'));
} else {
var jsonOutputFilePath = options.output + '.json';
var jsonOutput;


if (fs.existsSync(options.output + '.json')) {
jsonOutput= JSON.stringify(jsonFile.readFileSync(jsonOutputFilePath));
} else {
jsonOutput = Buffer.concat(buffer).toString();
}

var featureStartIndex = jsonOutput.search(/\[\s*{\s*\"\[\"/g);

var logOutput = jsonOutput.substring(0, featureStartIndex - 1);

featureOutput = jsonOutput.substring(featureStartIndex);
}

try {
featureJsonOutput = JSON.parse(featureOutput);
featureJsonOutput = JSON.parse(jsonOutput);
} catch (e) {
grunt.log.error('Unable to parse cucumberjs output into json.');

return callback(false);
}

generateReport(featureJsonOutput, logOutput);
generateReport(featureJsonOutput);
}

return callback(code);
Expand Down Expand Up @@ -246,7 +239,7 @@ module.exports = function HandleUsingProcess(grunt, options, commands, callback)
* @param {object} featureOutput Features result object
* @param {string} logOutput Contains any console statements captured during the test run
*/
var generateReport = function(featureOutput, logOutput) {
var generateReport = function(featureOutput) {
var suite = {
name: projectPkg.name,
features: featureOutput,
Expand All @@ -257,12 +250,10 @@ module.exports = function HandleUsingProcess(grunt, options, commands, callback)
failed: 0,
skipped: 0,
notdefined: 0
},
logOutput: logOutput
}
};

suite = setStats(suite);

if (options.saveJson) {
grunt.file.write(options.output + '.json', JSON.stringify(featureOutput, null, '\t'));
}
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
"underscore": "^1.8.3"
},
"devDependencies": {
"cucumber": "^0.4.8",
"chai": "^1.10.0",
"chai-fs": "^0.1.0",
"cucumber": "^1.0.0",
"grunt": "~0.4.5",
"grunt-cli": "~0.1.13",
"grunt-contrib-clean": "~0.5.0",
Expand Down
8 changes: 4 additions & 4 deletions tasks/cucumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ module.exports = function(grunt) {

if (options.tags) {
if (options.tags instanceof Array) {
options.tags.forEach(function(element, index, array) {
options.tags.forEach(function(element) {
commands.push('-t', element);
});
} else {
Expand All @@ -80,7 +80,7 @@ module.exports = function(grunt) {
} else {
commands.push('-f', format);
}
})
});
} else if (options.format) {
applyLegacyFormatters();
}
Expand All @@ -95,7 +95,7 @@ module.exports = function(grunt) {

if (grunt.cli.options['parallel']) {
commands.push('--parallel', grunt.cli.options['parallel']);
} else if (options.parallel) {
} else if (options.parallel) {
commands.push('--parallel', options.parallel);
}

Expand All @@ -107,7 +107,7 @@ module.exports = function(grunt) {

if (options.require) {
if (options.require instanceof Array) {
options.require.forEach(function(element, index, array) {
options.require.forEach(function(element) {
commands.push('--require', element);
});
} else {
Expand Down
24 changes: 24 additions & 0 deletions test/assert/report.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';
var fs = require('fs');
var chai = require('chai');
chai.use(require('chai-fs'));
var expect = chai.expect;

module.exports = {

assert: function(htmlFile) {
var jsonFile = htmlFile + '.json';

function assertJsonContents() {
var jsonOutput = require('../../' + jsonFile);
var jsonOutputStringify = JSON.stringify(jsonOutput);
expect(jsonOutputStringify).to.contain('mime_type":"image/png"', 'screenshot was not attached to report');
expect(jsonOutputStringify).to.contain('mime_type":"text/plain"', 'test data was not attached to report');
}

expect(jsonFile).to.be.a.file('expected a file to be Json').with.json;
expect(htmlFile).to.be.a.path('HTML report was not created');

return assertJsonContents();
}
};
48 changes: 48 additions & 0 deletions test/features/step_definitions/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

var steps = function() {
this.Before(function(scenario, callback) {
this.scenario = scenario;
callback();
});

this.Then(/^this feature runs with background$/, function(callback) {
callback();
});

this.Then(/^Fred runs a passing cucumber scenario$/, function(callback) {
callback();
});

this.Then(/^he choose "([^"]*)" output as one of the formatter$/, function(arg1, callback) {
callback();
});

this.Then(/^the output should contain test results in HTML format$/, function(callback) {
callback();
});

this.Then(/^Fred runs a failing cucumber scenario$/, function(callback) {
callback();
});

this.Then(/^a failing scenario captures a screenshot$/, function(callback) {
this.scenario.attach(new Buffer('').toString('base64'), 'image/png');
callback();
});

this.Then(/^the output should contain test results with screenshot in HTML format$/, function(callback) {
callback();
});

this.Then(/^Fred attaches the "([^"]*)" to the Given step of passing cucumber scenario$/, function(testData, callback) {
this.scenario.attach(testData);
callback();
});

this.Then(/^the output should contain test data attached to the Given step in HTML format$/, function(callback) {
callback();
});
};


module.exports = steps;
25 changes: 25 additions & 0 deletions test/features/test.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Feature: Grunt Cucumberjs Feature

In order to review cucumber reports
Fred, a cucumber user
Wants to have cucumber reports in HTML

Background:
When this feature runs with background

Scenario: Fred wants to have passing scenarios in the HTML reports
Given Fred runs a passing cucumber scenario
When he choose "html" output as one of the formatter
Then the output should contain test results in HTML format

Scenario: Fred wants to have failing scenarios in the HTML reports
Given Fred runs a failing cucumber scenario
When he choose "html" output as one of the formatter
And a failing scenario captures a screenshot
Then the output should contain test results with screenshot in HTML format

Scenario: Fred wants to print test data in the HTML reports for debugging purpose
Given Fred attaches the "test data to be printed" to the Given step of passing cucumber scenario
When he choose "html" output as one of the formatter
Then the output should contain test data attached to the Given step in HTML format

0 comments on commit 20afb9e

Please sign in to comment.