6

次のように、各 QUnit テストでクイックセパレーターをコンソールに記録したいと思います。

test( "hello test", function() {
    testTitle = XXX; // get "hello test" here
    console.log("========= " + testTitle + "==============");
    // my test follows here
});

テストのタイトル (「名前」とも呼ばれます) を取得するにはどうすればよいですか?

4

4 に答える 4

9

QUnitのコールバックを使用してそれを実現できます。これらは、テストの実行中にいくつかの異なるポイントで呼び出されます (たとえば、各テストの前、各モジュールの後など)。

これが私のテストスイートの例です:

QUnit.begin = function() {
    console.log('####');
};

QUnit.testStart = function(test) {
    var module = test.module ? test.module : '';
    console.log('#' + module + " " + test.name + ": started.");
};

QUnit.testDone = function(test) {
    var module = test.module ? test.module : '';
    console.log('#' + module + " " + test.name + ": done.");
    console.log('####');
};

これを というファイルに入れhelper.js、テストの index.html ページに含めます。

次のような出力が生成されます。

####
#kort-Availability Includes: started.
#kort-Availability Includes: done.
#### 
#kort-UrlLib Constructor: started.
#kort-UrlLib Constructor: done.
#### 
#kort-UrlLib getCurrentUrl: started.
#kort-UrlLib getCurrentUrl: done. 
#### 
于 2013-02-11T14:05:45.433 に答える
2

このソリューションを使用するのは簡単です:

test( "hello test", function(assert) {
  testTitle = assert.test.testName; // get "hello test" here
  console.log("========= " + testTitle + "==============");
  // my test follows here
});

========= ハローテスト==============

于 2016-02-25T10:21:14.030 に答える
0

Javascriptargumentsオブジェクトを試してみてください(詳細はこちらをご覧ください)。

test( "hello test", function() {
    testTitle = arguments.callee.caller.arguments[0]; // get "hello test" here
    console.log("========= " + testTitle + "==============");
    // my test follows here
});

編集:私はそれがどのように機能するかについて
の小さな(そして文書化された)jsFiddleの例を作成しました。
私の答えは純粋なJavaScriptものであり、にだけ当てはまらないことに注意してくださいQUnit

于 2013-02-11T10:31:46.910 に答える
0

QUnit.config.current は、現在実行中のテストを含むオブジェクトです。したがって、console.log(QUnit.config.current) のように表示できます。このオブジェクトには多くのパラメーター (testName、started..) があり、それらを変更できます。

QUnit.test("some test", function() {
  console.log( QUnit.config.current.testName);
});
于 2016-03-02T12:18:43.707 に答える