[英]How to get Mocha test name in the “before” hook?
我試圖before
鈎子中獲取當前的describe
名稱,如下所示:
describe('increasing 3 times', function() {
before(function() {
console.log('test name');
});
...
});
我基本上想要檢索前鈎子中'增加3倍'的字符串。
如何實現這一目標?
謝謝!
這是代碼,說明了如何做到這一點:
describe("top", function () {
before(function () {
console.log("full title:", this.test.fullTitle());
console.log("parent title:", this.test.parent.title);
});
it("test 1", function () {});
});
使用spec
報告器運行,這將輸出:
full title: top "before all" hook
parent title: top
✓ test 1
1 passing (4ms)
當摩卡打電話給你傳遞的功能,它的各種功能( describe
, before
, it
等)的值, this
是一個Context
對象。 該對象的一個字段名為test
。 這有點用詞不當,因為它可以指向別的東西,而不是實際的測試。 在類似於before
的鈎子的情況下,它指向為before
調用創建的當前Hook
對象。 在此對象上調用fullTitle()
將獲得對象的層次結構名稱:對象自己的名稱前面是包含它的測試套件( describe
)的名稱。 Hook
對象還有一個parent
字段,指向包含鈎子的套件。 套件有一個title
字段,這是第一個被傳遞來describe
參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.