Creating test groups with grunt-mocha-test - node.js

I'm using grunt-mocha-test to run node server side tests.
My Gruntfile.js looks like this
module.exports = function(grunt) {
// Add the grunt-mocha-test tasks.
grunt.loadNpmTasks('grunt-mocha-test');
grunt.initConfig({
// Configure a mochaTest task
mochaTest: {
test: {
options: {
reporter: 'dot'
},
src: [ 'test/setup.js', 'test/*.test.js' ]
}
},
});
grunt.registerTask('default', 'mochaTest');
};
What I want to do is this, I want to be able to run different test groups with different commands as follows
grunt will run all tests
grunt test1 runs a subset of tests in the test folder, and
grunt test2 runs another subset of tests
I don't know if this is possible, and I haven't been able to find anything about this in the documentation for grunt-mocha-test
I could sure use some help here.
Thanks

At the moment you have one group called test. Just add another object inside mochaTest and call grunt mochaTest:test2.

Related

How to parameterise specs in conf.js of Protractor Framework?

I am new to protractor framework,working on parameterising specs in conf.js...seeked help,googled but didn't get a solution...if any one is aware kindly help me...
If you want to run multiple files in protractor better use the naming conventions.
For example if there are two files :
todo-spec.js
log.js
If both files are in D:/Folder
Better name it in following way:
todo-spec.test.js
log.test.js
And for the spec file use the following way:
exports.config ={ specs: ['D:/Folder/*test.js']};
These will make sure that all your files containing test.js as a part of file name will run.
So keep a habit of writing test after every file.
I hope you are clear now. :-)
You can use multicapabilities , if you can:
exports.config = {
specs: [
// keep this blank
],
multiCapabilities: [{
'browserName': 'chrome',
'specs': ['todo-spec.js']
}, {
'browserName': 'chrome',
'specs': ['log.js']
}, {
'browserName': 'chrome',
'specs': ['test.js']
}],
};
I'm not sure if you are familiar with tools like Grunt and Gulp but it sounds to me like you will probably need one of them to do this. If I had the same requirements as you I would setup a Gulp task that opened your excel/csv file and put the file names of the tests you want to run into a list. Then you can use that same gulp task to kick off your protractor tests by using a package like gulp-protractor to run the tests. You will need to work out the logic to get your list of files on your own but here is an example of how to use a gulp task to do this:
gulp.task("e2e", function() {
//logic to get list of spec files here
gulp.src([<array/list of spec files>])
.pipe(protractor({
configFile: "test/protractor.config.js"
}))
.on('error', function(e) { throw e })
})

grunt-cucumber not running step_definitions

I'm trying to create a grunt task to run cucumber.js tests. The tests are organized in feature "areas" within my project eg:
project_root
--test
--spec-e2e
--home_Page
--features
--step_definitions
From my project's node_modules dir I can run cucumber.js manually and all is well:
$ node cucumber.js ../../../test/spec-e2e/home_Page/features/
Output:
1 scenario (1 passed)
3 steps (3 passed)
I cannot seem to get the grunt-cucumber task configured properly to recreate the same result.
In my Gruntfile.js I have the following configuration:
// Cucumber test runner
cucumberjs: {
src: 'test/spec-e2e/home_Page/features',
options: {
steps: 'test/spec-e2e/home_Page/features/step_definitions',
format: 'pretty'
}
}
...
//Register task
grunt.registerTask('cucumber', ['cucumberjs']);
Running $ grunt cucumber allows just outputs:
$ Running "cucumberjs:src" (cucumberjs) task
$ Done, without errors.
So I'm not receiving any errors or cucumber summary output. If I purposely edit one of my step_definitions to fail the result is always the same. Can someone tell me how to configure this correctly?
Thanks!
Try this one:
Please go through the below doc :
Grunt cucumber js docs
This code works for me :
grunt.initConfig({
cucumberjs: {
all: {
src: 'features',
options: {
backtrace: true,
useShortStackTraces: false,
format: "json:<path where want to write json report>",
steps: 'features',
tags: grunt.option('feature')
}
}
});
grunt.registerTask('default', ['cucumberjs:all']);

How can I run one particular CucumberJS feature using GruntJS?

I'm using CucumberJS to run tests on my NodeJS web app.
At the moment, I can run all of my grunt tasks by executing grunt, or only the CucumberJS tasks, using grunt cucumberjs.
But now I want to only execute particular features.
For example, say I have the following feature files:
Signin.feature
Favourite.feature
I want to only run the Favourite feature tests, using a command such as:
grunt cucumberjs Favourite
Is this possible?
BTW, here's my gruntfile.js:
'use strict';
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
...
cucumberjs: {
src: 'features',
options: {
steps: 'features/step_definitions',
format: 'pretty'
}
}
});
...
grunt.loadNpmTasks('grunt-cucumber');
grunt.registerTask('default', [... 'cucumberjs']);
};
I finally figured out a solution that seems to work well enough, based on tags.
So for each of my feature files, I've added a tag.
For example, for Favourite.feature:
#favourite
Feature: Favourite
As a user of the system
I would like to favourite items
Then I've used a GruntJS option to specify the tags I want to run via a command-line argument.
I do this through a grunt.option() call in my gruntfile:
cucumberjs: {
src: 'features',
options: {
steps: 'features/step_definitions',
format: 'pretty',
tags: grunt.option('cucumbertags')
}
}
So now I can run GruntJS from the command-line like this:
grunt cucumberjs --cucumbertags=#favourite
And it will only run the feature with the #favourite tag. Yay!
here is my task that allows you to filter by feature.name:
https://github.com/webforge-labs/cuked-zombie/blob/master/tasks/cucumber.js
(download it into a "tasks" folder and you can load it with: grunt.loadTasks('tasks'))
configure it like this (Gruntfile.js):
grunt.loadNpmTasks('grunt-cucumber');
grunt.initConfig({
cucumberjs: {
// config for all features when called with: `grunt cucumber`
all: {
src: 'features',
options: {
steps: "tests/js/cucumber/bootstrap.js",
format: "pretty"
}
},
// config for single features when called with `grunt --filter some-feature`
features: {
src: 'features',
options: {
steps: "tests/js/cucumber/bootstrap.js",
format: "pretty"
}
}
}
});
use it with:
grunt cucumber --filter post
which will run the post.feature (somewhere in your feature directory)
With the new cucumber-js Version it is possible to run it by feature-line:
https://github.com/cucumber/cucumber-js/pull/168
I haven't tested it with grunt but cucumber has an option for executing a scenario without modifying the gherkin file. Just call cucumber-js --name "Scenario Name", therefore I assume that sending the same argument to grant it will do its job.

How to test nodejs backend code with Karma (testacular)

How do I setup Karma to run my backend unit tests (written with Mocha)? If I add my backend test script to the files = [], it fails stating that require is undefined.
You don't. Karma is only for testing browser-based code. If you have a project with mocha tests on the backend and karma/mocha on the front end, try editing your package.json under scripts to set test to: mocha -R spec && karma run karma.con
Then, if npm test returns true, you'll know it's safe to commit or deploy.
It seems like it cannot be done (thanks #dankohn). Here is my solution using Grunt:
Karma: update your karma.conf.js file
set autoWatch = false;
set singleRun = true;
set browsers = ['PhantomJS']; (to have inline results)
Grunt:
npm install grunt-contrib-watch grunt-simple-mocha grunt-karma
configure the two grunt tasks (see grunt file below)
Gruntfile.js:
module.exports = function (grunt) {
grunt.loadNpmTasks('grunt-simple-mocha');
grunt.loadNpmTasks('grunt-karma');
grunt.initConfig({
simplemocha: {
backend: {
src: 'test/server-tests.js'
}
},
karma: {
unit: {
configFile: 'karma.conf.js'
}
}
});
// Default task.
grunt.registerTask('default', ['simplemocha', 'karma']);
};
Grunt (optional): configure grunt-watch to run after changing spec files or files to be tested.
run all using grunt command.

Running grunt task with api, without command line

I want to create and run grunt task in node.js code for test use.
var foo = function() {
var grunt = require("grunt");
var options = {"blahblah": null} // ...creating dynamic grunt options, such as concat and jshint
grunt.initConfig(options);
grunt.registerTask('default', [/*grunt subtasks*/]);
}
But this doesn't work. Grunt doesn't seem to run any task. I'm almost sure that there is some API to run grunt task externally without command line, but don't know how to do it.
Is there any way to do it?
You can. I don't know why anyone would need to do this as currently Grunt is a command line tool. WARNING: I don't recommend running Grunt in this way. But here it is:
var grunt = require('grunt');
// hack to avoid loading a Gruntfile
// You can skip this and just use a Gruntfile instead
grunt.task.init = function() {};
// Init config
grunt.initConfig({
jshint: {
all: ['index.js']
}
});
// Register your own tasks
grunt.registerTask('mytask', function() {
grunt.log.write('Ran my task.');
});
// Load tasks from npm
grunt.loadNpmTasks('grunt-contrib-jshint');
// Finally run the tasks, with options and a callback when we're done
grunt.tasks(['mytask', 'jshint'], {}, function() {
grunt.log.ok('Done running tasks.');
});
You can get inspiration on how to run grunt from code by looking at grunt-cli which does this and which is a project maintained by the grunt folks.
Grunt is launched from code in grunt-cli/bin/grunt and you can read more about the options in grunt/lib/grunt/cli.js.
I use it in a private project like this:
var grunt = require("grunt");
grunt.cli({
gruntfile: __dirname + "/path/to/someGruntfile.js",
extra: {key: "value"}
});
The key "extra" will be available from inside the gruntfile as grunt.option("extra")
Here is a bloggpost that describes an alternative way to run a grunt task: http://andrewduthie.com/2014/01/14/running-grunt-tasks-without-grunt-cli/

Resources