Rake TestTask won't run Unit Tests - ruby

my rake TestTask looks like this
Rake::TestTask.new(:tasks) do |t|
t.libs << "test"
t.test_files = FileList['tasks/tasks*.rb']
t.verbose = true
end
My unit test named tasks01.rb looks like this
require_relative '../../class_based_page_objects/tasks/add_task'
require_relative '../../class_based_page_objects/tasks/tasks_grid_view'
require_relative '../../helpers/ssh_test_session'
require_relative '../../class_based_page_objects/login'
require "test-unit"
gem "test-unit"
require "selenium-webdriver"
puts "Will you print this?"
class Task01Test < Test::Unit::TestCase
def setup
#test Setup goes here
puts "Setup"
end
def test_assign_to_not_real_user
# Test goes here
puts "Test"
end
end
When I
rake tasks
I get
"Will you print this?"
What am I missing?

You just need a description line in your Rakefile code, like so:
desc "A description here without a period"
Rake::TestTask.new(:tasks) do |t|
t.libs << "test"
t.test_files = FileList['tasks/tasks*.rb']
t.verbose = true
end

Related

Uninitialized constant NameError in Rspec

When I run rails c, I can call the following class and the method works:
test = SlackService::BoardGameNotifier
test.create_alert("test")
>>method works
I'm trying to set this up in rspec like this:
require 'spec_helper'
require 'slack-notifier'
RSpec.describe SlackService::BoardGameNotifier do
describe '#notify' do
#notifier = SlackService::BoardGameNotifier
it 'pings Slack' do
error = nil
message = "test"
expect(notifier).to receive(:ping).with(message)
notifier.send_message()
end
end
end
But I keep getting the error:
NameError:
uninitialized constant SlackService
Does this have to do with how I set up the module?
My current setup:
slack_service/board_game_notifier.rb
module SlackService
class BoardGameNotifier < BaseNotifier
WEBHOOK_URL = Rails.configuration.x.slack.url
DEFAULT_OPTIONS = {
channel: "board-games-channel",
text: "board games alert",
username: "bot",
}
def create_alert(message)
message #testing
end
end
end
slack_service/base_notifier.rb
module SlackService
class BaseNotifier
include Singleton
def initialize
webhook_url = self.class::WEBHOOK_URL
options = self.class::DEFAULT_OPTIONS
#notifier = Slack::Notifier.new(webhook_url, options)
end
def self.send_message
message = instance.create_alert("test")
instance.notify(message)
end
def notify(message)
#notifier.post blocks: message
end
end
end
Add this to your spec_helper.rb
# spec_helper.rb
ENV["RAILS_ENV"] ||= "test"
require File.expand_path("../config/environment", __dir__)
When running RSpec, Rails doesn't automatically boot up, and therefore doesn't automatically load all the libraries.
Also, I'd suggest creating a .rspec in your app's root folder with the following lines so that spec_helper is automatically loaded for all your RSpec tests:
# .rspec
--format documentation
--color
--require spec_helper
I would use the described_class from Rspec
require 'spec_helper'
require 'slack-notifier'
RSpec.describe ::SlackService::BoardGameNotifier do
describe '#notify' do
it 'pings Slack' do
error = nil
message = "test"
expect(described_class).to receive(:ping).with(message)
notifier.send_message()
end
end
end

Rake not running any tests with minitest

I have a file containing a class of multiple tests (using minitest). I have require 'minitest/autorun' at the top of the file and all tests run correctly when I call the file directly (ruby my_tests.rb).
So far, so good. However, now I'm trying to run my tests via rake.
require "rake/testtask"
task :default => [:test]
Rake::TestTask.new do |t|
t.libs << Dir.pwd + "/lib/examples"
t.test_files = FileList['test/test*.rb']
end
Calling rake shows test/my_test.rb getting called but no tests within the class get run (0 tests, 0 assertions, etc.). I do get these warnings:
...gems/minitest-5.8.0/lib/minitest/assertions.rb:17: warning: already initialized constant MiniTest::Assertions::UNDEFINED
...ruby/2.1.0/lib/ruby/2.1.0/minitest/unit.rb:80: warning: previous definition of UNDEFINED was here
How can I run my tests within rake successfully? I am not using rails.
EDIT: Here is the top of my test file:
require 'minitest/spec'
require 'minitest/autorun'
require 'minitest/reporters'
reporter_options = { color: true }
Minitest::Reporters.use![Minitest::Reporters::DefaultReporter.new(reporter_options)]
class Test_PowerSpecInputs < Minitest::Test
def setup
#mc = TestClass.new()
end
def test_does_lib_have_constant
# my test code
end
end
Try changing your Rakefile to this.
require "bundler/gem_tasks"
require "rake/testtask"
Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.libs << "lib"
t.test_files = FileList['test/**/*_test.rb']
end
task :default => :test
jphager2 got me thinking about tool versions and it turned out that my version of rake was fairly old. Updating to 11.x did the trick.

unit test in ruby not printing results to console

I'm trying to set up a testing environment in ruby using rake. It seems like the code I have gets reached by rake but it doesn't return any test results. I'm hoping I'm missing something simple and you could lend me a hand.
rakefile
require 'rake'
require 'rake/testtask'
Rake::TestTask.new do |t|
t.libs = ["lib"]
t.warning = true
t.verbose = true
t.test_files = ['test/numbersTest_test.rb']
end
task default:[:test]
numbersTest_test.rb
require "test/unit"
class TestMyApplication < Test::Unit::TestCase
def dummyCase
assert(false, "dummy case failed")
end
end
Result when I run "rake"
C:\Users\Daniel\Classes\assign1\PerfectNumbersRuby
λ rake
C:/Ruby21-x64/bin/ruby.exe -w -I"lib" -I"C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib" "C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/rake_test_loader.rb" "test/numbersTest_test.rb"
test
There are no testcases in your code. Testcases are methods whose name starts with test_.

ActiveSupport TestCase not running in Sinatra

I'm setting up a new Sinatra app and am having issues getting my tests to run via a rake task. When I run rake:test, the task runs, shows me which files it will be running, but nothing happens. I know it's loading the class because it has failed due to syntax errors, but I never see my tests running. What am I missing? Below is my configuration and example test:
rakefile.rb
require "rake/testtask"
require "sinatra/activerecord/rake"
require "./app"
task :default => :test
TEST_FILES = FileList["test/**/test*.rb"]
desc "Run all of the tests for redFish"
Rake::TestTask.new do |t|
t.libs << "test"
t.test_files = TEST_FILES
t.verbose = true
end
task :default => "test"
test/test_helper.rb
ENV["RACK_ENV"] = "test"
require "rack/test"
require "awesome_print"
require "active_support"
require "active_support/core_ext"
/test/unit/test_organization.rb
require File.expand_path '../../test_helper.rb',__FILE__
class TestOrganization < ActiveSupport::TestCase
def setup
puts "setup for tests"
end
test "validates_required_fields" do
puts "RUNNING TESTS"
assert true
refute false
end
end
When I run rake:test, I can see that test_helper and test_organization.rb are being found by the TestTask, but I don't see any tests pass/fail.
Am I missing something obvious?
Looks like the issue was caused by not requiring minitest/autorun in my test helper. I added that line, and the tests ran fine.

A rake task by .rake file

I have the following Rakefile in a Ruby 1.9.3 project:
require 'rake/testtask'
require 'json'
Rake::TestTask.new do |t|
t.pattern = "spec/**/*_spec.rb"
t.verbose = true
end
task :default => :test
namespace :omglol do
namespace :file_a do
task :foo do
# do some stuff
end
end
namespace :file_b do
task :bar do
# do some stuff
end
end
end
As you can see, the first part of this file allow to run tests, just using rake command. And the second part contains some tasks.
Actually, I have a lot of tasks inside omglol:file_a and omglol:file_b namespaces. That's why I would like to move each of them inside a file, for instance tasks/omglol/file_a.rake and tasks/omglol/file_b.rake.
Is there a best way to do so? Thanks.
Yes. Simply move the logic into the appropriate files and then require them.
Example Rakefile:
require 'rake/testtask'
require 'json'
require 'lib/tasks/omglol/file_a.rake' # <= contains your subtasks
Rake::TestTask.new do |t|
t.pattern = "spec/**/*_spec.rb"
t.verbose = true
end
task :default => :test
Then in lib/tasks/omglol/file_a.rake simply define your tasks as normal:
namespace :omglol do
namespace :file_a do
task :foo do
# do some stuff
end
end
end
The pattern would be the same for file_b.rake.

Resources