First API testing Framework with Rake - ruby-on-rails

I am setting up my own API testing framework but I am using this guide as a foundation.
In the guide it uses rake to run the tests from the rake file by using the default rake command.
The command runs all tests under the spec folder; however, I want to create a rake task I can run from the command line and just pass in which file under that folder I would like to run.
I.E. I want to run one script at a time from the command line, instead of running every file with the default rake command.
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
task :default => :spec
task :run do |file|
:spec => file
end
You can see the default rake command will run all tests under spec folder; however, I just want to pass in the file I want to run. How can I achieve this?

Related

How to run a custom list of minitest tests in Rails

Say I have a file containing name of test files to run tests from and It can contain specific test names too. If test file contains that specific test, run only that test from the file containing the test and run all tests from other test files.
I use Codebuild to run tests for our application but Codebuild does not provide a way to run only specific tests. So I am replacing bin/rails test command on codebuild with our custom rake task. That rake task will check for a file in our system containing list of tests to run and If it finds the file it run only those tests other normal bin/rails test
You could copy how rails is already defining their tasks like test:system here:
namespace :test do
desc "Run tests from file"
task from_file: "test:prepare" do
$: << "test"
Rails::TestUnit::Runner.rake_run(
File.read(Rails.root.join("tests_to_run.txt")).lines.map(&:chomp)
)
end
end
$ bundle exec rails test:from_file

How can i add a task that will run after rake test:db:prepare in Rails

I am using rails 4.2 and i am trying to run test using the command rake test.I am trying to use plv8 extention so i create it manually from psql console, then when i run the test it seems like it has deleted that extension from postgres. I looked into rails 4.2 project and i noticed that they brought test:db:prepare back. This is what is deleting the plv8 extension every time. how can i add a pice of code that will run after the test:db:prepare or test:db:create?
I am not a big fan of modifying Rakefile solution.
You can name multiple tasks inline:
rake test:db:create test:db:prepare custom:task
Or when you create a new rake task you can make it dependent on any other task:
desc "the dependent task will run before this one"
task my_task: :other_task do
# stuff
end
You can also arbitrarily invoke other tasks:
Rake::Task['db:test:prepare'].invoke
More info here: http://jasonseifer.com/2010/04/06/rake-tutorial and here: How to run Rake tasks from within Rake tasks?

How can I create a rake task that will always run when any Rake task is ran?

From what I remember, in the documentation is specified that in the test environment, the database is always cleared even when you run rake ( with no arguments ). I'd like to achieve such a thing, so that it doesn't matter if I run a task or not, when I run rake, there's always a Rake task being executed. Is this possible? Is this where the default task kicks in?
Create a file called rakefile in the directory you want to run the task from.
This code will make it so that if you just type "rake" my_default_task will run:
task :default => 'my_default_task'
task :my_default_task do
puts "Now I am doing the task that Tempus wants done when he/she types 'rake' in the console."
end
task :my_not_default_task do
puts "This isn't the default task."
end
However, if you typed rake my_not_default_task, then my_default_task would NOT run. If you want it to run regardless here is one thing you can do:
task :default => 'my_default_task'
task :my_default_task do
puts "This is the default task"
end
task :my_not_default_task do
puts "This isn't the default task."
end
Rake::Task['my_default_task'].invoke
The last line in this code ensures that my_default_task runs even when you call some other task, so if you typed rake my_not_default_task the my_default_task'would also run.
EDIT:
When you're working with rails you can put the tasks above in a file in the lib/tasks folder with an extension of .rake and rails will automagically run them when you do rake
Jason Seifer has a real nice tutorial on rake.

Ruby on Rails: How to run a rake task for the env.rb?

in features/support/env.rb in cucumber, i would like to have a rake task run everytime i start my tests... but not before each scenario.. just.. once.
This is what i need to run
Rake::Task["db:test:prepare"].reenable
Rake::Task["db:test:prepare"].invoke
Assuming a standard cucumber install with the rake file cucumber.rake in app/lib/tasks.
Something like this should work
task :data_prep
Rake::Task["db:test:prepare"].reenable
Rake::Task["db:test:prepare"].invoke
end
task :all => [:data_prep,:ok,:wip]
task :default => [:data_prep,:cucumber]
All you do is define another task (:data_prep) and add a call to it for the existing cucumber tasks.
Not the cleanest of methods but I cannot remember if cucumber has a method to run on startup and not on a per scenario basis.

Rails: How to test code in the lib/ directory?

I have a model which gets its data from a parser object. I'm thinking that the parser class should live in the lib/ directory (although I could be persuaded that it should live soewhere else). The question is: Where should my unit tests for the parser class be? And how do I ensure that they are run each time I run rake test?
In the Rails application I'm working on, I decided to just place the tests in the test\unit directory. I will also nest them by module/directory as well, for example:
lib/a.rb => test/unit/a_test.rb
lib/b/c.rb => test/unit/b/c_test.rb
For me, this was the path of last resistance, as these tests ran without having to make any other changes.
Here's one way:
Create lib/tasks/test_lib_dir.rake with the following
namespace :test do
desc "Test lib source"
Rake::TestTask.new(:lib) do |t|
t.libs << "test"
t.pattern = 'test/lib/**/*_test.rb'
t.verbose = true
end
end
Mimic the structure of your lib dir under the test dir, replacing lib code with corresponding tests.
Run rake test:lib to run your lib tests.
If you want all tests to run when you invoke rake test, you could add the following to your new rake file.
lib_task = Rake::Task["test:lib"]
test_task = Rake::Task[:test]
test_task.enhance { lib_task.invoke }
I was looking to do the same thing but with rspec & autospec and it took a little digging to figure out just where they were getting the list of directories / file patterns that dictated which test files to run. Ultimately I found this in lib/tasks/rspec.rake:86
[:models, :controllers, :views, :helpers, :lib, :integration].each do |sub|
desc "Run the code examples in spec/#{sub}"
Spec::Rake::SpecTask.new(sub => spec_prereq) do |t|
t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""]
t.spec_files = FileList["spec/#{sub}/**/*_spec.rb"]
end
end
I had placed my tests in a new spec/libs directory when the rpsec.rake file was configured to look in spec/lib. Simply renaming libs -> lib did the trick!
An easy and clean way is just to create a directory under test/unit/lib. Then create test as test/unit/lib/foo_test.rb corresponding to lib/foo.rb. No new rake tasks required, and you can nest more directories if needed to match the lib directory structure.
As of Rails 4.0:
rake test:all # Run all tests in any subdir of `test` without resetting the DB
rake test:all:db # Same as above and resets the DB
As of Rails 4.1, redefine test:run to include additional tasks when running rake or rake test:
# lib/tasks/test.rake
namespace :test do
Rake::Task["run"].clear
task run: ["test:units", "test:functionals", "test:generators", "test:integration", "test:tasks"]
["tasks"].each do |name|
Rails::TestTask.new(name => "test:prepare") do |t|
t.pattern = "test/#{name}/**/*_test.rb"
end
end
end
This has the added bonus of defining rake test:tasks in the given example.
As of Rails 4.2, test:run includes all subdirs of test including them when running rake test, and thus rake.
To not define additional rake tasks to run tests from the custom defined folders you may also run them with the command rake test:all. Tests folders structure for the lib folder or any other custom folder is up to you. But I prefer to duplicate them in classes: lib is matched to test/lib, app/form_objects to test/form_objects.
Use:
[spring] rake test:all
to run all tests, including the directories you created (like [root]/test/lib/).
Omit [spring] if tou aren't using it.

Resources