2

特定のプロジェクトのノードユニットテストには、次のフォルダー構造があります。

/tests
/tests/basic-test.js
/tests/models/
/tests/models/model1-tests.js
/tests/models/model2-tests.js

私の質問は、ノードユニットに、テストフォルダ内のサブディレクトリを含むすべてのテストを自動的に実行させるにはどうすればよいですか?

nodeunitテストを実行すると、basic-test.jsのみが実行され、デフォルトでサブフォルダー内のすべてがスキップされます。

4

4 に答える 4

4

makeベースの魔法(またはシェルベースの魔法)を使用します。

test: 
    nodeunit $(shell find ./tests -name \*.js)

ここで、実行の結果を渡すと、すべてのjavascriptテストが再帰的に実行find ./tests -name \*.jsされます。nodeunit

于 2012-02-09T01:48:30.790 に答える
1

Nodeunitを使用すると、テストを実行するディレクトリのリストを渡すことができます。ファイルとディレクトリを同期的かつ再帰的にループするdiveSyncというパッケージを使用しました。すべてのディレクトリを配列に格納し、それをnodeunitに渡します。

var diveSync = require("diveSync"),
    fs = require("fs"),
    nodeUnit = require('nodeunit'),
    directoriesToTest = ['test'];

diveSync(directoriesToTest[0], {directories:true}, function(err, file) {
    if (fs.lstatSync(file).isDirectory()) {
        directoriesToTest.push(file);
    }
})

nodeUnit.reporters.default.run(directoriesToTest);
于 2014-03-30T19:23:55.230 に答える
0

これは上記のような自動ソリューションではありませんが、次のようなコレクターファイルを作成しました。

allTests.js:

exports.registryTests = require("./registryTests.js");
exports.message = require("./messageTests.js")

を実行するnodeunit allTests.jsと、すべてのテストが実行され、階層配置も示されます。

? registryTests - [Test 1]
? registryTests - [Test 2]
? messageTests - [Test 1]

等...

新しい単体テストファイルを作成するには、それをコレクターに含める必要がありますが、これは簡単な1回限りの作業であり、各ファイルを個別に実行できます。非常に大規模なプロジェクトの場合、これにより、すべてのテストではなく、複数のテストを実行するコレクターも許可されます。

于 2013-10-03T05:03:40.743 に答える
0

私は同じ質問の解決策を探していました。提示された回答はどれも、次のような私の状況に完全には適合しませんでした。

  • 追加の依存関係は必要ありませんでした。
  • すでにノードユニットをグローバルにインストールしました。
  • テストファイルを維持したくありませんでした。

したがって、私にとっての最終的な解決策は、Ianとmbmcavoyのアイデアを組み合わせることでした。

// nodeunit tests.js
const path = require('path');
const fs = require('fs');

// Add folders you don't want to process here.
const ignores = [path.basename(__filename), 'node_modules', '.git'];
const testPaths = [];

// Reads a dir, finding all the tests inside it.
const readDir = (path) => {
    fs.readdirSync(path).forEach((item) => {
        const thisPath = `${path}/${item}`;
        if (
            ignores.indexOf(item) === -1 &&
            fs.lstatSync(thisPath).isDirectory()
        ) {
            if (item === 'tests') {
                // Tests dir found.
                fs.readdirSync(thisPath).forEach((test) => {
                    testPaths.push(`${thisPath}/${test}`);
                });
            } else {
                // Sub dir found.
                readDir(thisPath);
            }
        }
    });
}

readDir('.', true);
// Feed the tests to nodeunit.
testPaths.forEach((path) => {
    exports[path] = require(path);
});

これで、コマンドを実行するだけで、新旧を問わず、すべてのテストを実行できますnodeunit tests.js

コードからわかるように、テストファイルはtestsフォルダー内にあり、フォルダーには他のファイルが含まれていてはなりません。

于 2017-06-17T16:11:35.067 に答える