LoginSignup
22
20

More than 5 years have passed since last update.

test-unitで指定したテストだけ実行する方法

Posted at

はじめに

Rubyらしくテストを書けるRuby用単体テストフレームワークにtest-unitがあります。

Webサイト: https://test-unit.github.io/ja/
GitHub: https://github.com/test-unit/test-unit

普通に実行するとファイルに書かれている全てのテストが実行されますが、
ひとつだけ実行したいときがよくあります。

そんなときは-n-tオプションにテスト名や正規表現を指定することで実現できます。

普通に実行

test-unitのリポジトリーのサンプルを例に実行してみます。

test-unit/test_user.rb at master · test-unit/test-unit
https://github.com/test-unit/test-unit/blob/master/sample/test_user.rb

まず、普通に実行するとこんな感じです。

$ ruby test_user.rb
Loaded suite test_user
Started
..

Finished in 0.000777458 seconds.
------
2 tests, 2 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
------
2572.49 tests/s, 2572.49 assertions/s

2つのテストが実行されました。

テスト名を表示させる

次に、テストの名前を表示させてみます。
ファイル名の後ろに-vまたは--verboseオプションを付けます。

$ ruby test_user.rb -v
Loaded suite test_user
Started
UserTest: 
  test_full_name:                   .: (0.000200)
  ProfileTest: 
    test_has_profile:                   .: (0.000144)

Finished in 0.000863226 seconds.
------
2 tests, 2 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
------
2316.89 tests/s, 2316.89 assertions/s

test_full_nameとtest_has_profileの2つのテストがあることがわかりました。
構成としては、UserTestクラスの中にtest_full_nameというテストと
ProfileTestというサブテストクラスがあります。そして、ProfileTestクラスの
中にtest_has_profileというテストがあります。

テストを指定して実行

それでは、test_full_nameだけ実行してみます。

$ ruby test_user.rb -v -n test_full_name
Loaded suite test_user
Started
UserTest: 
  test_full_name:                   .: (0.000314)

Finished in 0.000953645 seconds.
------
1 tests, 1 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
------
1048.61 tests/s, 1048.61 assertions/s

test_full_nameだけが実行されました。
次はtest_has_profileだけ実行してみます。

$ ruby test_user.rb -v -n test_has_profile
Loaded suite test_user
Started
UserTest: 
  ProfileTest: 
    test_has_profile:                   .: (0.000220)

Finished in 0.000694584 seconds.
------
1 tests, 1 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
------
1439.71 tests/s, 1439.71 assertions/s

また、正規表現で指定することも可能です。

$ ruby test_user.rb -v -n "/test_full_name|test_has_profile/" 
Loaded suite test_user
Started
UserTest: 
  test_full_name:                   .: (0.000208)
  ProfileTest: 
    test_has_profile:                   .: (0.000140)

Finished in 0.00091056 seconds.
------
2 tests, 2 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
------
2196.45 tests/s, 2196.45 assertions/s

オプションの説明

オプションの説明は--helpオプションで見ることができます。

$ ruby test_user.rb --help
Test::Unit automatic runner.
Usage: test_user.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.
                                     Use '/PATTERN/' for NAME to use regular expression.
        --ignore-name=NAME           Ignores tests matching NAME.
                                     Use '/PATTERN/' for NAME to use regular expression.
    -t, --testcase=TESTCASE          Runs tests in TestCases matching TESTCASE.
                                     Use '/PATTERN/' for TESTCASE to use regular expression.
        --ignore-testcase=TESTCASE   Ignores tests in TestCases matching TESTCASE.
                                     Use '/PATTERN/' for TESTCASE to use regular expression.
        --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).

テストクラスを指定して実行

テストクラスを指定したいときは、-tオプションを指定します。
こちらも正規表現が使えます。

$ ruby test_user.rb -v -t "/Profile/"
Loaded suite test_user
Started
UserTest: 
  ProfileTest: 
    test_has_profile:                   .: (0.000241)

Finished in 0.000762848 seconds.
------
1 tests, 1 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
------
1310.88 tests/s, 1310.88 assertions/s

おわりに

Ruby用単体テストフレームワークtest-unitで、指定したテストだけ実行する方法を説明しました。
開発のスピードを上げたいときなどに活用してみてください。

22
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
20