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

01function UserInfoTest() {
02  // Each test function gets its own instance of UserInfoTest, so tests can
03  // use instance variables to store state that doesn't affect other tests.
04  // There's no need to write a tearDown method, unless you modify global
05  // state.
06  //
07  // Create an instance of the class under test here, giving it a mock
08  // function that we also keep a reference to below.
09  this.getInfoFromDb_ = createMockFunction();
10  this.userInfo_ = new UserInfo(this.getInfoFromDb_);
11}
12registerTestSuite(UserInfoTest);
13 
14UserInfoTest.prototype.formatsUSPhoneNumber = function() {
15  // Expect a call to the database function with the argument 0xdeadbeef. When
16  // the call is received, return the supplied string.
17  expectCall(this.getInfoFromDb_)(0xdeadbeef)
18    .willOnce(returnWith('phone_number: "650 253 0000"'));
19 
20  // Make sure that our class returns correctly formatted output.
21  expectEq('(650) 253-0000',this.userInfo_.getPhoneForId(0xdeadbeef));
22};
23 
24UserInfoTest.prototype.returnsLastNameFirst = function() {
25  expectCall(this.getInfoFromDb_)(0xdeadbeef)
26    .willOnce(returnWith('given_name: "John" family_name: "Doe"'));
27 
28  // Make sure that our class puts the last name first.
29  expectEq('Doe, John'this.userInfo_.getNameForId(0xdeadbeef));
30};
項目地址: http://code.google.com/p/google-js-test/