Testing ruby script - ruby

I'm creating a Ruby (not Rails) script and I have to test using standard Rails unit tests and Cucumber. I can't use RSpec.
So how do I test a ruby script with Cucumber and Rails Test::Unit? Is it possible?

Here, I created a complete example including a Rakefile for convenience. The file structure is this:
./notepad.rb
./Rakefile
./test
./test/test_notepad.rb
./features
./features/add_note.feature
./features/step_definitions
./features/step_definitions/add_note_step.rb
In fact this example doesn't even use classes, so you can really test typical "Scripts" with it. File contents are here: http://pastebin.com/sJUP7VSA
So in the end you can do this:
$ rake test
Finished tests in 0.001368s, 730.9942 tests/s, 1461.9883 assertions/s.
1 tests, 2 assertions, 0 failures, 0 errors, 0 skips
$ rake cucumber
1 scenario (1 passed)
2 steps (2 passed)
0m0.002s
Cheers, Philip

Related

minitest-reporters does not change minitest output style

I have been trying to use the minitest-reporters gem to alter the output style of Ruby's builtin minitest testing library. However, it does not actually change the output.
It should be noted that I am not using Rails or Rake, but I didn't think that would make a difference. I am simply trying to test a Ruby command-line program that I have written.
Here's a dumb little test case (let's call it dumbtest.rb) that I was trying out:
require 'minitest/autorun'
require 'minitest/reporters'
Minitest::Reporters.use! [Minitest::Reporters::DefaultReporter.new(:color => true), Minitest::Reporters::SpecReporter.new]
describe "MiniTest demo" do
describe "when asked about the number 2" do
it "should be equal to the number 2" do
2.must_equal 2
end
end
end
When I run the test, it just produces the default minitest output (i.e. colorless, no descriptions of passing tests, etc.):
$ ruby -Ilib:test dumbtest.rb
Run options: --seed 48983
# Running:
.
Finished in 0.001356s, 737.6595 runs/s, 737.6595 assertions/s.
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
With minitest-reporters enabled, I expect the output to look something more like this (i.e. list both passing and failing tests as opposed to just failing, the word PASS is colored green, the final summary is color-coded, etc.):
There are no runtime errors. It's just not working for me. Any idea why?

Selenium-Webdriver in Ruby with minitest. How to run specific test case inside a file

Im transitioning from Rspec to minitest to test the later comparing speed and paralellism, maintenance, etc.
In Rspec, I can run my test cases with the command:
rspec path/to/test/suite.rb -e 'Should test this case'
'Should test this case' is this line inside the suite.rb file:
it 'Should test this case' do
...
end
But I can't seem to find any way to do this in minitest.
Not only that you can run individual tests, but you can also use Regex expressions
$ ruby path/to/test/suite.rb --name /Should test this case/

Recursively glob for RSpec test files at runtime

I have a suite of RSpec tests I want to group under the following hierarchy:
tests/
featA/
t1.rb
t2.rb
featB/
t3.rb
but when I run
$ rspec tests
I get the following:
rspec tests
No examples were matched. Perhaps {:unless=>#<Proc:0x00007f318919cc08#/usr/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:51>, :if=>#<Proc:0x00007f318919cdc0#/usr/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:50>} is excluding everything?
Finished in 0.00003 seconds
0 examples, 0 failures
I feel like I'm going mad, but there doesn't seem to be a way to get RSpec to recursively glob for test files? Does this functionality exist?
EDIT:
I have a workaround by doing this:
$ rspec `find tests -name "*.rb"`
but I suspect I shouldn't have to. Am I right?
You've exposed an oversight on my part! In rspec-1, you could say this:
spec test --pattern "**/*.rb"
But the --pattern option is missing in rspec-2. I've just added it (in development) and it will be included in the rspec-2.6.0 release.
I usually manage running RSpec on my specs via rake. The relevant portion of my Rakefile looks something like this:
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |t|
t.rspec_opts = ['--color', '-f progress', '-r ./spec/spec_helper.rb']
t.pattern = 'spec/**/*_spec.rb'
t.fail_on_error = false
end
Now rake spec runs RSpec with the appropriate options; you'll need to change t.pattern to match the specs you want to run.
Be sure to check out the RSpec2 site for more information.

How do you write and execute Ruby tests in a Ruby interpreter?

I was working with the lotrepls Ruby interpreter, and I want like to write tests in the interpreter that I can then write Ruby code to pass. In Python, I can write doctests and then write code to pass the doctests. For example:
>>> b
1
This tests that b=1, and entering b=1 will get this doctest to pass.
Is there a similar way to write tests in a Ruby interpreter, execute them, write code to pass the tests, and then execute the test again? Is there a Ruby doctest equivalent? For my application, I will execute tests and code in a hosted interpreter like lotrepls rather than install something on my local machine.
There's RubyDocTest, but I'd encourage you to look at something like RSpec or another modern BDD/TDD framework.
It's pretty easy to write tests there too, and you get access to complex and/or custom assertions that you can't really get in a doctest. For instance, here's a simple set of tests for a baseball scoring app:
describe BaseballScorer do
before :each do
#s = Scorer.new(Game.new)
end
it "should score a 0-0 game when no runs are hit" do
#s.home.score.should == #s.away.score.should == #s.total_runs
end
it "should record runs that are hit" do
#s.game.run_hit(:away)
#s.away.runs.should == #s.away.score.should == 1
end
# ...
This is a little old post but I faced the same problem some months ago.
John's answer is correct but if you want to use something similar to a irb session you could try byexample, in particular it supports Ruby
For example you can write a Markdown doc like this:
This is an awesome expression:
```ruby
>> 1 + 2
=> 3
```
Then you just run from the shell
$ byexample -l ruby your-markdown-doc.md
[PASS] Pass: 1 Fail: 0 Skip: 0
You could also embed the test inside a Ruby comment like
# square 2
# => 4
def square x
x * x
end
And that's it. The example is executed and checked so your doc works as regression test as well.
Disclaimer: Like I said, I had the same desire to do TDD in Ruby so I wrote byexample. I really hope than others find it as much as useful and I do.

How do you run a single test/spec file in RSpec?

I want to be able to run a single spec file's tests — for the one file I'm editing, for example. rake spec executes all the specs. My project is not a Rails project, so rake spec:doc doesn't work.
Don't know if this matters, but here is my directory structure.
./Rakefile
./lib
./lib/cushion.rb
./lib/cushion
./lib/cushion/doc.rb
./lib/cushion/db.rb
./spec
./spec/spec.opts
./spec/spec_helper.rb
./spec/db_spec.rb
Or you can skip rake and use the 'rspec' command:
bundle exec rspec path/to/spec/file.rb
In your case I think as long as your ./spec/db_spec.rb file includes the appropriate helpers, it should work fine.
If you're using an older version of rspec it is:
bundle exec spec path/to/spec/file.rb
The raw invocation:
rake spec SPEC=spec/controllers/sessions_controller_spec.rb \
SPEC_OPTS="-e \"should log in with cookie\""
Now figure out how to embed this into your editor.
This question is an old one, but it shows up at the top of Google when searching for how to run a single test. 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:
Let's say it's saved in spec/models/foo_spec.rb. Then you would run:
rspec spec/models/foo_spec.rb:2
and it would just run that one spec. In fact, that number could be anything from 2 to 5.
You can also use the actual text of the *e*xample test case with -e !
So for:
it "shows the plane arrival time"
you can use
rspec path/to/spec/file.rb -e 'shows the plane arrival time'
./scripts/spec path/to/spec/file.rb -e 'shows the plane arrival time'
no need for rake here.
from help (spec -h):
-l, --line LINE_NUMBER Execute example group or example at given line.
(does not work for dynamically generated examples)
Example: spec spec/runner_spec.rb -l 162
To run all of your rspec files: rspec
note: you must be in the root of your project
To run one rspec file: rspec 'path_to/spec.rb'
note: replace 'path_to/spec.rb' with your path. Quotation marks optional.
To run one rspec test from one file: rspec 'path_to/spec.rb:7'
note: :7 is the line number where the test starts
If you installed rspec as a plugin rather than as a gem, then you won't have the spec executable.
At any rate, All you need to do is run the file using ruby. The rspec code is clever enough to run the tests for you.
eg:
ruby myclass_spec.rb
http://github.com/grosser/single_test lets you do stuff like..
rake spec:user #run spec/model/user_spec.rb (searches for user*_spec.rb)
rake test:users_c #run test/functional/users_controller_test.rb
rake spec:user:token #run the first spec in user_spec.rb that matches /token/
rake test:user:token #run all tests in user_test.rb that match /token/
rake test:last
rake spec:last
Ruby 1.9.2 and Rails 3 have an easy way to run one spec file:
ruby -I spec spec/models/user_spec.rb
Explanation:
ruby command tends to be faster than the rake command
-I spec means "include the 'spec' directory when looking for files"
spec/models/user_spec.rb is the file we want to run.
Although many great answers were written to this question, none of them uses the Rspec tags approach.
I use tags to run one or more specs in different files -- only those related to my current development task.
For example, I add the tag "dev" with the value "current":
it "creates an user", dev: :current do
user = create(:user)
expect(user.persisted?).to be_truthy
end
then I run
bundle exec rspec . --tag dev:current
Different tags/values can be set in individual specs or groups.
I was having trouble getting any of these examples to work, maybe because the post is old and the commands have changed?
After some poking around I found this works:
rspec spec/models/user_spec.rb
That will run just the single file and provides useful output in the terminal.
specky.vim
Alternatively, have a look at autotest.
Running autotest in a command window will mean that the spec file will be executed whenever you save it. Also, it will be run whenever the file you are speccing is run.
For instance, if you have a model spec file called person_spec.rb, and a model file that it is speccing called person.rb, then whenever you save either of these files from your editor, the spec file will be executed.
Lets say, you're running test for creating todo. You can always run that specific todo spec code using the file crete_spec.rb file as below.
rspec/spec/features/controller/spec_file_name.rb
Example:
Creating rspec spec/features/todos/create_spec.rb
Editing rspec spec/features/todos/edit_spec.rb
Deleting rspec spec/features/todos/destroy_spec.rb
If you want to run all the specs in one single short.
rspec
If you want to run all the specs in a specific controller user this.
rspec/spec/feaures/controller_name
Example: rspec/spec/features/todos
Hope it gives you more understanding!
And you can run specific line into your test file
rspec spec/models/model_spec.rb:47

Resources