我已经编写了很多django应用程序,并且已经习惯于扩展unittest.TestCase和运行python manage.py test app_name。有没有类似的简单方法来对Kanso应用程序进行单元测试?请提供一个最小的示例。
谢谢。
发布于 2012-05-04 09:53:24
Kanso应用程序是CouchDB应用程序。然而,最好的办法是暂时忽略CouchDB。重要的是:Kanso应用程序是Node.js应用程序。测试它们的方式与测试Node.js应用程序的方式相同。测试它们是否符合文档化的CouchDB应用程序接口,您就不会有问题。
理想情况下,我们可能希望在CouchDB中实际运行测试。JavaScript引擎是不同的(V8与SpiderMonkey);环境也不同。然而,在实践中,测试Node.js代码要容易得多。(而且,在这两个平台上都没有一整类JavaScript错误:设置全局变量的第三方代码,更改内置类型,更改原型-这些都是浏览器问题。Node.js和CouchDB都是原始的和可预测的。)
示例
让我们创建一个简单的沙发应用程序,在_show function中输出"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命令类似。)
让我们用./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,可以在这里看到我的应用程序:
添加测试
我喜欢node-tap,所以让我们使用它。但重点是,这只是一些Node.js代码。使用你喜欢的任何方法来测试它。
首先,一个快速的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上工作时,我的$PATH中总是有./node_modules/.bin。而不是全局安装,我喜欢在项目中拥有我需要的所有东西。
接下来,可能是一个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我将更改代码以返回硬编码的"Hello,world“(即忽略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发布于 2012-05-04 22:39:52
我有一些项目可以帮助展示测试kanso应用程序:
仪表板核心项目
https://github.com/ryanramage/dashboard-core
功能:
使用NodeUnit
Node-Couchapp项目
https://github.com/kanso/node-couchapp
中的符号链接技巧
发布于 2012-05-04 22:48:25
与JasonSmith一样,我也建议您尽可能使用Node.js进行测试。但是,由于CouchApps的性质,您最终通常必须编写单元测试才能在浏览器中运行,这要么是因为它们与您不想模拟的浏览器API交互,要么是因为您需要测试它在多种浏览器中的工作。
在进行基于浏览器的单元测试时,我使用了几个Kanso包,它们是我拼凑在一起的,用来自动显示用于运行nodeunit测试套件的界面。目前它有点粗糙,但已经完成了工作。
kanso.json
将nodeunit和nodeunit-testrunner包添加到您的kanso.json文件中,然后运行kanso install从存储库中获取它们。
{
"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
}
}请注意,我已经包含了‘test’目录作为模块路径。放入该目录的任何模块都将用作nodeunit测试套件,并由nodeunit-testrunner UI显示。
重写
您需要手动将nodeunit-testrunner包的重写添加到您的应用程序中,在我的示例中,这意味着编辑lib/app.js如下所示:
exports.rewrites = [
require('nodeunit-testrunner/rewrites')
];添加一些测试
假设我们有一个如下所示的模块lib/foo.js:
exports.hello = function (name) {
return 'hello ' + name;
};我们可以通过在tests/ test -foo.js中添加一个模块来添加一个测试(这个模块可以被命名为任何名称,只要它在test目录中)。
var foo = require('lib/foo');
exports['test for foo.hello'] = function (test) {
test.equal(foo.hello('bar'), 'hello bar');
test.done();
};如果您随后推送您的应用程序并在浏览器中访问http://localhost:5984/example/_design/example/_rewrite/test,您将看到一个基本界面,用于运行测试目录中的测试套件,可以单独运行,也可以依次运行所有测试套件。
希望这能有所帮助。
https://stackoverflow.com/questions/8267119
复制相似问题