STONEDSOUL

I wish I could give you meaningful additions

It’s easy to make things bigger, it’s hard to make things better.
Douglas Crockford

YUI Test を使った JavaScript のテスト

最近初めて YUI Test を使って JavaScript をテストした。また Unit テストというものも初めての経験なので 何か勘違いしてる部分もあるかもしれないが、一応メモしておく。

まず、URL エンコーディングや Unicode 文字列にエスケープする string converter を作った。 UTF-8 の URL エンコードや Unicode のエスケープは JavaScript で行うが、 その他の文字コードに対する URL エンコーディングは、サーバサイドのスクリプトで 行っている。

YUI Test はユーザのアクションをシミュレートできるので (ボタンのクリックなど)、 UI のテストケースも加えてみた。今回やったテストケースがすべてをカバーできてるとは 思わないが、自分が YUI Test の使い方を覚えるにはちょうど良かった。 また、これを使うとブラウザでのテストが少し楽になるんじゃないかと思う。

“run test suite” というリンクをクリックすると、テストの様子が見れる。 サーバサイドのスクリプトが処理する部分については、5 秒ほど待ってから テストしていることに注意。以下がソース。

Testing JavaScript with YUI Test Utility

It was the first time that I tested JavaScript with YUI Test. And this is also the first time that I had a unit test for my JavaScript. Though there may be wrong way in the description, anyway, I take notes for my case.

I created the string converter which converts text to URL encoding and Unicode escaping. For UTF-8 of URL encoding and unicode escaping, it converts with JavaScript. For other character encoding of URL encoding, it sends the input text to server side script and get the results asynchronously.

Since YUI Test unility can simulate user action (click a button, for example), I added UI test cases to the test code. I don’t think the test cases don’t fully cover all possible cases, but it’s good for me to understand how to use the test utility. And now I think it’s reduce cost of browter testing.

Click “run test suite” link to see how it works. Note that it waits 5 seconds for converting strings on sever side script. The followings are the source codes.

powered by Tumblr