从grunt

时间:2016-03-16 04:27:31

标签: json gruntjs jasmine

我正在使用Jasmine来执行Grunt任务中的测试。我想在JSON中输出测试用例的结果。以下是茉莉花的配置。此配置在junit文件夹下生成XML文件。

var conf = {

        src: [
           // '../../../Test.UnitTest.JS/UnitTests/' + name + '/**/*.js'
        ],
        options: {
            template: require('grunt-template-jasmine-requirejs'),
            templateOptions: {
                requireConfig: {
                    baseUrl: 'SinglePageApplications/' + name,
                    //waitSeconds: 30,
                    paths: mixIn({
                        'knockout-editables': '../../Assets/scripts/vendor/ko.editables-0.9.0',
                        'knockout-validation': '../../Assets/scripts/vendor/knockout.validation-1.0.2',
                        'bignumber': '../../Assets/scripts/vendor/bignumber-1.4.1',
                        'testutils': '../../../Test.UnitTest.JS/Utils',
                        'shared': '../../Legacy/Shared',
                        'testdata': '../../../Test.UnitTest.JS/UnitTests/' + name + '/testdata' 
                    }, addConfigurationPaths(app))

                }
            },
            helpers: [
                'Assets/scripts/ato/helperscript.js'
            ],
            specs: [
                       '../Test.UnitTest.JS/UnitTests/' + name + '/**/*.js'
            ],
            junit: {
                path: 'build/junit/' + name + '/'
            },
            timeout: 100000,
            vendor: arr
        }
            ,
        //specs : 'src/test/js/unit-headless.html',

        phantomjs: {
            'ignore-ssl-errors': true
        }
 }

我希望结果以JSON格式显示,所以我使用npm安装了jasmine-json-test-reporter,并尝试在我当前的配置中实现它,如下所示:

 var conf = {

        src: [
           // '../../../Test.UnitTest.JS/UnitTests/' + name + '/**/*.js'
        ],
        options: {
            template: require('grunt-template-jasmine-requirejs'),
            templateOptions: {
                requireConfig: {
                    baseUrl: 'SinglePageApplications/' + name,
                    //waitSeconds: 30,
                    paths: mixIn({
                        'knockout-editables': '../../Assets/scripts/vendor/ko.editables-0.9.0',
                        'knockout-validation': '../../Assets/scripts/vendor/knockout.validation-1.0.2',
                        'bignumber': '../../Assets/scripts/vendor/bignumber-1.4.1',
                        'testutils': '../../../Test.UnitTest.JS/Utils',
                        'shared': '../../Legacy/Shared',
                        'testdata': '../../../Test.UnitTest.JS/UnitTests/' + name + '/testdata' 
                    }, addConfigurationPaths(app))

                }
            },
            helpers: [
                'Assets/scripts/ato/helperscript.js'

                //'Legacy/Shared/common/constants.js'
            ],
            specs: [
                //'../Test.UnitTest.JS/UnitTests/' + name + '/common/*.js',
                //'../Test.UnitTest.JS/UnitTests/' + name + '/testdata',
                '../Test.UnitTest.JS/UnitTests/' + name + '/**/*.js'
            ],
            junit: {
                path: 'build/junit/' + name + '/'
            },
            timeout: 100000,
            vendor: arr
        }
            ,
        //specs : 'src/test/js/unit-headless.html',

        phantomjs: {
            'ignore-ssl-errors': true
        },
        onPrepare: function () {
            var JSONReporter = require('jasmine-json-test-reporter');
            var jasmine = require('jasmine');
            return browser.getProcessedConfig().then(function (config) {
                browser.params['browsername'] = config.capabilities.browserName.toUpperCase();
                browser.params['version'] = config.capabilities.version || 'latest stable';
                browser.params['platform'] = config.capabilities.os + '-' + config.capabilities.os_version;
                jasmine.getEnv().addReporter(new JSONReporter({
                    file: 'results/' + browser.params.platform + '-' + browser.params.version + '.' + browser.params.browsername + '.json',
                    beautify: true,
                    indentationLevel: 2 // used if beautify === true 
                }));
            });
        }

    }

此代码未生成任何json文件。我不太确定使用jasmine-json-test-reporter和当前的jasmine配置。

1 个答案:

答案 0 :(得分:0)

我设法让它运转起来。源文件未指向正确的位置。更改以下行修复了它:

src: [
                'SinglePageApplications/' + name + '/common/**/*.js',
                'SinglePageApplications/' + name + '/viewmodels/**/*.js',
                'SinglePageApplications/' + name + '/views/**/*.js',
                'SinglePageApplications/' + name + '/main.js'
            ],