我正在使用 grunt-mocha-test 来运行我们的 mocha 测试。我希望能够运行测试并生成 xunit 报告并获得覆盖率(使用毯子.js)。我的 gruntfile 中有以下部分:

mochaTest: {
        'unit-jenkins': {
            options: {
                reporter: 'XUnit',
                require: paths.test + '/blanket',
                captureFile: paths.tmp + '/xunit.xml'
            },
            src: [paths.test + '/unit/**/*.js'],
        },
        'integration-jenkins': {
            options: {
                reporter: 'XUnit',
                require: paths.test + '/blanket',
                captureFile: paths.tmp + '/xunit.xml'
            },
            src: [paths.test + '/integration/**/*.js']
        },
        coverage: {
            options: {
                reporter: 'html-cov',
                quiet: true,
                captureFile: paths.tmp + '/coverage.html'
            },
            src: [paths.test +  '/**/*.js']
        }
    },


    grunt.registerTask('test-jenkins', [
    'mochaTest:unit-jenkins',       // run unit tests
    'mochaTest:integration-jenkins',    // run unit tests
]);

当我运行 grunt test-jenkins 时,我可以在控制台上看到测试输出和 xunit 输出。此外,创建了 xunit 文件,但是,它包含测试输出和 xunit 输出,例如:
[14:30:17.164Z] TRACE App: HTTP Response /versions
HTTP/1.1 200 OK
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 46
ETag: "-1409762768"
Date: Mon, 17 Feb 2014 14:30:17 GMT
Connection: close
<testsuite name="Mocha Tests" tests="1" failures="0" errors="0" skipped="0" timestamp="Mon, 17 Feb 2014 14:30:17 GMT" time="0.029">
<testcase classname="Application" name="should contain description of API versions" time="0.028"/>
</testsuite>

应该如何配置 grunt-mocha-test 以便 xunit 输出文件仅包含 xunit 输出?

最佳答案

我在 Selenium 上遇到了同样的问题。我通过以下任务解决了它:

    var outputFile = process.env.MOCHA_OUTPUT_FILE || 'xunit_results.xml';
    grunt.registerTask('cleanXunitFile', 'Remove Selenium/WebDriver output from xunit file', function() {
        if (grunt.file.exists('./' + outputFile)) {
            var file = grunt.file.read('./' + outputFile);
            if (file.indexOf("<testsuite")) {
                grunt.file.write('./' + outputFile, file.substring(file.indexOf("<testsuite")));
            }
        }
        else {
            grunt.log.error("'cleanXunitFile' task was specified but file " + outputFile + " does not exist.");
        }
    });

关于node.js - Grunt-mocha-test Xunit 报告器将整个控制台输出写入 xunit 文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21831900/

10-16 19:47