JavaScript ã®ãã¹ãã£ã³ã°ãã¬ã¼ã ã¯ã¼ã¯ QUnit ãã TAP åºåããããã®ä»çµã¿ãä½æããããã« CommonJS ç°å¢ä¸ã§ãåãããã«ãã¦ã¿ã¾ããã®ã§ã github ã§å ¬éãã¾ããã©ã¤ã»ã³ã¹ã¯ QUnit ã«åãã㦠MIT 㨠GPLv2 ã®ãã¥ã¢ã«ã©ã¤ã»ã³ã¹ã§ãã
http://github.com/twada/qunit-tap
ããã¯ä½?
å¹³ããè¨ãã¨ã主ã«ç»é¢éä¾åã® JavaScript ã³ã¼ãããµã¼ããµã¤ãã§åãã JavaScript ã³ã¼ãã«å¯¾ãã¦ã³ãã³ãã©ã¤ã³ããã¦ããããã¹ããè¡ãããã®ä»çµã¿ã§ãã
js ã®ã¦ããããã¹ãã¨ããã¨ãã©ã¦ã¶ä¸ã§åãããã®ãä¸è¬çã§ããã DOM ã«ä¾åããªããã¸ãã¯ãæ½è±¡çãªã¢ã¸ã¥ã¼ã«ã®ãã¹ãã¯ã§ããã°ã³ãã³ãã©ã¤ã³ä¸ã§é«éã«å®è¡ãããå³åº§ã«ãã£ã¼ãããã¯ãå¾ãããã®ã§ãã
(æ´æ°) ãããã¬ã¹ãã©ã¦ã¶ PhantomJS ãç¾ãããããã§ã DOM ã«å¯¾ãããã¹ãã QUnit-TAP ã§ã©ãã©ãæ¸ããããã«ãªãã¾ãã
QUnit ã¯ã JavaScript ç¨ã®ã·ã³ãã«ã§å¼·åãªãã¹ãã£ã³ã°ãã¬ã¼ã ã¯ã¼ã¯ã§ããæå jQuery ã®ãã¹ãç¨ã«éçºãããå¾ã« jQuery éä¾åã«ãªããç¬ç«ãã¦åä½ã§ã使ããããã«ãªãã¾ããã QUnit ã¯ãã©ã¦ã¶ãç¡ãã¦ãåä½ããã¨ãããã¨ããªãªã¼ã¹å½åãã謳ããã¦ãããå®éã«ã³ãã³ãã©ã¤ã³ã§ãåä½ãã¾ããããããã QUnit ã¯ããã©ã«ãã§ã¯ãã©ã¦ã¶åãã®åºåãã©ã¼ãããããæä¾ãã¦ãã¾ããã§ããã
ããã§ãã³ãã³ãã©ã¤ã³åºåã®ä»çµã¿ãéçºãã¦ã¿ããã¨èãã¾ãããã³ãã³ãã©ã¤ã³åãã®åºåã®ãã©ã¼ãããã¨ãã¦ã¯ã Perl ã®ä¸çãä¸å¿ã«åºãæ®åãã¦ãã TAP (Test Anything Protocol) å½¢å¼ãé¸æãã¾ãããTAP å½¢å¼ã§åºåãããã¨ã§ãã³ãã³ãã©ã¤ã³ä¸ã§ãã¹ãçµæã確èªããã ãã§ãªãã TAP ã対象ã¨ããæ¢åãã¼ã«(ä¾ãã° prove)ã¨ã®é£æºã試ã¿ã¾ããã
CommonJS 対å¿
å ã㦠CommonJS 対å¿ãè¡ãã¾ãããCommonJS ã¨ã¯ãããã±ã¼ã¸ã³ã°ä»æ§ãä¾åé¢ä¿ã®è¨è¿°/å¶å¾¡ãæ¨æºã©ã¤ãã©ãªãªã©ãä»æ§åãããµã¼ããµã¤ãã§ã JavaScript ã§ããã°ã©ãã³ã°ã§ããããã«ããããã®è©¦ã¿ã§ãã
ãã¹ãã®ä»çµã¿ã node.js ã narwhal ä¸ã§ã使ããããåä½ããããã¨èããQUnit-TAP ã CommonJS 対å¿ããã¾ããã
åèã«ãããªã³ã¯
TAP ã Perl ã®ä¸çã®ãã¹ãã«ã¤ãã¦ã¯ gihyo.jp ã® id:charsbar ããã®é£è¼ãããããããã§ãã
- 第27å Test::Mostï¼Test::Moreã§ã¯ç©è¶³ããªããªã£ã¦ããã
- 第28å Test::Moreï¼no_planããdone_testingã¸
- 第29å Test::Baseï¼ãã¼ã¿æ¬ä½ã®ãã¹ããããã¨ãã¯
TAP ã®ä»æ§ã«ã¤ãã¦ã¯ä»¥ä¸ã®ãªã³ã¯ãåèã«ãã¾ãã
- http://search.cpan.org/~petdance/Test-Harness-2.65_02/lib/Test/Harness/TAP.pod
- http://testanything.org/
- http://en.wikipedia.org/wiki/Test_Anything_Protocol
ã¾ã JavaScript 㧠TAP ã¨ãã㨠Yappo ããã® JSTAPd ãããã¾ãã JSTAPd 㯠QUnit-TAP ãããããã«å
æ¬çãªãã¹ããè¡ãä»çµã¿ã§ã Ajax ãã¢ãªã¢ãªä½¿ã£ãç»é¢ã®ãã¹ãã§ãèªååã§ãããã¨ãå¼·ã¿ã§ããèå³ãããæ¹ã¯æ¯é使ã£ã¦ã¿ã¦ãã ããã
QUnit-TAP ã¯ã©ã¡ããã¨ãã㨠js ã®åä½ãã¹ãããµã¼ããµã¤ã js (node.js ç)ã®ãã¹ãã®ç°¡æ½ãªåºåãæåãã¦ãã¾ãã
QUnit-TAP ã®ä½¿ãæ¹
3ã¹ããã㧠QUnit-TAP ã使ããããã«ãªãã¾ã
- qunit.js ããã¼ã
- qunit-tap.js ããã¼ã
- qunitTap é¢æ°ã« QUnit ãªãã¸ã§ã¯ãã¨åºåç¨ã®é¢æ° (print ã¨ã puts ç)ã渡ã
ä¸è¨3ã¹ãããã§ã QUnit ã« TAP åºåããæ©è½ãçµã¿è¾¼ããã¨ãã§ãããã¹ãã³ã¼ãã«æãå ããªãã¨ã Rhino ã SpiderMonkey ã§åããã¨ãã¹ããã TAP åºåãã§ããããã«ãªãã¾ãã
æ°è»½ã« QUnit-TAP ããã¸ã§ã¯ããã lib/qunit-tap.js ãèªåã®ã¨ããã«ã³ãã¼ãã¦ä½¿ã£ããã git submodule ã¨ãã¦ä½¿ã£ãããã¦ãã ããã
(æ´æ°) node.js ã®ããã±ã¼ã¸ããã¼ã¸ã£ã§ãã npm ã«ã対å¿ããã¾ããã
(2011/03/25 æ´æ°) åæåæ¹æ³ãå¤ãã£ãã®ã§è¨è¿°ãä¿®æ£ãã¾ããã
ãµã³ãã«ãåããæºå
qunit-tap ããã¸ã§ã¯ãã«ãµã³ãã«ãå梱ãã¦ãã¾ãã®ã§ããããåããã¦ã¿ã¾ãã
ã¾ã㯠git ãªãã¸ããªãæã£ã¦ãã¦ããã®å¾ã§ submodule (svn ã® external ã®ãããªãã®) ãåå¾ãã¾ãã
$ git clone git://github.com/twada/qunit-tap.git $ cd qunit-tap $ git submodule update --init
ããã¸ã§ã¯ãã¯ä»¥ä¸ã®ãããªãã£ã¬ã¯ããªæ§æã«ãªã£ã¦ãã¾ãã sample ãã£ã¬ã¯ããªä»¥ä¸ããµã³ãã«ã³ã¼ãã§ã
- sample/js
- é常㮠JavaScript ã§åä½ããããµã³ãã«
- sample/commonjs
- CommonJS ã§åä½ããããµã³ãã«
- sample/interop
- é常㮠JavaScript 㨠CommonJS ã©ã¡ãã®ç°å¢ä¸ã§ãåä½ããããã«è©¦ã¿ããµã³ãã«
ã¨ãªã£ã¦ãã¾ãã
$ tree . |-- GPL-LICENSE.txt |-- MIT-LICENSE.txt |-- README.md |-- lib | `-- qunit-tap.js |-- package.json |-- sample | |-- commonjs | | |-- lib | | | |-- incr.js | | | `-- math.js | | |-- test | | | |-- incr_test.js | | | |-- math_test.js | | | `-- tap_compliance_test.js | | `-- test_helper.js | |-- interop | | |-- index.html | | |-- lib | | | |-- incr.js | | | |-- math.js | | | `-- namespaces.js | | |-- phantomjs_test.sh | | |-- run_qunit.js | | |-- run_tests.js | | |-- test | | | |-- incr_test.js | | | |-- math_test.js | | | `-- tap_compliance_test.js | | `-- test_helper.js | `-- js | |-- index.html | |-- lib | | |-- incr.js | | `-- math.js | |-- phantomjs_test.sh | |-- run_qunit.js | |-- run_tests.js | `-- test | |-- incr_test.js | |-- math_test.js | `-- tap_compliance_test.js |-- test | |-- regression.rb | `-- regression_spec.rb `-- vendor `-- qunit |-- README.md |-- package.json |-- qunit | |-- qunit.css | `-- qunit.js `-- test |-- headless.html |-- index.html |-- logs.html |-- logs.js |-- same.js `-- test.js 16 directories, 43 files $
Rhino ã§åããã¦ã¿ã
JavaScript ã¨ã³ã¸ã³ã® Java å®è£ ã Rhino ã§åããã¦ã¿ã¾ãã
(Ubuntu 10.04 LTS ã§ã¯ sudo aptitude install rhino rhino-doc ã§ã¤ã³ã¹ãã¼ã«ã§ãã¾ãã Rhino ã®å®è¡å½¢å¼ã¯ãã ã® jar ãã¡ã¤ã«ã§ãã®ã§ã Windows ã Mac ã§ã Rhino ã® jar ããããã° 'java -jar js.jar run_tests.js' ã§åé¡ãªãåä½ããã¨æãã¾ã)
$ cd sample/js/ $ rhino run_tests.js # module: math module # test: add ok 1 ok 2 ok 3 - passing 3 args ok 4 - just one arg ok 5 - no args not ok 6 - expected: 7 result: 1 not ok 7 - with message, expected: 7 result: 1 ok 8 ok 9 - with message not ok 10 not ok 11 - with message # module: incr module # test: increment ok 12 ok 13 # module: TAP spec compliance # test: Diagnostic lines ok 14 - with # multiline # message not ok 15 - with # multiline # message, expected: foo # bar result: foo # bar not ok 16 - with # multiline # message, expected: foo # bar result: foo # bar 1..16 $
å®è¡ãã㨠'ok' ã 'not ok' ãªã©ãã¹ãçµæãæ¨æºåºåã«åºã¦ãã¾ããããã®åºåå½¢å¼ã TAP ãã©ã¼ãããã§ãã 'ok' ã§å§ã¾ãè¡ãã¢ãµã¼ã·ã§ã³æåã 'not ok' ã§å§ã¾ãè¡ãã¢ãµã¼ã·ã§ã³å¤±æã '#' ã§å§ã¾ãè¡ã¯ã³ã¡ã³ãã§ãã
ããããã詳ãã
å ã»ã©ã³ãã³ãã©ã¤ã³ä¸ã§å©ããã®ã¯ Rhino ã SpiderMonkey ãããã¹ããåããããã«æ¸ããã³ã¼ãã§ãã
sample/js/run_tests.js
load("./lib/math.js"); load("./lib/incr.js"); load("../../vendor/qunit/qunit/qunit.js"); load("../../lib/qunit-tap.js"); qunitTap(QUnit, print, {noPlan: true}); QUnit.init(); QUnit.config.updateRate = 0; load("./test/math_test.js"); load("./test/incr_test.js"); load("./test/tap_compliance_test.js"); QUnit.start();
åç´ãªã³ã¼ãã§ãããã¹ã対象ããã¼ããã QUnit 㨠QUnit-TAP ããã¼ãããQUnit-TAP ã®è¨å®ã QUnit ã®åæåãè¡ãããã¹ãã³ã¼ãããã¼ãããæå¾ã«ãã¹ããéå§ãã¦ãã¾ãã
ãããã¯ãã³ã¼ã
ä»åã®ãããã¯ãã³ã¼ãã®ãµã³ãã«ã¯åç´ãªè¶³ãç®ã¨ã¤ã³ã¯ãªã¡ã³ãã®ããã°ã©ã ã§ãã CommonJS Spec Wiki ã® CommonJS Modules ä»æ§ã®ãµã³ãã«ã³ã¼ããè¥å¹²ã¢ã¬ã³ã¸ãã¦ä½¿ã£ã¦ãã¾ãã incr.js ã math.js ã使ããã¨ããé¢ä¿ã«ãªã£ã¦ãã¾ãã
sample/js/lib/math.js
if (typeof math === 'undefined') { math = {}; } math.add = function() { var sum = 0, i = 0, args = arguments, l = args.length; while (i < l) { sum += args[i++]; } return sum; };
sample/js/lib/incr.js
if (typeof incr === 'undefined') { incr = {}; } incr.increment = function(val) { var add = math.add; return add(val, 1); };
ãã¹ãã³ã¼ãã¨ãç°¡å㪠QUnit å ¥é
ãã¹ãã³ã¼ãã¯ãã¡ãã QUnit ã使ã£ã¦ãã¾ãã QUnit-TAP ã¯è£æã§åãã¾ãã®ã§ããã¹ãã³ã¼ãã¯ç´ç²ãª QUnit ã®ã³ã¼ãã§ãã
sample/js/test/math_test.js
module("math module"); test('add' , function() { var add = math.add; equals(add(1, 4), 5); equals(add(-3, 2), -1); equals(add(1, 3, 4), 8, 'passing 3 args'); equals(add(2), 2, 'just one arg'); equals(add(), 0, 'no args'); });
sample/js/test/incr_test.js
module("incr module"); test('increment' , function() { var inc = incr.increment; equals(inc(1), 2); equals(inc(-3), -2); });
使ã£ã¦ãã QUnit API ã®èª¬æ
ä»åã¯åç´ãªãã¹ãã³ã¼ããªã®ã§ãç»å ´ãã QUnit API ãå°éã§ãã以ä¸ã«ãã使ã QUnit API ãåæãã¾ãã
- test( name, expected, test )
- ã²ã¨ã¤ã®ãã¹ããå®ç¾©ãã¾ãã expected å¼æ°ã¯ã¢ãµã¼ã·ã§ã³å®è¡ã®äºæ¸¬æ°ã§ãçç¥å¯è½ã§ã(詳ãã㯠QUnit ã®ãã¹ãã³ã¼ããè¦ã¦ã¿ã¦ãã ãã)ã
- module( name, lifecycle )
- è¤æ°ã®ãã¹ããã¾ã¨ãã¾ãã module ã¬ãã«ã§ã®åæåãå¯è½ã§ãã ãªã module å¼ã³åºãã¯å¿ é ã§ã¯ããã¾ããã test ã ãã§ããã¹ãã¯åä½ãã¾ãã
- equals( actual, expected, message )
- å¼æ° actual 㨠expected ãç価ã§ãããã¨ã == ã§æ¤è¨¼ãã¾ããmessage å¼æ°ã¯çç¥å¯è½ã§ãã
- ok( state, message )
- å¼æ°ãçã§ãããã¨ãæ¤è¨¼ãã¾ã(JUnit ã§ããã¨ããã® assertTrue ã«ç¸å½ãã¾ã)
QUnit ã«æ°ãã«è¿½å ããã(ããã¥ã¡ã³ãåãã¦ããªã) API
å®ã¯ QUnit ã¯å»å¹´æ®ãã« CommonJS ã® assert ã©ã¤ãã©ãªã« API ãåããã¦ãã¾ããããµã¤ãã§ã¯ç¹ã«è¨åããã¦ããªãããã§ãã CommonJS äºæã® assertion ã¡ã½ãããè¤æ°è¿½å ããã¦ãã¾ãã
- equal( actual, expected, message )
- equals ã®ã¨ã¤ãªã¢ã¹ã§ããå¼æ° actual 㨠expected ãç価ã§ãããã¨ã == ã§æ¤è¨¼ãã¾ã
- notEqual( actual, expected, message)
- equal ã®å対ã§ããå¼æ° actual 㨠expected ãç価ã§ãªããã¨ã != ã§æ¤è¨¼ãã¾ã
- deepEqual( a, b, message)
- same ã®ã¨ã¤ãªã¢ã¹ã§ããå¼æ° a,b ã Array ã Object ã®å ´åã«ã¯å帰çã«æ¯è¼ãè¡ããç価ã§ãããã¨ãæ¤è¨¼ãã¾ã (Perl ã® is_deeply ã«ç¸å½ãã¾ã)
- notDeepEqual( a, b, message)
- deepEqual ã®å対ã§ããå帰çã«æ¯è¼ããç価ã§ãªããã¨ãæ¤è¨¼ãã¾ãã
- strictEqual( actual, expected, message)
- å¼æ° actual 㨠expected ãç価ã§ãããã¨ã === ã§æ¤è¨¼ãã¾ã
- notStrictEqual( actual, expected, message)
- strictEqual ã®å対ã§ããå¼æ° actual 㨠expected ãç価ã§ãªããã¨ã !== ã§æ¤è¨¼ãã¾ã
QUnit ã«ã¯ä»ã«ãéåæãã¹ãã®ä»çµã¿ãã«ã¹ã¿ãã¤ãºãªã©ã¾ã ã¾ã æ©è½ã¯ããã¾ãããããã«è©³ãã㯠QUnit ã®ãµã¤ããåèã«ãã¦ãã ããã
ãã¹ãåºåä»æ§ã®éè¦æ§
TAP ãé¸æããçç±ã¯ããã¹ãçµæã®åºåå½¢å¼ãã·ã³ãã«ã§ããã¤ç¹å®ã®ããã°ã©ãã³ã°è¨èªããã¹ãã£ã³ã°ãã¬ã¼ã ã¯ã¼ã¯ããç¬ç«ãã¦ããããã§ãã
TAP ã®åºèªã Perl ã³ãã¥ããã£ãªã®ã§ãã¼ã«ã¯å½ç¶ Perl ãå¤ãã§ãããæ¬è³ªçã«ã¯ UNIX ä¼çµ±ã®ãã¬ã¼ã³ããã¹ãæåã«åã£ã¦ãã¾ããã¤ã¾ãããã¹ãçµæã UNIX æåã§ãããã¤ãã¨ãã£ã«ã¿ã使ã£ãããã°ã©ãã³ã°ã®å
¥åã¨ãã¦ä½¿ããã¨ãããã¨ã§ããããã°ã©ããèªåã®ããã®ãã¼ã«ãæ¸ãããããã©ã¼ãããã ã¨æãã¾ãã
失æãããã¹ãããããã©ãã
ä¾ãã°ã失æããã¨ãã ãåããããã«ããããªãã° grep ããã°è¯ãããã§ã
$ js run_tests.js | grep '^not ok'
prove ã使ã£ã¦ã¿ã
TAP ã対象ã¨ãããã¹ãåé/èªååãã¼ã« prove ã使ãã¾ãã
ãã®ä¾ã¯åç´ã§ããããã¹ãã®æ°ãå¤ããªã£ã¦ãã㨠prove ã®ä¾¿å©ããå®æã§ãã¾ãã
$ prove --timer --exec=/usr/bin/rhino run_tests.js [12:28:04] run_tests.js .. ok 1037 ms [12:28:05] All tests successful. Files=1, Tests=7, 1 wallclock secs ( 0.05 usr 0.00 sys + 1.27 cusr 0.11 csys = 1.43 CPU) Result: PASS $
ãã¡ã¤ã«ãæ´æ°ãããããã¹ããåãã
ãã¡ã¤ã«ãæ´æ°ãããããã¹ããåãã autotest ã®ãããªããã¿ãç°¡åã«ä½ãã¾ãã
以ä¸ã®ã¹ã¯ãªããã¯ç§ã使ã£ã¦ããã¹ã¯ãªãã(ããã¡ãã£ã¨ãµã³ãã«ç¨ã«ç·¨éãããã®)ã§ãã inotifywait ã³ãã³ãã§ãã¡ã¤ã«æ´æ°ãç£è¦ããæ´æ°ããã£ãããã¹ããå®è¡ããã¹ãçµæã notify-send (Mac ã® growl ã®ãããªãã®) ã«éç¥ãã¦ãã¾ãã
autotest.sh
#!/bin/sh while inotifywait -r -e modify public/test ; do js tests.js | tee js_test.log | grep '^not ok' > /dev/null if [ $? -eq 0 ]; then notify-send -u critical -t 1000 --icon=./fail.png 'FAILED' else notify-send -u normal -t 1000 --icon=./pass.png 'SUCCEEDED' fi done
ã½ããã¦ã§ã¢ãæ¢å(ã¦ã)ã¨ãã¦ä½¿ã
ãUNIX ã¨ããèãæ¹ãã®ä¸ã«ããã½ããã¦ã§ã¢ãæ¢å(ã¦ã)ã¨ãã¦ä½¿ããã¨ããèãæ¹ãåºã¦ãã¾ããæ©è½æºè¼ã®å¤§ããªã½ããã¦ã§ã¢ãæ¸ãã®ã§ã¯ãªãããããããåæ©è½ã§ä¾¡å¤ã«éä¸ããããã°ã©ã ãã·ã§ã«ã¹ã¯ãªãããã°ã«ã¼è¨èªã§ã¤ãªãã¦ä»äºãæãéããã¨ããèãæ¹ã§ãã
ä»åç§ãæ¸ããã³ã¼ã㯠QUnit ã« TAP åºåãããã¦ã¿ãã¨ããã³ã¼ããè¡æ°ã¯ç¾æç¹ã§ 71 è¡ã§ã(ãã®ã»ã¨ãã©ã¯ CommonJS 対å¿ã§ã)ããã£ãããã ãã®ã³ã¼ãã§ããåºæ¥ããã¨ãããããã¨å¢ãã¦ãã¾ããããã½ããã¦ã§ã¢ãæ¢å(ã¦ã)ã¨ãã¦ä½¿ããã¨ãããã¨ãå®æã§ããã®ã¯ããããã¨ãã§ãã
ãUNIX ã¨ããèãæ¹ãã¯ãç§ã®ãã¤ãã«ã§ããUNIX ã®èå¾ã«æµãã価å¤è¦³ãè¨è¨ææ³ãã·ã³ãã«ãªè¨è¨ã¨ã¯ä½ããå¦ã¹ããå¤ãã®æè¡è ã«ããããã®æ¬ã§ãã
UNIXã¨ããèãæ¹âãã®è¨è¨ææ³ã¨å²å¦
- ä½è : Mike Gancarz,è³å°¾æ¡
- åºç社/ã¡ã¼ã«ã¼: ãªã¼ã 社
- çºå£²æ¥: 2001/02/01
- ã¡ãã£ã¢: åè¡æ¬
- è³¼å ¥: 40人 ã¯ãªãã¯: 498å
- ãã®ååãå«ãããã° (145件) ãè¦ã
ãã¦ã CommonJS ã«ã¤ãã¦ãæ¸ããããã¨ãããããããã®ã§ãããã¨ã³ããªãé·ããªã£ã¦ããã®ã§ã¾ãããã§çµããã«ãã¾ãã CommonJS ã«ã¤ãã¦ãã CommonJS ã¨æ®éã® js ã®ã©ã¡ãã§ãåãã³ã¼ãã®æ¨¡ç´¢ã«ã¤ãã¦ããä»å¾ã©ããã§æ¸ãã¦ã¿ããã§ãã
ãã®ã¨ã³ããªãèªã㧠QUnit-TAP ã«èå³ãæãããæ¹ã¯ãæ¯éä¸åº¦è©¦ãã¦ã¿ã¦ãã ããã