How can I count the number of examples in my RSpec tests?

rspec shared examples
rspec example
rspec unit testing
how to run rspec tests
rspec tutorial
rspec validates_presence_of
rspec documentation
rails testing rspec

I want to count how many tests are in a directory without running the tests.

I tried something similar to what I found in this, but wasn't able to print anything other than 0.


This is what I tried (with and without the commented out config stuff):

task 'count_specs' do
  RSpec.configure do |config|
    config.pattern = 'features/**/*_spec.rb'
    # config.inclusion_filter.rules
    # config.filter_run focus: true
    # config.run_all_when_everything_filtered = true
    # config.inclusion_filter.rules
    config.requires = ['spec_helper']

    config.load_spec_files

    puts 'count of examples'
    puts config.files_to_run

  puts RSpec.world.example_count
  end

  puts 'count of examples'
  puts RSpec.world.example_count
end

How can I create a task that counts how many examples match a specific pattern?

I want to run this against two directories and compare the result.

In RSpec 3 there is a --dry-run option which print your suite's formatter output without running any examples or hooks.

rspec --dry-run

If you need to have the count of examples in your ruby script:

output = `bundle exec rspec --dry-run | grep 'examples, 0 failures'`
example_count = output.split(' ').first.to_i

Show number of examples in rspec (Example), Counting the number of specs in a test suite is difficult to do with regex due to * Shared example groups * Dynamic spec code (i.e. examples� If you want to run this code for each example (example = test in RSpec) you can use :each instead of :all. How To Create Testing Subgroups. If you’re testing different scenarios in your app then it may be helpful to group related tests together. You can do this using a context block in RSpec. Here’s an example:

Perhaps there is some specific RSpec method for this but I suggest this script:

Dir["**/*_spec.rb"].map { |path| File.read(path) }.join.scan(/(  it)|(  scenario)/).count

It is very naive but reliable.

This variant search " it" and " scenario", but you can change pattern if you like.

This variant works only for tests without blocks like

RSpec.describe 'smth' do
  array.each do |i|
    it { should_smth }
  end
end

The Definitive RSpec Tutorial With Examples, we have to find out some valid values online or by hand calculation. Then we use those with our tests. wrong number of arguments (given 1, expected 0) And you'll get your first passing test: Essentially, rearrange the digits to create the highest possible number. Examples: Input: 21445 Output: 54421. Input: 145263 Output: 654321. Input: 1254859723 Output: 9875543221. Lets start by creating a descending_order.rb file where we'll code our solution. Now in your spec directory, create a descending_order_spec.rb file. This is where we'll write our tests.

RSpec.configuration.world.example_count

This is undocumented so it could break in future releases. Note that this is the cumulative number of tests that have run, so the number isn't accurate until the after every test has run.

How to Test Rails Models with RSpec, By the end of this tutorial you will know how to test your Rails models writing just the right amount of code and design to make the software work. For this example, we will need to create an Auction object without a title. Relish helps your team get the most from Behaviour Driven Development. Publish, browse, search, and organize your Cucumber features on the web.

Effective Use of RSpec Shared Examples, RSpec shared examples can be used to DRY up your code and make testing easier. meaning there isn't much context required, and they can be used in many of your tests. expect { perform }.to change { User.count }.by(1) I don’t know if it’s a recent addition, but to run a single test out of a spec you can do the following: rspec path/to/spec:<line number> where -line number- is a line number that contains part of your test. For example, if you had a spec like: 1: 2: it "should be awesome" do 3: foo = 3 4: foo.should eq(3) 5: end 6:

RSpec Tests for Beginners. Use Patterns to Simplify Writing Tests , Spec tests appeared pretty daunting to me the first few times I approached them, but after awhile, certain patterns emerged and Build as many of these files as you need for your resources. }.to change(User,:count).by(1) The --profile command line option (available from RSpec.configure as. #profile_examples ), when set, will cause RSpec to dump out a list of your. slowest examples. By default, it prints the 10 slowest examples, but you can. set it to a different value to have it print more or fewer slow examples.

Profile examples - Configuration - RSpec Core - RSpec, #profile_examples ), when set, will cause RSpec to dump out a list of your When: I run rspec spec; Then: the examples should all pass; And: the output should� This RSpec tutorial will be based on implementing a part of a string calculator. The plan is to: Create a simple string calculator with a method int Add(string numbers) The method can take 0, 1 or 2 numbers, and will return their sum. For an empty string it will return 0. For example, input can be “” or “1” or “1,2”.

Comments
  • You could try and use the --dry-run option From the documentation Use the --dry-run option to have RSpec print your suite's formatter output without running any examples or hooks.
  • Thanks for the suggestion, but I need to get the example_count programmatically. I can do it manually, but I'm trying to find a way to do it using the RSpec object.
  • Don't need it "fails in example" do; fail; end. It made +1 example
  • @carterw485 What you mean by counting programmatically? You mean a variable named example_count inside your ruby code to save example counts?
  • Yeah. I want to use whatever RSpec uses to count the examples. RSpec.world.example_count should work, but getting RSpec to update it is what I'm having a hard time with.
  • @carterw485 Check the updated answer. does this work?
  • This doesn't work if there are shared example groups or examples defined with loops.
  • @SajadRastegar, thanks, you're right. I added these limitations to the answer
  • I have many different indentations of it. I could check for a lot of them, but I want to just hook into whatever RSpec already does in order to count them.