gjstest - 单元测试框架


未知
跨平台
C/C++

软件简介

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