ruby Test::Unit Command line options? - ruby

When running tests in Ruby's unit::test framework, is there a really easy way to specify, from the command-line, that only one test should be run (that is, specify the test class and test member variable)? If not, is there another framework that has this feature?

ruby /path/to/foo_test.rb --name test_should_do_something_really_spiffy
That will call the test defined by the method test_should_do_something_really_spiffy in that file.
EDIT: That's for the Test::Unit framework that most ruby tests are written with. I am assuming you meant the same.

If you have the full Test::Unit framework, you can do
ruby /path/to/foo_test.rb --help
to get the command line options. I don't think it works if you just have the minitest version though.

Related

RSpec basics: bin/rspec --format doc

I've installed RSpec on a win7 lappy and am following along the http://rspec.info/ homepage tutorial to make sure everything works. If I am reading their demo correctly bin/rspec --format doc should run the specification test file.
Instead, I get a system prompt for a text editor... ? I am confused.
Any explanation of what is going on or guidance about how to get my RSPEC configuration working in accordance to the makers homepage would be great.
FWIW Ruby 2.2.5p319, Bundler version 1.13.1 and gem -v tells me 2.6.7 (originally I had 2.4 but that is broken on windows, so I upgraded according to http://guides.rubygems.org/ssl-certificate-update/) Also, I have basic RSpec functionality and have completed the tutorial here: https://www.tutorialspoint.com/rspec/rspec_writing_specs.htm
Ah, I figured out what I need to do... I just need to explicitly call ruby:
ruby bin/rspec --format doc
...and the test gets run - YaY!
Per #JörgWMittag, I confirmed my Environment Variable Path to make sure ruby.exe was in there (C:\Ruby22\bin;).
Then looking at my Program Defaults, I thought that maybe I could tell win7 to associate any file named "rspec" with ruby.exe per https://support.microsoft.com/en-us/help/18539/windows-7-change-default-programs ...but I couldn't actually add file type "extensions" or "protocols" - I could only change the association of existing ones, but .rb and .rbw were in there... Maybe there is a way to do this manually, but I am not a windows expert.
Thinking on all this it occurred to me that I just needed to explicitly tell ruby to ingest the command... Heh.
I apologize if this is off-topic.

Globbing doesn't work with Minitest - Only one file is run

I have placed all my specs in specs/*.rb.
However, when I run Minitest with ruby spec/**/*_spec.rb, only one file is run.
What gives?
This is not minitest specific, but Ruby. You are effectively running a ruby program which knows nothing about the program being run.
Ruby does not support running multiple files at once afaik, so if you want to get a similar result you could try something like:
for file in spec/**/*_spec.rb; do ruby $file; done
UPDATE: for what you want you should probably create a Rake task as described here
You can use the testrbl third party gem to run multiple Minitest files on the command line. You could also use the mtest bin from maxitest extensions.
Using a for loop in bash will incur overhead of loading your application/library for every test you pass it. If you have just ten tests, and you're testing a Rails app that takes 5 seconds to boot, that's over a minute of totally unnecessary load time.

How to make minitest stop execution on failure?

I am using Minitest as the runner for my functional tests, using Selenium as the driver to run the browser. Each test is modeled as MiniTest::Unit::TestCase.
Minitest reports summary of execution when it completes executing all tests. The Exceptions that were encountered are also printed towards the end of the execution. I find it difficult to debug when something unexpected fails as the context of execution is lost. The exceptions I am running into are not deterministic.
Is there a way to make Minitest runner to stop execution of tests on exception or assertion failure?
I am using minitest (2.11.2) and ruby 1.9.2p290 (2011-07-09) [i386-mingw32]
I think you mean to have a "fast fail" option available. I found the fail_fast for minitest (Test::Unit): Immediate backtrace & exit article, but that's out of date (still covers what can be done). I think you'll need to monkeypatch your testing library to enable this option. I found a Gist showing how to add a simple fail-fast option to minitest/turn/minitest-rails so that might get you on the right track. I understand your problem is to do with the first article I've referenced:
When I run the Test::Unit suite in my Ruby on Rails 3 project through
rake test and a test is failing, the default behavior is to just
print “F” or “E”, keep running until all the tests are finished (while
I twiddle my thumbs), and only then print out a stack trace.
try rails test -f will do it. It means abort test run of first failure or error.
As I answered here, I found a gem for: minitest-fail-fast. It works with Rails 4.2 and Minitest 5.6.1
It does not directly answer the question, but might be very useful.
You could use pry-rescue gem so that your tests will launch a pry session whenever something goes wrong. All you have to do is to add the gem into your Gemfile:
group :development, :test do
gem 'pry-rescue'
end
Then launch your tests with the following flag:
PRY_RESCUE_RAILS=1 rails test rails test test/integration/agendas_test.rb
Use -f.
In pure ruby, as you ask for, this means ruby <file> -f.
In rails, this means rails test -f.
Minitest will check the terminal's arguments for the -f flag, so how you call it appears to not be relevant to the -f flag.

rcov outside rails

This may seem obvious but I don't find a way to run Rcov or coverMe outside a Rails project.
I would like to use it with Rspec 2.5
I am using Ruby 1.9.2 so I guess this may be the problem.
I also would like not to use rake tasks but a command on the command line. I have tried several things and the best result I got is Rcov report for :
/var/lib/gems/1.9.1/gems/rcov-0.9.9/lib/rcov/code_coverage_analyzer.rb
and
/var/lib/gems/1.9.1/gems/rcov-0.9.9/lib/rcov/code_coverage_analyzer.rb
No idea why
Check out http://ruby-toolbox.com/categories/code_metrics.html for some alternatives to RCov if using Ruby 1.9 is the problem.
SimpleCov, it runs awesome outside the Rails box.
http://rubydoc.info/gems/simplecov/0.5.4/frames
The latest available version of rcov (0.9.8) still doesn't have good support for 1.9.2. You can try to run it on your project by doing:
rcov spec/*.rb
But you'll most likely get something like:
** WARNING: Ruby 1.9 Support is experimental at best. Don't expect correct results! **
And then some errors after that.

cucumber debugger not stopping

I'm trying to debug a simple BDD test using cucumber. In order to do that, I inserted a debugger statement where I would like to break the control flow. But it seems that cucumber ignores this statement.
I'm running the tests executing:
rake cucumber:wip
It's rather simple, so I think the code isn' t worth pasting here. Thanks in advance
You don't have the rails-debug gem installed and/or included.
Make sure rails-debug listed in your Gemfile
If it's still not being included, make sure you run cucumber like:
bundle exec cucumber ...

Resources