JS測試工具 Google JS Test

openkk 13年前發布 | 36K 次閱讀 JavaScript

Google JS Test是一個運行于V8 JavaScript引擎下的Javascript單元測試框架,其在Google內部負責對Chrome的快速JS執行速度進行測試,現在Google以開源工程開放大家使用。Google JS Test主要特性:

  • 超快的啟動速度和執行時間,不需要在瀏覽器里運行
  • 清爽而具有可讀性的輸出內容
  • 也有一個可選的基于瀏覽器的測試器,可在JS修改的時候刷新
  • 其樣式和語義跟Google Test for C++類似
  • 內置的Mocking框架只需要最簡單的樣板代碼(比如no $tearDown or $verifyAll 請求),其樣式和語義基于Google C++ Mocking Framework
  • 匹配系統允許表達式測試,并可直觀的閱讀輸出的錯誤提示,內置了很多匹配器,用戶也可自行添加

測試工具 Google JS Test

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);

UserInfoTest.prototype.formatsUSPhoneNumber = function() {
  // 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));
};

UserInfoTest.prototype.returnsLastNameFirst = function() {
  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://code.google.com/p/google-js-test/

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