列出所有mocha测试而不执行它们

时间:2016-12-29 12:52:22

标签: javascript node.js testing mocha

mochajs中是否有一种方法可以列出测试运行器收集的所有测试而不执行它们?

E.g。如果有规格看起来像:

First
    should test something
Second
    should test something else

然后我想让控制台输出类似于测试报告者产生的输出,但没有执行实际测试,如下所示:

describe

UPD:

目前,我正在使用正则表达式提取所有it NSMutableString *stringToWrite = [[NSMutableString alloc] init]; [stringToWrite appendString:[NSString stringWithFormat:@"First Name /t Last Name /t Full Name /tPhone Number /tEmail /t Job/t organizationName /tNote\n\n"]]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ for(int i = 0 ;i<[Contact count];i++) { [stringToWrite appendString:[NSString stringWithFormat:@"%@/t",[[Contact objectAtIndex:i] valueForKey:@"firstName"] ]]; [stringToWrite appendString:[NSString stringWithFormat:@"%@/t",[[Contact objectAtIndex:i] valueForKey:@"lastName"] ]]; [stringToWrite appendString:[NSString stringWithFormat:@"%@/t",[[Contact valueForKey:@"userName"] objectAtIndex:i]]]; [stringToWrite appendString:[NSString stringWithFormat:@"%@/t",[[Contact objectAtIndex:i] valueForKey:@"phoneNumber"] ]]; [stringToWrite appendString:[NSString stringWithFormat:@"%@/t",[[Contact objectAtIndex:i] valueForKey:@"emailAddress"] ]]; [stringToWrite appendString:[NSString stringWithFormat:@"%@/t",[[Contact objectAtIndex:i] valueForKey:@"jobTitle"] ]]; [stringToWrite appendString:[NSString stringWithFormat:@"%@/t",[[Contact objectAtIndex:i] valueForKey:@"organizationName"] ]]; [stringToWrite appendString:[NSString stringWithFormat:@"%@\n",[[Contact objectAtIndex:i] valueForKey:@"note"] ]]; } dispatch_async(dispatch_get_main_queue(), ^(void) { NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString *documentDirectory=[paths objectAtIndex:0]; NSString *strBackupFileLocation = [NSString stringWithFormat:@"%@/%@", documentDirectory,@"ContactList.xls"]; [stringToWrite writeToFile:strBackupFileLocation atomically:YES encoding:NSUTF8StringEncoding error:nil]; }); }); ,但正在寻找更清晰的解决方案。

3 个答案:

答案 0 :(得分:1)

将所有描述块包裹在描述块中,然后跳过

describe.skip('Outline', function() {
    describe('First', function() {
        it('should test something', function() {
            ...
        })
    });

    describe('Second', function() {
        it('should test something else', function() {
            ...
        })
    });
});

答案 1 :(得分:0)

mocha-list-tests软件包

我不骗你,有一个单独的包装:https://www.npmjs.com/package/mocha-list-tests

npm install mocha mocha-list-tests
node ./node_modules/mocha-list-tests/mocha-list-tests.js main.js

示例输出:

{
  "suites": [
    "a1",
    "a2"
  ],
  "tests": [
    "a1.a11",
    "a1.a12",
    "a2.a21",
    "a2.a22"
  ],
  "tree": {
    "a1": {
      "a11": true,
      "a12": true
    },
    "a2": {
      "a21": true,
      "a22": true
    }
  }
}

此测试文件:

main.js

#!/usr/bin/env node

const assert = require('assert');
const fs = require('fs');

describe('a1', function() {
  it('a11', function() {
    assert.equal(1, 1);
    fs.writeFileSync('abc', 'def', 'utf8');
  });
  it('a12', function() {
    assert.equal(1, 2);
  });
});
describe('a2', function() {
  it('a21', function() {
    assert.equal(1, 1);
  });
  it('a22', function() {
    assert.equal(1, 2);
  });
});

文件abc尚未创建,因此我知道测试未执行。

这很可能使用https://github.com/mochajs/mocha/wiki/Using-Mocha-programmatically

中提到的API Mocha

mocha@6.2.2中的Teste,mocha-list-tests @ 1.0.2,节点v10.15.1。

答案 2 :(得分:0)

mocha-list-tests软件包很有用,但仅适用于BDD样式describe()it(),如果您.skip()进行任何测试,它都会因为{{1} }。

如果您需要克服这些问题之一,或者在测试中获取其他信息,则自己执行此操作的一种方法是利用Mocha的根it()钩子。这将在Mocha加载所有文件之后但在执行任何测试之前执行,因此此时所需的所有信息都将存在。

通过这种方式,很容易在before()命令行选项中打补丁以切换测试运行的行为,而无需添加或更改任何其他内容。

关键在于--list-only钩中的this是Mocha的before(),而其中的Context是指钩子本身。因此.test是指根套件。从那里,您可以沿着this.test.parent数组的树,以及每个套件的.suites数组的树走。

已经收集了所有想要的内容,然后必须输出该内容并退出该过程以阻止Mocha继续运行。

纯文本示例

给出 root.js

.tests

test.js

#!/bin/env mocha

before(function() {
    if(process.argv.includes('--list-only')) {
        inspectSuite(this.test.parent, 0);
        process.exit(0);
    }
    // else let Mocha carry on as normal...
});

function inspectSuite(suite, depth) {
    console.log(indent(`Suite ${suite.title || '(root)'}`, depth));

    suite.suites.forEach(suite => inspectSuite(suite, depth +1));
    suite.tests.forEach(test => inspectTest(test, depth +1));
}

function inspectTest(test, depth) {
    console.log(indent(`Test ${test.title}`, depth));
}

function indent(text, by) {
    return '    '.repeat(by) + text;
}

然后正常运行#!/bin/env mocha describe('foo', function() { describe('bar', function() { it('should do something', function() { // ... }); }); describe('baz', function() { it.skip('should do something else', function() { // ... }); it('should do another thing', function() { // ... }); }); }); 会给您期望的测试结果:

mocha

但是运行 foo bar ✓ should do something baz - should do something else ✓ should do another thing 2 passing (8ms) 1 pending 会给你(不运行任何测试):

mocha --list-only

JSON示例

root.js

Suite (root)
    Suite foo
        Suite bar
            Test should do something
        Suite baz
            Test should do something else
            Test should do another thing

使用与以前相同的测试脚本将给您:

#!/bin/env mocha

before(function() {
    let suites = 0;
    let tests = 0;
    let pending = 0;

    let root = mapSuite(this.test.parent);

    process.stdout.write(JSON.stringify({suites, tests, pending, root}, null, '    '));
    process.exit(0);

    function mapSuite(suite) {
        suites += +!suite.root;
        return {
            title: suite.root ? '(root)' : suite.title,
            suites: suite.suites.map(mapSuite),
            tests: suite.tests.map(mapTest)
        };
    }

    function mapTest(test) {
        ++tests;
        pending += +test.pending;
        return {
            title: test.title,
            pending: test.pending
        };
    }
});