模块 test

符合 CommonJS Unit 单元测试规范的测试运行器。 它管理单元测试的执行并处理测试结果。 跑步者将失败的总数报告为退出状态码。

跑步者将模块视为测试用例。 测试用例定义了灯具运行多个测试。 测试用例可以提供可选的setUp() 和 tearDown() 函数来初始化和销毁灯具。 测试运行人员将在每次测试之前/之后运行这些方法。 测试函数必须以名称中的测试前缀开头,否则跑步者会跳过它们。

下面的示例测试用例 testDatabase.js 在用 ringo testDatabase.js 执行时启动一个新的测试运行器

Example

// testDatabase.js
exports.setUp = function() { ... open db connection ... }
exports.tearDown = function() { ... close db connection ... }

// Test functions start with the prefix 'test'
exports.testCreateTable = function() { ... }
exports.testInsertData = function() { ... }
exports.testTransactions = function() { ... }
exports.testDeleteTable = function() { ... }

if (require.main === module) {
  // Get a runner and run on the current module
  require("test").run(exports);
}

See

assert 模块是一个用于编写单元测试的断言库。

Functions


getStackTrace (trace)

创建一个堆栈跟踪并解析它以便显示。

Parameters

java.lang.StackTraceElement trace

The trace to parse. If not given a stacktrace will be generated

Returns

String

The parsed stack trace


getType (obj)

返回作为参数传递的对象的类型。

Parameters

Object obj

Returns

String

The type of the object passed as argument


jsDump (value, lvl)

将作为参数传递的值转换为格式良好并带有缩进的字符串。

Parameters

Object value

The value to convert into a string

Number lvl

Optional indentation level (defaults to zero)

Returns

String

The string representation of the object passed as argument


run (scope, name, writer)

主要的跑步者方法。可以用一个,两个或三个参数调用此方法:run(scope), run(scope, nameOfTest), run(scope, writer)run(scope, nameOfTest, writer)

Parameters

String|Object scope

Either the path to a module containing unit tests to execute, or an object containing the exported test methods or nested scopes.

String name

Optional name of a test method to execute

Object writer

Optional writer to use for displaying the test results. Defaults to TermWriter.