iT邦幫忙

DAY 17
8

node.js伺服器實戰系列 第 17

node.js伺服器實戰(17) - 整合測試

只要有適合的方法可以使用,其實還是可以用nodeunit來驅動整合測試。
node.js的http.ClientRequest模組

node.js除了提供伺服器端的http/https功能,同時也提供了客戶端的功能,利用這個功能,就可以簡單地做整合測試。

不過因為我的需求,只是要確認伺服器的功能是否符合我的預期,所以只需要簡單的測試用例。實際使用到evolve來開發比較複雜的程式及介面,用這個工具就太花時間。這時候,還是建議使用Selenium比較好,尤其是Selenium IDE可以透過Firefox錄製操作的動作,這樣做整合測試會比較簡單。

撰寫測試

不多說廢話,就先來寫幾個整合測試吧。

不過在這之前,因為伺服器很久沒更新...先在tools加上一個cookieParser,然後順便寫一個單元測試及幾個整合測試。

首先寫測試整個evolve server功能的整合測試(testServer.js):

(太長了,請上github)
https://github.com/fillano/evolve/blob/750cf3fdb7c3eda25cf77a6a8433e435ae62fa8f/tests/testServer.js

看起來有點長,不過過程也很簡單:

  1. 在setUp裡面初始化一個evolve伺服器,設定好檔案系統對應及router
  2. 在tearDown裡面關閉伺服器,並且釋放資源
  3. 在個別測試裡面,透過http.ClientRequest去存取不同的URL,然後檢查回傳的http.ClientResponse是否符合預期

cookieParser的功能很簡單,http.ServerRequest裡面會有一個headers物件,裡面可能會有cookie屬性,是從Client(通常是瀏覽器)傳來的cookie字串,如果有這個header,就使用cookieParser來把它剖析成物件。然後用簡單的cookieHandler函數,把這個物件加到http.ServerRequest中。

單元測試的時候,只需要確定傳進來的各種cookie字串是否都能剖析成功(testToolsCookieParser.js):

(還是太長,請上github)
https://github.com/fillano/evolve/blob/750cf3fdb7c3eda25cf77a6a8433e435ae62fa8f/tests/testToolsCookieParser.js

因為還沒實做setCookie,所以要做整合測試的時候,只好...把收到的cookie用一個router直接顯示在頁面上,然後用http.ClientRequest來檢查是否符合預期(testCookie.js):

 var Evolve = require('../lib/evolve');
 var testCase = require('nodeunit').testCase;
 
 module.exports = testCase({
     "setUp": function(cb) {
         this.http = require('http');
         this.evolve = new Evolve({dirindex: ['index.html', 'index.htm', 'default.htm']});
         this.evolve
         .host('localhost:8443')
         .get('/testcookie', function(request, response) {
             if(request.cookie) {
 				var str = ''+request.headers.cookie;
                 response.writeHead('200', {"Content-Type":"text/plain","Content-Length":str.length});
                 response.end(str);
             }else{
             	var str = "no cookie";
                 response.writeHead('200', {"Content-Type":"text/plain","Content-Length":str.length});
                 response.end(str);
             }
         });
         this.evolve.listen(8443, 'localhost');
         cb();
     },
     "tearDown": function(cb) {
         this.evolve.close();
         cb();
     },
     "test if cookie received and appended to request": function(test) {
         test.expect(2);
         var req = this.http.request({
             "host": "localhost",
             "port": 8443,
             "path": "/testcookie",
             "method": "GET",
             "headers": {
                 "Cookie": "SID=1234567890abcd"
             }
         }, function(response) {
             var result = [];
             response.on('data', function(data) {
                 result.push(data);
             });
             response.on('end', function() {
                 var total = 0;
                 var entity = '';
                 for(var i=0; i<result.length; i++) {
                    total += result[i].length;
                    entity += result[i].toString('ascii');
                 }
                 test.ok(entity.indexOf('SID')>-1);
                 test.ok(entity.indexOf('1234567890abcd')>-1);
                 test.done();
             });
         });
         req.end();
     }
 });

接下來就要跑一跑測試。

透過ant驅動

當然還是要透過ant來跑,不過目前執行檔目錄寫死在build.xml中,在不同機器上測試會有一些麻煩,所以把這些先移到build.properties中,然後再用property tag把它引入。(為了維護方便,另外做了一個build.properties.sample來透過git維護,build.properties可能會不時變動,就把它排除了)接下來就各自設定好單元測試與整合測是的檔案,然後用ant來執行。

單元測試的結果:

整合測試的結果:

看起來還算ok,明天再來看一下測試覆蓋率。

今天的範例檔(今天的範例檔(fillano-evolve-v0.0.11-0-g750cf3f.zip),目前版本是v0.0.11。

相關文章


上一篇
node.js伺服器實戰(16) - 可測試性
下一篇
Node.js伺服器實戰(18) - 覆蓋率測試
系列文
node.js伺服器實戰33
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0
fillano
iT邦超人 1 級 ‧ 2011-10-27 22:15:37

怎麼每次一貼字數都到一萬三,這裡字數計算是正確的嗎...XD,記得以前寫一篇一萬字報告都很辛苦,怎麼現在每天都超過啦...

就是91 iT邦研究生 4 級 ‧ 2011-10-27 23:18:39 檢舉

我都是把所有超連結跟圖片網址,用goo.gl縮過,程式碼就沒法子了,咖圖鐵定被縮得太小看不清楚,只能貼Git了...

圖檔和超連結縮過之前,真的少了很多,費大可以試試看。

小朱 iT邦新手 4 級 ‧ 2011-10-28 08:26:56 檢舉

我的文有好幾篇都 >15000.... 目前最長的有 20000 左右吧,但問題是人家算的是 "字元"...

fillano iT邦超人 1 級 ‧ 2011-10-28 17:43:19 檢舉

嗯嗯,不過我長的是程式碼,所以大概還是得請讀者上githubXD

0
zanhsieh
iT邦新手 4 級 ‧ 2012-02-15 06:00:55

請教若要驗證 router.js route 過去的方法正確性要如何測試?您目前在 testCase 中 setUp 定義 get 路徑對應的方法,像這樣:

&lt;pre class="c" name="code">
...
"setUp": function(cb) {
        this.http = require('http');
        this.evolve = new Evolve({dirindex: ['index.html', 'index.htm', 'default.htm']});
        this.evolve.handle('pre', tools.cookieHandler);
        this.evolve.host('localhost:8443')
        .map('/', path.join(__dirname, '../www'))
        .get('/hello', function (request, response, cb) {
            cb(false, '/hello', {
                type: 'text/html',
                data: 'hello'
            });
        })
},
...

假設我有很多的 .get,定義在其他的檔案 (類似您的simple.js),像這樣:

&lt;pre class="c" name="code">
server.get('/path1', function (req, res, cb) {...});
server.get('/path2', function (req, res, cb) {...});
...
server.get('/path99', function (req, res, cb) {...});
server.get('/path100', function (req, res, cb) {...});

總不成全部抄一遍到 setUp 裡面吧?

0
fillano
iT邦超人 1 級 ‧ 2012-02-16 15:41:57

是的。

不過我這裡做的是框架的測試,用這樣測試就可以驗證了。如果是產品,那建議用其他的軟體來做,例如Selenium。

zanhsieh iT邦新手 4 級 ‧ 2012-02-24 06:57:52 檢舉

能否看看這題給點建議嗎http://ithelp.ithome.com.tw/question/10088161?感謝

我要留言

立即登入留言