"cucumber foo.feature" not working from command line [duplicate] - ruby

I've inherited a Rails (3) app and am trying to get to grips with the existing Cucumber tests. I've got the following setup in the app's 'features' folder (I've missed out any files which aren't relevant, eg extra features and steps)
/features
/people
new-person.feature
/step_definitions
people_steps.rb
web_steps.rb
/support
env.rb
paths.rb
selectors.rb
If I run 'rake' then it runs all the features in features/people/new-person.feature, correctly using the steps listed in step_definitions.
However, I don't want to run rake every time as it takes too long, I just want to run a specific test in Cucumber, e.g. cucumber features/people/new-person.feature -l 8
When I do this, it runs the feature but hasn't loaded the steps. I get this back:
Using the default profile...
Feature: Add a new person
In order to allocate tasks to people
As a community manager
I want to add a new person
Scenario: Secondary navigation should contain "Add new person" # features/people/new-person.feature:8
Given I am on the new person page # features/people/new-person.feature:9
Undefined step: "I am on the new person page" (Cucumber::Undefined)
features/people/new-person.feature:9:in `Given I am on the new person page'
Then I should not see "Add new person" # features/people/new-person.feature:10
Undefined step: "I should not see "Add new person"" (Cucumber::Undefined)
features/people/new-person.feature:10:in `Then I should not see "Add new person"'
1 scenario (1 undefined)
2 steps (2 undefined)
0m0.005s
You can implement step definitions for undefined steps with these snippets:
Given /^I am on the new person page$/ do
pending # express the regexp above with the code you wish you had
end
Then /^I should not see "([^"]*)"$/ do |arg1|
pending # express the regexp above with the code you wish you had
end
If you want snippets in a different programming language, just make sure a file
with the appropriate file extension exists where cucumber looks for step definitions.
Why isn't Cucumber loading the steps in? I'm guessing that I need to require the steps somewhere but I can't work out where.
Thanks, Max

Max Williams found the answer to his question:
EDIT - found the answer, here: https://rspec.lighthouseapp.com/projects/16211/tickets/401-envrb-not-loaded-when-running-individual-features-in-sub-directories
In config/cucumber.yml there's a line that looks like this:
std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} --strict --tags ~#wip"
change it to
std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} --strict --tags ~#wip --require features/"
This is like adding --require features/ to the end of your cucumber test run and makes it load everything up properly.

I did not already have a cucumber.yml file to change, for whatever reason. So, simply creating a blank /config/cucumber.yml file and putting the line in it:
std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} --strict --tags ~#wip --require features/"
did not work. (That is not surprising, I figured there was supposed to be more to it than just that one line.)
I found a good example of a FULL, working, cucumber.yml example file here: http://jdfrens.blogspot.com/2010/03/uh-yes-i-did-define-that-cucumber-step.html

Related

How do you use cucumber with zeus?

When I start up zeus, it does not offer zeus cucumber as one of the possible commands. Others seem to get this by default; At least I have seen a couple zeus write-ups that show the output from zeus start including zeus cucumber, and they don't say anything about that having been special or required extra configuration.
I don't really even know where to start to troubleshoot this; I have googled and searched here for "use cucumber with zeus." I get no setup discussions. The only results I get are from people who seem to take for granted that it should be there, and are investigating problems with it not functioning correctly.
You should use this custom plan file from Zeus. Save it as custom_plan.rb at the root of your application:
require 'zeus/rails'
# 1. Add the cucumber methods (below) to your custom plan (or take this file if
# you don't have an existing custom_plan).
#
# 2. Add the following line to the test_environment section of your zeus.json:
#
# "cucumber_environment": {"cucumber": []}
class CucumberPlan < Zeus::Rails
def cucumber_environment
::Rails.env = ENV['RAILS_ENV'] = 'test'
require 'cucumber/rspec/disable_option_parser'
require 'cucumber/cli/main'
#cucumber_runtime = Cucumber::Runtime.new
end
def cucumber(argv=ARGV)
cucumber_main = Cucumber::Cli::Main.new(argv.dup)
had_failures = cucumber_main.execute!(#cucumber_runtime)
exit_code = had_failures ? 1 : 0
exit exit_code
end
end
Zeus.plan = CucumberPlan.new

Cucumber.yml was found, but can't be parsed for Parallel_Tests

When running multiple features with the Ruby gem Parallel_Tests in cucumber using this command:
parallel_cucumber features/
with a cucumber.yml file under my project root>config folder, which looks like:
default: --format html --out report<%= ENV['TEST_ENV_NUMBER'] %>.html
I receive the following error message:
cucumber.yml was found, but could not be parsed. Please refer to cucumber's documentation on correct profile usage.
I have looked into this and some others have thought it was due to a rerun.txt file, but i have not created this file and a project file search returns nothing. I am currently at a loss of what is causing cucumber to fail reading in the yaml file. Any help would be great.
As described by another post, I went into lib/
cucumber/cli/profile_loader.rb and added a STDERR output like so:
begin
#cucumber_yml = YAML::load(#cucumber_erb)
rescue StandardError => e
STDERR.puts #cucumber_erb
raise(YmlLoadError,"cucumber.yml was found, but could not be parsed. Please refer to cucumber's documentation on correct profile usage.\n")
end
Here is the result:
#parallel_reports: --format html --out reports/cukes_.html
cucumber.yml was found, but could not be parsed. Please refer to cucumber's documentation on correct profile usage.
#
default: --format htm#l
-
oduetf aruelpto:r t-2-.fhotrmmla #hptamrla l-l-eolu_tr erpeoprotrst:4 .-h-tfmolr a#tp ahrtamlll e-l-_oruetp orretpso:r t-s-/fcourkmeast_ .hhttmmll - ouctu cruempboerrt.sy/mclu kweass_ .fhotumnld, buctu ccuomubledr .nyomtl bwea sp afrosuendd., Pblueta sceo urledf enro tt ob ec upcaurmsbeedr.' sP ldeoacsuem ernetfaetri otno ocnu ccuomrbreerc'ts pdroocfuimleen tu astaigoen.
n correct profile usage.
I stumbled upon this problem and only found solutions that suggested removing rerun.txt. That was not an option for me because I rely on that file to rerun failing scenarios.
For some reason Cucumber outputs failing scenarios separated by \n in rerun.txt which is not accepted by the default command found in cucumber.yml.
My solution was to change the first line of cucumber.yml to substitute \n with a space:
rerun = File.file?('rerun.txt') ? IO.read('rerun.txt').gsub(/\n/, ' ') : ""
I had the same issue with the tests of Rails-Cucumber itself. In my case, just running this line fixed the problem:
rm .cucumber.rerun
Caution, sometimes the rerun file can have a different name.
There is a suggestion to use gem update –system
For me, the error
cucumber.yml was found, but could not be parsed with ERB
meant that I was running my tests from RubyMine, and had set my Features folder not to the root of my project, but to a subfolder in the project called features.
When I changed this to the root of my project, it worked out fine!

how to run capybara sinatra

I was given a sample sinatra project with a hello world for capybara testing in akephalos. I understand the concept by looking at the code, but how do i run it? If I run rackup config.ru, and then go to :9292 I just see a hello world. Great, what is that telling me? How do I run the test? The project is bare bones, but below is a file called example_spec.rb. How can I see it fail, for example by looking for "Hi world" and watching it fail? Hope this is enough info. Thought I would check here before I ask the dude that supplied me with the test, thanks!
# describe and context blocks are optional but help organize things
describe 'the index page' do
include x
# :js => true is used to run the test in Firefox. Otherwise it runs headless
# and without JS support
it 'can view the index page', :js => true do
visit '/'
# check to see if the page has the following text (ignoring tags)
page.should have_content('Hello, world!')
# visit https://github.com/jnicklas/capybara to see a complete list of
# assertions
end
You need to set Capybara.app = <your Sinatra class>. Perhaps something like this:
setup do
Capybara.app = Main
end
bundle exec rspec spec, This means run "bundle exec rspec" on the "spec" directory

How do you run a specific test with test/spec (not a specific file, but a spec within a given file)?

With Test::Unit, I can run:
ruby path/to/test.rb --name=test_name_that_i_want_to_run
Thus far, I have not been able to figure out how to do this with test/spec specifications. I am wondering if the way that specifications are automatically named does not allow me to do something like this.
Take the following spec for example:
require 'rubygems'
require 'spec'
describe 'tests' do
it 'should be true' do
1.should == 1
end
it 'should be false' do
1.should_not == 2
end
end
You can execute a single spec by using the -e flag and providing the portion specified by the it block. e.g. ruby my_spec.rb -e 'should be false'
After contacting the gem maintainer, Christian Neukirchen, I found out how to do this, so I am documenting it here for future reference.
specrb path/to/test.rb --name ".*should behave this way.*"
I needed to use the specrb test runner, an extended version Test::Unit's test runner, rather than just the ruby command.
You can also do this with the ruby command:
ruby path/to/test.rb -n "/should behave this way/"

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