Test::Unit::TestCase from rakefile? - ruby

I'm stuck trying to call individual unit test methods from a rake file to automate my testing but I keep getting an error. Every time I run 'rake manage' I get an error in my 'manage' task saying: wrong number of arguments 0 for 1. Here is my rake file:
require "test_file"
task :default => [:commands]
task :manage do
myTest = Unit_Test.new
myTest.test
end
And my actual class that has uses the Test::Unit::TestCase class. This is in a separate file called 'test_file.rb'.
class Unit_Test < Test::Unit::TestCase
include Rack::Test::Methods
def test
puts "this is just a test"
end
end
There error is pointing to:
myTest = Unit_Test.new
How do I call individual methods from this class? I basically want to call certain methods from this class in different tasks but I cannot get it to work. How do I get this working?

Have you considered doing
ruby test_file.rb --name test_method_to_be_run
Or do you need to run multiple test methods?
Also, what version of Ruby are you using? 1.8 or 1.9?

Related

Unable to call Ruby mixin instance method from RSpec

Objective:
I want to run a basic RSpec unit test on an instance method of a mixin (module) named Debug. Below are the file contents of the Debug mixin:
Mixin File: ./mixins/debug.rb
module Debug
public
def class_info?
"#{self.class.name}"
end
end
Validate Debug mixin instance methods accessible from RSpec:
When I run irb and include the Debug mixin with commands require_relative './mixins/debug.rb' and include Debug, and then call Debug.class_info? it successfully returns "Module"
Then if I run rspec with the following RSpec unit test to confirm that the RSpec context can access the instance methods of the mixin, the test successfully passes:
RSpec Unit Test Setup #1: ./spec/mixins/debug_spec.rb
require_relative '../../mixins/debug.rb'
RSpec.describe Debug, "#class_info?" do
include Debug
before(:each) do
#class_info_instance_method = Debug.instance_methods[0].to_s
end
context "with mixins" do
it "has class info instance method" do
expect(#class_info_instance_method).to eq "class_info?"
end
end
end
Problem when calling Debug mixin instance method from RSpec:
Lastly, I change the RSpec unit test to be as follows, so instead it actually calls the class_info? instance method of the Debug mixin:
RSpec Unit Test Setup #2: ./spec/mixins/debug_spec.rb
require_relative '../../mixins/debug.rb'
RSpec.describe Debug, "#class_info?" do
include Debug
before(:each) do
#class_info = Debug.class_info?
end
context "with mixins" do
it "shows class info" do
expect(#class_info).to eq "Module"
end
end
end
But now when I run rspec from the command line, why does it return the following error? (Note: even though in the previous RSpec Unit Test Setup #1 that was entirely similar I checked I could successfully access this Debug mixin instance method)
1) Debug#class_info? with mixins shows class info
Failure/Error: #class_info = Debug.class_info?
NoMethodError:
undefined method `class_info?' for Debug:Module
Note: I have shared the above code in my RubyTest GitHub repo.
Setup and References:
My System:
Ruby: ruby 2.3.0p0 (ruby -v)
RSpec: 3.5.4 (rspec -v)
References:
Applying example from Mixins chapter of Programming Ruby book
When you include a module, the methods become instance methods in the included class. Debug.class_info? doesn't work because there is no class method class_info?. I'm also not sure that the way you've included the module in your test is the best way to do it. Would something like this work?
require_relative '../../mixins/debug.rb'
class TestClass
include Debug
end
RSpec.describe Debug, "#class_info?" do
let(:test_instance) { TestClass.new }
context "with mixins" do
it "shows class info" do
expect(test_instance.class_info?).to eq "TestClass"
end
end
end

ruby unit testing at_start not found

I am using test-unit-2.5.5 with ruby 1.9.3. In http://test-unit.rubyforge.org/test-unit/en/Test/Unit.html#at_start-class_method there is a method called at_start as part of the ruby test::unit module from version 2.5.2. I tried to use it from the examples on the page like so:
class TestAOS < Test::Unit::TestCase
Test::Unit.at_start do
puts "start"
end
Test::Unit.at_exit do
puts "Exit!"
end
But when I run my test I get the following:
NoMethodError: undefined method `at_start' for Test::Unit:Module
TestAOS at unit/TestAOS.rb:8
(root) at unit/TestAOS.rb:7
Do I need to do anything first before this method can be used? I'm new to ruby
When I comment out the at_start bloack and run the test I get a different error for at_exit:
NoMethodError: private method `at_exit' called for Test::Unit:Module
TestAOS at unit/TestAOS.rb:12
(root) at unit/TestAOS.rb:7
A
In the example provided by your link the
Test::Unit.at_start do
puts "start"
end
is called outside of test class. You are calling it from inside of your test class. Just move it outside of your TestAOS

Trouble w/ Rubymine and unit tests

DISCLAIMER: I am very new to ruby, still trying to get my feet wet, so this could be hugely stupid issue.
I'm trying to get a very simple project and one unit test working, and the Universe is throwing a LifeException (I just can't figure this out)
Using Rubymine 4.0.1 on Mac OS X, 10.7.3.
Launched RubyMine and created a new project (not Rails) "TestExample"
Created a new Ruby Class, file is my_class.rb.
class MyClass
def say_hi
puts "Hi!"
end
end
my = MyClass.new
my.say_hi
Create a new TestUnit Test Template, file is "my_test.rb"
require "test/unit"
class MyTest < Test::Unit::TestCase
def test_create
#my = MyClass.new
end
end
At this point I have two issues:
1. How do I 'require' my class in my tests? If I change the above test case to:
require "test/unit"
require "my_class"
class MyTest < Test::Unit::TestCase
def test_create
#my = MyClass.new
end
end
and attempt to run my "All tests in: TestExample" configuration, I get a "Exception message: cannot load such a file -- my_class". The Tests folder and working directory are pointed to the location of the files. (every file is in the same folder)
The other is a "Unable to attach test reporter to test framework".
I've googled and attempted to figure this out to no avail. I realize this is two questions in one, and if I could just get tests working, I'd be happy.
Thanks for any input and don't laugh to hard at my uber-ruby-noobness.
[Update] - This only happens when using the RVM: ruby-1-9.3-p125 SDK. If I use the ruby-1.8.7-p249(/usr/bin/ruby), it does work. This has to be some configuration issue.
For the first question, try:
require_relative 'my_class'
I can't answer the second.

How to use omit for Test::Unit::TestCase in Ruby to omit/skip the execution of a certain method/test

I am trying to skip/omit some tests. My environment is Ruby/selenium webdriver/Test Unit.
This how the structure looks:
class HeadTestCase < Test::Unit::TestCase
class UnitTest1 < CCTestCase
class UnitTest1 < CCTestCase
def web_test_01
omit("Skipping this test")
some code related to test
end
Omit method signature is omit("Message",&block)
I dont know what I should put for &block.
Also is this a correct way to skip a test?
Using omit the way you are using it here looks correct. The trick is that you need to be using the test-unit gem and not the minimal version of Test::Unit included with the Ruby distribution.
gem install test-unit
You do not need to use a code block with omit unless you want only part of the code in your test case to be omitted. Use do and end as usual for a block:
def web_test_01
omit("Skipping part of the test") do
# Code here would be skipped
end
# Code here would be executed
end

Ruby undefined method for 'assert' for :class

I'm trying to use the Test::Unit::TestCase API in ruby to do some unit tests but have run into an issue with using assert. I am only trying to call specific methods within a class that uses the Test::Unit::TestCase class but it keeps failing on assert. In my rake file I have:
require 'test_file'
task :manage do
my_app = Test::Unit::TestCase::Unit_Test
my_app.test1
end
And in my test_file.rb I have:
require 'rubygems'
require 'test/unit'
require 'rack/test'
class Unit_Test < Test::Unit::TestCase
include Rack::Test::Methods
# manage tests
def self.test1
browser = Rack::Test::Session.new(Rack::MockSession.new(Sinatra::Application))
browser.get '/homepage'
assert browser.last_response.ok?
end
end
Everything works up until I get to the 'assert' statement which says: Undefined method assert for Unit_Test:Class. If I do not make the method a static method than it will run ALL of the methods within the Unit_Test class. I only want to run specific unit tests from my rake file.
What am I missing?
I can't think of a nice way to use assert from a class method because assert is an instance method.
If you want to run just a specific test method you might be better off making the test an instance method and using the name parameter, e.g.:
ruby test_file.rb --name test1
You should be able to invoke that from your rake task.

Resources