單元測試框架:gjstest

b77m 9年前發布 | 11K 次閱讀 gjstest 單元測試

gjstest(Google JS Test)是在 V8 引擎上快速運行 javascript 單元測試框架的工具,且不需要用戶啟動完整的瀏覽器。當然,假如你在 V8 上面測試的時候沒有瀏覽器也沒有 DOM 的話,你可以使用本工具來完成測試。

特征如下:

  • 能夠極快的啟動測試和執行測試,且不需要運行瀏覽器

  • 輸出可讀的測試結果,測試失敗也可讀、明了

  • 一個基于瀏覽器的測試運行工具能夠隨 JS 的變化而變化

  • 風格和語義都類似 C++風格

  • 內置模擬框架,所以只需要極少的代碼即可完成測試

代碼示例:

function UserInfoTest() {
  // Each test function gets its own instance of UserInfoTest, so tests can
  // use instance variables to store state that doesn't affect other tests.
  // There's no need to write a tearDown method, unless you modify global
  // state.
  //
  // Create an instance of the class under test here, giving it a mock
  // function that we also keep a reference to below.
  this.getInfoFromDb_ = createMockFunction();
  this.userInfo_ = new UserInfo(this.getInfoFromDb_);
}
registerTestSuite(UserInfoTest);

addTest(UserInfoTest, function formatsUSPhoneNumber() {
  // Expect a call to the database function with the argument 0xdeadbeef. When
  // the call is received, return the supplied string.
  expectCall(this.getInfoFromDb_)(0xdeadbeef)
    .willOnce(returnWith('phone_number: "650 253 0000"'));

  // Make sure that our class returns correctly formatted output.
  expectEq('(650) 253-0000', this.userInfo_.getPhoneForId(0xdeadbeef));
});

addTest(UserInfoTest, function returnsLastNameFirst() {
  expectCall(this.getInfoFromDb_)(0xdeadbeef)
    .willOnce(returnWith('given_name: "John" family_name: "Doe"'));

  // Make sure that our class puts the last name first.
  expectEq('Doe, John', this.userInfo_.getNameForId(0xdeadbeef));
});

項目主頁:http://www.baiduhome.net/lib/view/home/1429710351791

 本文由用戶 b77m 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!