nrw*_*nrw 10 unit-testing couchdb couchapp kanso
我写了很多django应用程序,并习惯于扩展unittest.TestCase
和运行python manage.py test app_name
.是否有类似的简单方法来测试Kanso应用程序?请提供一个最小的例子.
谢谢.
Jas*_*ith 10
Kanso应用程序是CouchDB应用程序.然而,最好的选择是暂时忽略CouchDB.重要的是:Kanso应用程序是Node.js应用程序.以与测试Node.js应用程序相同的方式测试它们.测试它们是否符合文档化的CouchDB API,你会没事的.
理想情况下,我们可能希望在CouchDB中实际运行测试.JavaScript引擎是不同的(V8与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"
}
Run Code Online (Sandbox Code Playgroud)
下一次运行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
}
}
}
Run Code Online (Sandbox Code Playgroud)
我跑了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" }
}
Run Code Online (Sandbox Code Playgroud)
运行npm install
以获取node-tap包.(我总是有./node_modules/.bin
我的$PATH
,当我在Node.js的工作,而不是全球的安装,我想有我需要的一切正确的,在该项目.
接下来,也许是一个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()
})
Run Code Online (Sandbox Code Playgroud)
通过运行测试tap test
:
$ tap test
ok test/show_function.js ................................ 5/5
total ................................................... 5/5
ok
Run Code Online (Sandbox Code Playgroud)
我将更改代码以返回"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
Run Code Online (Sandbox Code Playgroud)
我有一些项目可能有助于展示测试kanso应用程序:
https://github.com/ryanramage/dashboard-core
特征:
https://github.com/kanso/node-couchapp
和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
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,我已将'tests'目录包含为模块路径.放入该目录的任何模块将用作nodeunit测试套件并由nodeunit-testrunner UI显示.
重写
您需要手动将nodeunit-testrunner软件包的重写添加到您的应用程序,在我的示例中,这意味着将lib/app.js编辑为如下所示:
exports.rewrites = [
require('nodeunit-testrunner/rewrites')
];
Run Code Online (Sandbox Code Playgroud)
添加一些测试
假设我们有一个模块lib/foo.js,如下所示:
exports.hello = function (name) {
return 'hello ' + name;
};
Run Code Online (Sandbox Code Playgroud)
我们可以通过在tests/test-foo.js中添加一个模块来添加一个测试(只要它在tests目录中就可以命名).
var foo = require('lib/foo');
exports['test for foo.hello'] = function (test) {
test.equal(foo.hello('bar'), 'hello bar');
test.done();
};
Run Code Online (Sandbox Code Playgroud)
如果您随后推送您的应用程序并在浏览器中访问http:// localhost:5984/example/_design/example/_rewrite/test,您将看到一个基本界面,用于在tests目录中单独或全部运行测试套件他们一个接一个.
希望有所帮助.
归档时间: |
|
查看次数: |
1178 次 |
最近记录: |