我已经编写了很多Django应用程序,并习惯于扩展 unittest.TestCase
并运行 python manage.py test app_name
。是否有一种同样简单的方法来对Kanso应用程序进行单元测试?请提供一个最小示例。
谢谢。
我已经编写了很多Django应用程序,并习惯于扩展 unittest.TestCase
并运行 python manage.py test app_name
。是否有一种同样简单的方法来对Kanso应用程序进行单元测试?请提供一个最小示例。
谢谢。
Kanso应用是CouchDB应用程序。但是为了现在而言,最划算的方法是忽略CouchDB。重要的是:Kanso应用是Node.js应用程序。像测试Node.js应用程序一样测试它们。测试它们是否遵守记录在案的CouchDB API,你就会没问题。
理想情况下,我们可能希望在CouchDB中实际运行测试。JavaScript引擎不同(V8 vs. SpiderMonkey);环境也不同。然而,在实践中,测试Node.js代码会更容易。 (此外,两个平台上都没有一整类JavaScript错误:第三方代码设置全局变量,更改内置类型,更改原型 - 这些都是浏览器问题。 Node.js和CouchDB都是纯净且可预测的。)
让我们创建一个简单的Couch应用程序,在_show函数中输出“Hello world”。
kanso.json
文件:
{ "name" : "hello_world"
, "version": "0.1.0"
, "description": "A simple hello-world Couch app"
, "dependencies": { "node-couchapp": "~0.8.3" }
, "app": "app"
}
接下来运行kanso install
命令,它将引入"node-couchapp"依赖项。(注意使用kanso
命令类似于使用npm
命令。)
让我们创建一个非常简单的Couch应用,在./app.js
中:
// A Couch app that just says hello in a _show function.
module.exports = {
'shows': {
'hello': function(doc, req) {
var who = req.query.who || "world"
return "Hello, " + who
}
}
}
kanso push http://example.iriscouch.com/so_hello
,现在我可以在这里看到我的应用程序:
package.json
文件:{ "name" : "hello_world"
, "description": "A simple hello-world Couch app"
, "version": "0.1.0"
, "private": true
, "devDependencies": { "tap": "~0.2.3" }
}
npm install
以获取node-tap包。(当我在Node.js上工作时,我总是将./node_modules/.bin
添加到我的$PATH
中。与全局安装不同,我喜欢在项目中拥有我所需要的一切。)test/show_function.js
文件:var tap = require('tap')
tap.test('The Couch app loads', function(t) {
t.doesNotThrow(load_app, 'No problem loading the app.js file')
t.end()
function load_app() {
var app = require('../app')
}
})
tap.test('The show function', function(t) {
var app = require('../app')
, hello = app.shows.hello
t.type(hello, 'function', 'Show function "hello" in the couch app')
var doc = {}
, null_req = {'query':{}}
, john_req = {'query':{'who':'John Doe'}}
t.equal(hello(doc, null_req), 'Hello, world', '"Hello world" by default')
t.equal(hello(doc, john_req), 'Hello, John Doe', 'Supports ?who query string')
t.end()
})
通过运行tap test
来测试它:
$ tap test
ok test/show_function.js ................................ 5/5
total ................................................... 5/5
ok
req.query.who
参数)。请注意测试失败:$ tap test
not ok test/show_function.js ............................ 4/5
Command: "node" "show_function.js"
ok 1 No problem loading the app.js file
ok 2 Show function "hello" in the couch app
ok 3 "Hello world" by default
not ok 4 Supports ?who query string
---
file: /private/tmp/j/test/show_function.js
line: 23
column: 5
stack:
- getCaller (/private/tmp/j/node_modules/tap/lib/tap-assert.js:403:17)
- assert (/private/tmp/j/node_modules/tap/lib/tap-assert.js:19:16)
- Function.equal (/private/tmp/j/node_modules/tap/lib/tap-assert.js:160:10)
- Test._testAssert [as equal] (/private/tmp/j/node_modules/tap/lib/tap-test.js:86:16)
- Test.<anonymous> (/private/tmp/j/test/show_function.js:23:5)
- Test.<anonymous> (native)
- Test.<anonymous> (events.js:88:20)
- Test.emit (/private/tmp/j/node_modules/tap/lib/tap-test.js:103:8)
- GlobalHarness.<anonymous> (/private/tmp/j/node_modules/tap/lib/tap-harness.js:86:13)
- Array.0 (native)
found: Hello, world
wanted: Hello, John Doe
diff: |
FOUND: Hello, world
WANTED: Hello, John Doe
^ (at position = 7)
...
ok 5 test/show_function.js
1..5
# tests 5
# pass 4
# fail 1
total ................................................... 4/5
not ok
我有一些项目可以帮助展示测试 kanso 应用程序:
https://github.com/ryanramage/dashboard-core
特性:
https://github.com/kanso/node-couchapp
{
"name": "example",
"version": "0.0.1",
"description": "example app with unit tests",
"modules": ["lib", "tests"],
"load": "lib/app",
"dependencies": {
"modules": null,
"properties": null,
"nodeunit": null,
"nodeunit-testrunner": null
}
}
exports.rewrites = [
require('nodeunit-testrunner/rewrites')
];
添加一些测试
假设我们有一个模块 lib/foo.js,内容如下:
exports.hello = function (name) {
return 'hello ' + name;
};
var foo = require('lib/foo');
exports['test for foo.hello'] = function (test) {
test.equal(foo.hello('bar'), 'hello bar');
test.done();
};