Help us understand the problem. What is going on with this article?

RSpecで作ったexampleの一覧をテストの実行なしに出力する

More than 3 years have passed since last update.

やりたいこと

RSpecで作ったexampleの一覧をテストの実行なしに出力したい。
(つまりテスト項目一覧がほしい)

コマンド

Rails(Spring)で実行する場合

$ bin/rspec -f d --dry-run --order defined

Bundle execで実行する場合

$ bundle exec rspec -f d --dry-run --order defined

オプションの意味

  • -f d = ドキュメント形式で出力する。--format documentationと書いても良い。
  • --dry-run = テストを実行しない(Dry run)
  • --order defined = 定義されている順に実行する。spec_helper.rb等でランダム実行を指定していなければ省略可。

出力例

$ bin/rspec -f d --dry-run --order defined

ContactsController
  administrator access
    behaves like public access to contacts
      GET #index
        with params[:letter]
          populates an array of contacts starting with the letter
          renders the :index template
        without params[:letter]
          populates an array of all contacts
          renders the :index template
      GET #show
        assigns the requested contact to @contact
        renders the :show template

# 省略...

NewsRelease
  example at ./spec/models/news_release_spec.rb:4
  example at ./spec/models/news_release_spec.rb:5
  example at ./spec/models/news_release_spec.rb:6
  returns the formatted date and title as a string

Phone
  does not allow duplicate phone numbers per contact
  allows two contacts to share a phone number

Finished in 0.00696 seconds (files took 1.66 seconds to load)
82 examples, 0 failures

応用:フィーチャスペックだけを一覧化する

$ bin/rspec spec/features -f d --dry-run --order defined   

About BigCo modal
  toggles display of the modal about display

News releases
  as a user
    adds a news release
  as a guest
    reads a news release

User management
  adds a new user

Finished in 0.00072 seconds (files took 1.71 seconds to load)
4 examples, 0 failures

参考資料

その他

上の実行例で使ったコードは電子書籍「Everyday Rails - RSpecによるRailsテスト入門」で使われているテストコードです。

jnchito
SIer、社内SEを経て、ソニックガーデンに合流したプログラマ。 「プロを目指す人のためのRuby入門」の著者。 http://gihyo.jp/book/2017/978-4-7741-9397-7 および「Everyday Rails - RSpecによるRailsテスト入門」の翻訳者。 https://leanpub.com/everydayrailsrspec-jp
https://blog.jnito.com/
sonicgarden
「お客様に無駄遣いをさせない受託開発」と「習慣を変えるソフトウェアのサービス」に取り組んでいるソフトウェア企業
http://www.sonicgarden.jp
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした