Google JS Test是一個(gè)運(yùn)行于V8 JavaScript引擎下的Javascript單元測(cè)試框架,其在Google內(nèi)部負(fù)責(zé)對(duì)Chrome的快速JS執(zhí)行速度進(jìn)行測(cè)試,現(xiàn)在Google以開源工程開放大家使用。Google JS Test主要特性:
- 超快的啟動(dòng)速度和執(zhí)行時(shí)間,不需要在瀏覽器里運(yùn)行
- 清爽而具有可讀性的輸出內(nèi)容
- 也有一個(gè)可選的基于瀏覽器的測(cè)試器,可在JS修改的時(shí)候刷新
- 其樣式和語(yǔ)義跟Google Test for C++類似
- 內(nèi)置的Mocking框架只需要最簡(jiǎn)單的樣板代碼(比如no $tearDown or $verifyAll 請(qǐng)求),其樣式和語(yǔ)義基于Google C++ Mocking Framework
- 匹配系統(tǒng)允許表達(dá)式測(cè)試,并可直觀的閱讀輸出的錯(cuò)誤提示,內(nèi)置了很多匹配器,用戶也可自行添加

01 | function 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 |
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_); |
12 | registerTestSuite(UserInfoTest); |
14 | UserInfoTest.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"' )); |
20 | // Make sure that our class returns correctly formatted output. |
21 | expectEq( '(650) 253-0000' , this .userInfo_.getPhoneForId(0xdeadbeef)); |
24 | UserInfoTest.prototype.returnsLastNameFirst = function () { |
25 | expectCall( this .getInfoFromDb_)(0xdeadbeef) |
26 | .willOnce(returnWith( 'given_name: "John" family_name: "Doe"' )); |
28 | // Make sure that our class puts the last name first. |
29 | expectEq( 'Doe, John' , this .userInfo_.getNameForId(0xdeadbeef)); |
項(xiàng)目地址: http://code.google.com/p/google-js-test/