test-unitããããã°ã飯大çãä¸æ¯ã¯ã¤ã±ã
rspec3 ã«ç²å¼ãã¦ããçããããã«ã¡ã¯ãä»æ¥ã¯ rspec3 ã«ç²ããçããã¸æ©ãã®å ´ã¨ã㦠test-unit gem ã®ã話ãæä¾ãããã¨æãã¾ãã
test-unit gem ã«ã¤ãã¦
ãRubyã®ãã¹ãã£ã³ã°ãã¬ã¼ã ã¯ã¼ã¯ã®æ´å²ï¼2014å¹´çï¼ãã®è¨äºã«è©³ããæ¸ãã¦ããã¾ãããtest-unit gem ã¯å¤æ¥ã«ãã£ã test/unit ã¨ã minitest ã¨ãéã第ï¼ã®(第ï¼ï¼ï¼ï¼ãããããã¾ãã)ã®åä½ãã¹ãã©ã¤ãã©ãªã§ãã
Ruby ã³ããã¿ã§ããã @kou æ°ãç²¾åçã«ã¡ã³ããã³ã¹ãç¶ãã¦ããã¦ããã Ruby 2.2 ã«æ¨æºã§ bundle ãããä»ãã¨ããããã¹ãã£ã³ã°ã©ã¤ãã©ãªã§ãã
å¤æ¥ã® test/unit ã«æ¯ã¹ã¦å¤ãã®æ©è½ãå ¥ã£ã¦ããã大å¤ä¾¿å©ã«ãªã£ã¦ãã¾ãã
ãã¹ãã®æ¸ãæ¹
æ¬æ¥ãªãã°ããã§ããã¹ãã®è¨æ³ã«ã¤ãã¦èªãã®ã§ããããã@repeatedly æ°ã以åã«æ¸ãã¦ããã ãTest::Unit ã§ãã¹ããæ¸ããã®è¨äºã大å¤åèã«ãªãã®ã§ããã¡ãã«ãã¾ãããã¾ãã
æ¬è¨äºã§ã¯ãããã«å¯¾ããè£å®ã¨ãã¦ãtest-unit ã®ã³ãã³ãã©ã¤ã³ãªãã·ã§ã³ã«ã¤ãã¦è¨è¼ãã¾ãã
ãã¡ã¤ã«ãæå®ãã¦ãã¹ããå®è¡ããã
rspec ã§ã¯ã以ä¸ã®ããã«ãã¡ã¤ã«ãæå®ãã¦ãã¹ããå®è¡ã§ãã¾ããã
rspec spec/xxx_spec.rb
ãã㯠test-unit ã§ãã§ãã¾ããrequire 'test/unit'
ãã¦ãããã¡ã¤ã«ãç´æ¥æå®ãã¦ãruby ã¹ã¯ãªããã¨ãã¦å®è¡ããã° OK ã§ãã
# test/test_xxx.rb require 'test/unit' class TestXXX < Test::Unit::TestCase end
$ ruby test/test_xxx.rb
ã¡ãªã¿ã«ãrequire ãã¦ã¯ã©ã¹å®ç¾©ãã¦ããã ããªã®ã«ããªãã§ã¹ã¯ãªããå®è¡ãããã®ï¼ã¡ã½ããå¼ãã§ãªããï¼ã¨ããæ°ã«ãªãã¾ãããã©ããã以ä¸ã®ãããªæåã«ãªã£ã¦ãããããã§ãã
ä¸ã®ä¾ã§ã¯ããã¹ãã¯ã©ã¹ããå®ç¾©ããã ããã§ããã¹ããå®è¡ããã¦ãã¾ãã ããã¯ãrequire 'test/unit'ããæã« Test::Unit.autorunãå®è¡ã ã¦ããããã§ãããã®çµæãçµäºæã®å¾å¦çã¨ãã¦å®è¡ãããããã«ãªã£ã¦ã ã¾ãã
cf. http://docs.ruby-lang.org/ja/2.1.0/library/test=2funit.html (æ£ç¢ºã«ã¯è¦ãã¹ãããã¥ã¡ã³ããéãã¾ããä¼¼ããããªæåã§ããã)
ãã«ã
-h
ãªãã·ã§ã³ã§ãã«ããåç
§ã§ãã¾ããtest-unit ã«ã¯ rspec ã®ãããªã³ãã³ãã¯ãªãã®ã§ãããã require 'unit/test'
ãã¦ãããã¡ã¤ã«ãæå®ãã¦ã-h
ãªãã·ã§ã³ãæå®ãã¾ãã
ruby test/test_xxx.rb -h
ã¨ãããããã«ããå ¨é¨è²¼ã£ã¦ããã¾ãã
Test::Unit automatic runner. Usage: test_xxx.rb [options] [-- untouched arguments] -r, --runner=RUNNER Use the given RUNNER. (c[onsole], e[macs], x[ml]) --collector=COLLECTOR Use the given COLLECTOR. (de[scendant], di[r], l[oad], o[bject]_space) -n, --name=NAME Runs tests matching NAME. (patterns may be used). --ignore-name=NAME Ignores tests matching NAME. (patterns may be used). -t, --testcase=TESTCASE Runs tests in TestCases matching TESTCASE. (patterns may be used). --ignore-testcase=TESTCASE Ignores tests in TestCases matching TESTCASE. (patterns may be used). --location=LOCATION Runs tests that defined in LOCATION. LOCATION is one of PATH:LINE, PATH or LINE --attribute=EXPRESSION Runs tests that matches EXPRESSION. EXPRESSION is evaluated as Ruby's expression. Test attribute name can be used with no receiver in EXPRESSION. EXPRESSION examples: !slow tag == 'important' and !slow --[no-]priority-mode Runs some tests based on their priority. --default-priority=PRIORITY Uses PRIORITY as default priority (h[igh], i[mportant], l[ow], m[ust], ne[ver], no[rmal]) -I, --load-path=DIR[:DIR...] Appends directory list to $LOAD_PATH. --color-scheme=SCHEME Use SCHEME as color scheme. (d[efault]) --config=FILE Use YAML fomat FILE content as configuration file. --order=ORDER Run tests in a test case in ORDER order. (a[lphabetic], d[efined], r[andom]) --max-diff-target-string-size=SIZE Shows diff if both expected result string size and actual result string size are less than or equal SIZE in bytes. (1000) -v, --verbose=[LEVEL] Set the output level (default is verbose). (important-only, n[ormal], p[rogress], s[ilent], v[erbose]) --[no-]use-color=[auto] Uses color output (default is auto) --progress-row-max=MAX Uses MAX as max terminal width for progress mark (default is auto) --no-show-detail-immediately Shows not passed test details immediately. (default is yes) --output-file-descriptor=FD Outputs to file descriptor FD -- Stop processing options so that the remaining options will be passed to the test. -h, --help Display this help. Deprecated options: --console Console runner (use --runner).
è¡çªå·ãæå®ãã¦å®è¡ãã
rspec ã®æã¯ã以ä¸ã®ããã«ãã¦è¡çªå·ãæå®ãã¦ãç¹å®ã®ãã¹ããå®è¡ãããã¨ãã§ãã¾ããã
$ rspec spec/xxx_spec.rb:12
ãããã§ããªããã¹ãã£ã³ã°ãã¬ã¼ã ã¯ã¼ã¯ã¸ã®ç§»è¡ãªã©ã§ããã¯ãããªãããã§ãããtest-unit gem ã§ãã§ãã¾ãï¼--location
ãªãã·ã§ã³ã«è¡çªå·ãæå®ãã¦ãã¹ããå®è¡ãããã¨ãã§ãã¾ãã
$ ruby test/test_xxx.rb --location 12
ãã£ãï¼ â( ï½¥ã ï½¥)Ù ÌÌ
ãã¹ãåãæå®ãã¦å®è¡ãã
--name
ãªãã·ã§ã³ã§ç¹å®ã®ãã¹ã(ã¡ã½ãã)ãæå®ãã¦ãã¹ããå®è¡ãããã¨ãã§ãã¾ãã
# test/test_xxx.rb require 'test/unit' class TestXXX < Test::Unit::TestCase def test_foo assert_true(true) end def test_bar assert_false(true) end end
$ ruby test/test_xxx.rb --name test_foo
test_foo
ã®ãã¹ãã ããå®è¡ããã¾ãã注æç¹ã¨ãã¦ã¯ã以ä¸ã®ãã㪠test æåå do; end
ã®è¨æ³ã使ã㨠--name
ãªãã·ã§ã³ã使ç¨ã§ããªããªãã¨ããã§ãã
# test/test_xxx.rb require 'test/unit' class TestXXX < Test::Unit::TestCase test "test_foo" do assert_true(true) end test "test_bar" do assert_false(true) end end
--location
使ããã°ãªãã¨ããªãã¨æã£ã¦ããã®ã§ãå人çã«ã¯ãã¾ãæ°ã«ãã¦ãã¾ããã
ã¿ã°ãæå®ãã¦å®è¡ãã
rspec ã«ã¯ã¿ã°ã¨ããæ©è½ããã£ã¦ãã¿ã°ãæå®ãã¦ãã¹ãããã£ã«ã¿ãªã³ã°ãã¦å®è¡ãããã¨ãã§ãã¾ããã
# stack_spec.rb describe Stack do context "when new" do it '#empty?', :current => true do should be_empty end it '#items', :type => 'getter' do should have(0).items end end end
$ rspec stack_spec.rb --tag current $ rspec stack_spec.rb --tag type:getter
test-unit gem ã«ã¯å±æ§ã¨ããæ©è½ããã£ã¦ãå±æ§ãã¤ãã¦ãã£ã«ã¿ãªã³ã°ãããã¨ãã§ãã¾ãã
require 'test/unit' class TestXXX < Test::Unit::TestCase attribute :current, true test "test_foo" do assert_true(true) end attribute :type, 'getter' test "test_bar" do assert_false(true) end end
$ ruby test_xxx.rb --attribute current $ ruby test_xxx.rb --attribute 'type == "getter"'
Ruby ã®ææ³ã§è¤éãªæ¡ä»¶ãæå®ã§ãã¾ãã
$ ruby test_xxx.rb --attribute 'current || type == "getter"'
ã¾ã¨ã
rspec ã§ã¯ã§ãããªãã·ã§ã³ã test-unit gem ã§ã¯ã©ã®ããã«æå®ããã®ã解説ãã¦ã¿ã¾ããã test-unit gem ã«ã¯ä»ã«ã priority æå®ã®æ©è½ãªã©ããã£ããããã®ã§ãæ¯é調ã¹ã¦ã¿ã¦ãã ããï¼
ããã¥ã¡ã³ããã¼ã¸ ã«ããã®è¾ºæ¸ãã¦ããã¨ãããã ã©ãªã|Ïã»`ï¼ãã©