System rake test task doesn't run my tests - testing

I'm sure I'm doing something naive or stupid, I'm just not sure what it is.
I'm writing a simple library for parsing data URIs. Being so simple, I figured I'd go ahead and just give ruby-1.9's minitest a whirl. The tests run great when I run them by hand, but when I try to run them with 'rake test', hoping to invoke the system rake test task, I get no joy. Specifically, with trace and verbose:
Donalds-Decisiv-MacBook-Pro:data_uri dball$ rake test -t -v
(in /Users/dball/src/data_uri)
** Invoke test (first_time, not_needed)
I've got tests in the test folder, they all start with test_ and end in .rb. Any ideas?
The repository of the project is http://github.com/dball/data_uri

Tests are not invoked because you didn't give rake any information about them.
Put this task in Rakefile:
require 'rake/testtask'
Rake::TestTask.new do |i|
i.test_files = FileList['test/test_*.rb']
i.verbose = true
end
Or grab a patch for your project.

Related

Jenkins succeed when unit test fails (Rails)

I'm barely started to use Jenkins and this is the first problem I've had so far. Basically my jenkins job always succeed even when an error happened in some of the tests. This is what I'm running in the shell config:
bundle install
rake db:migrate:reset
rake test:units
rake spec:models
Thing is that Jenkins only reports a failure when the task which fails is the last one. For instance, if I put "rake test:units" the last task it will notify an error if something go wrong. Using this configuration I only get error reports for the rspec tests but not for the unit tests.
Anyone wondering why I don't only use rspec or unit test, we are currently migrating to rspec but this problem is still painful.
This is part of the log from Jenkinsm as you can see one of the unit test fails but jenkins still finish with success.
314 tests, 1781 assertions, 1 failures, 0 errors, 0 skips
rake aborted!
Command failed with status (1): [/var/lib/jenkins/.rvm/rubies/ruby-1.9.3-p1...]
Tasks: TOP => test:units
(See full trace by running task with --trace)
Lot of rspec tests here....
Finished in 3.84 seconds
88 examples, 0 failures, 42 pending
Pushing HEAD to branch master of origin repository
Pushing HEAD to branch master at repo origin
Finished: SUCCESS
Jenkins executes the commands you type into a Build Step box by writing them to a temporary file and then running the script using /bin/sh -xe.
Usually this produces the desired effect: Commands are executed in sequence (and printed) and the script aborts immediately when a command fails i.e. exits with non-zero exit code.
If this is not happening to you, the only reason can be that you have overridden this behavior. You can override it by starting the first line of your Build Step with these two characters: #!.
For example, if your Build Step looks like this:
#!/bin/bash
bundle install
rake db:migrate:reset
rake test:units
rake spec:models
Then it means Jenkins will write the script to a temporary file and it will be executed with /bin/bash. When invoked like that, bash will execute commands one-by-one and not care if they succeed. The exit code of the bash process will be the exit code of the last command in the script and that will be seen by Jenkins when the script ends.
So, take care in what you put on the first line of the Build Step. If you do not know how shell works, do not put a hash-bang at all and let Jenkins decide how the script should be run.
If you need more control over how the Build Step is executed, you should study the man page of the shell you use to find out how to make it behave the way you want. Jenkins doesn't have much of a role in here. It just executes the shell you wanted the way you wanted.
Jenkins can only see the result code of the last command run so it has no way of knowing what the result of rake test:units is.
The easiest thing is probably to have each command of those commands as a separate jenkins build step.
An alternative solution is change your first line to the following:
#!/bin/bash -e
This tells your script to fail if any of the commands in the script return an error.
See: Automatic exit from bash shell script on error

How do I create a rake task for a Rails engine which is not exposed to the host application?

# lib/tasks/test.rake
task :hello do
puts 'hello'
end
$ rake app:hello
To run the task I need to prefix it with "app:" and it runs in the context of the dummy app. It is also exposed to the host application (i.e when used as a plugin in a parent Rails app) as rake hello.
I want to run a rake task which does not require a Rails environment and runs some command, but it is run from the engine root, not the dummy app root.
I know that's a bit late, but for others here searching for the correct answer, do the following :
Create your task :
# lib/tasks/your_engine_tasks.rake
desc "Explaining what the task does"
task :your_task do
# Task goes here
end
Then go to your engine ./Rakefile and add
load 'lib/tasks/your_engine_tasks.rake'
Here we go, now:
$ rake -T
gives you your task.
Hope I helped.
I want a better answer to this question however I did figure out that you can add tasks in the Rakefile for the engine (so ./Rakefile not in the spec/dummy application) like so:
task :my_task do
puts "hi!"
end
task :default => [:spec, :my_task]
I would prefer to have my task in another file but at least this provides a way to go forward. In my case, I want to run Konacha javascript tests in the dummy application so my Rakefile looks like this:
task :spec_javascript do
exec 'cd spec/dummy && rake konacha:run' # exec passes command return value up stack!
end
task :default => [:spec, :spec_javascript]

how can I troubleshoot "rake test" missing from my list of rake tasks?

I have two apps. One is a very simple app that I built with rails new..., added a unit test to and ran the unit test. The other is an existing app that is running fine but I'd like to add some tests to it. In AppA (the simple one) when I run rake -vT I see:
...
rake test # Runs test:units, test:functionals, test:integrati...
rake test:recent # Run tests for {:recent=>"test:prepare"} / Test re...
rake test:single # Run tests for {:single=>"test:prepare"}
rake test:uncommitted # Run tests for {:uncommitted=>"test:prepare"} / Te...
...
Which seems normal. But when I run that same command in AppB (the existing app) I don't see any of the commands related to rake test. My first thought was to just 'bring over' the tests from AppA to AppB to see if that would help. So I wiped all content from the test directory in AppB and copied over the test directory from AppA. Still no rake test in the list of apps. But I can run a unit test in AppB via ruby -Itest test/unit/first_test.rb (oddly I have to comment out fixtures :all to get it to work, maybe that's a clue).
Found the answer to this last night. Where a new app's application.rb has:
require 'rails/all'
I had:
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "sprockets/railtie"
require "rails/test_unit/railtie"
I did that because I was following a guide about MongoMapper I think. Go forward a few versions and the last line is commented out -- that's the real reason. I commented it out at the same time I switched my ODM over to Mongoid. I'm not sure why I commented it out, but that definitely did it.
Without knowing the contents of your Rakefile it's really hard to debug this, but you might be missing the .load_tasks call in the Rakefile.
If you're using Rails 3 you should have something like this:
MyApplication::Application.load_tasks
That line will take care of loading the default Rails tasks. You might be able to accomplish the same calling require "rails/tasks" in your Rakefile.

rails 3.1 assets pipeline + acceptance test

we have a rather extensive acceptance test suite, running on our CI server triggered by a Github push hook. i'd like to add a feature or spec to basically test wether rake assets:precompile runs smoothly. we run this task usually in a capistrano callback, but it has happened, that, for whatever reason, some assets couldn't/wouldn't be precompiled, and i missed the warning in that painfully cluttered capistrano output. in my opinion, it would make sense if this breaks a build in our CI.
now my question is, is anybody doing that? is there a 'standard' or easy solution? is there something more comprehensive than just invoking the rake task with should_not raise_error ...?

Automatically run gem tasks in test environment

I have a Rails 3 gem which has some rake tasks that should only be run in the test environment. Running in other environments doesn't really make sense.
My problem is Rake loads the Rails system in order to find my tasks in my gem. So by the time it gets to my tasks Rails is already loaded in the "development" environment (or whatever environment the user specified). This means in order to run my rake tasks properly the user must do:
RAILS_ENV=test rake mytask
Since my task only make sense in the "test" environment this is annoying as I would much rather the user be able to just type:
rake mytask
This is similar to how test:units and test:functionals automatically assume the test environment and the user doesn't need to specify RAILS_ENV=test at the command line. So the question is how do I modify my test so that Rails switches to the test environment?
My current workaround is:
Rails.env = 'test'
ActionMailer::Base.delivery_method = :test
require Rails.root.join('test/test_helper')
This seems to somewhat work but it is still logging to log/development.log and I think it is still actually running the "development" config. Anybody have any ideas? Looking at how the test tasks are defined in Rails itself doesn't reveal how to do it that I can see.
https://github.com/rails/rails/blob/master/railties/lib/rails/test_unit/testing.rake
UPDATE: I've updated my code after taking inputs from Eric's implementation at https://github.com/eric1234/test_inline/commit/fe3da7efa3a2cdb7824c23cfa41697b0ceb9e8e2.
For original code see - https://stackoverflow.com/posts/4600524/revisions
desc "Do something in Test environment"
task :example => :environment do
if not Rails.env.test?
Dir.chdir(Rails.root) do
system "rake example RAILS_ENV=test"
end
else
#.... stuff ....
end
end
I didn't check for the correctness of code, but you get the idea, right?