Why am I getting "Test is not a class" in this Ruby script? - ruby

I've got a problem with this class
require "test/unit"
require "selenium/client"
class Test < Test::Unit::TestCase
def setup
#verification_errors = []
#selenium = Selenium::Client::Driver.new \
:host => "localhost",
:port => 4444,
:browser => "*chrome",
:url => "http://change-this-to-the-site-you-are-testing/",
:timeout_in_second => 60
#selenium.start_new_browser_session
end
def teardown
#selenium.close_current_browser_session
assert_equal [], #verification_errors
end
def test_test
#selenium.open "/apj/gestionnaire/flux.ex"
#selenium.wait_for_pop_up "_self", "30000"
end
end
it says to me that it's not a class :
/test.rb:4: Test is not a class (TypeError)
from C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
from C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
from C:/Documents and Settings/Micro/My Documents/Aptana RadRails Workspace/.metadata/.plugins/org.rubypeople.rdt.testunit/ruby/RemoteTestRunner.rb:301
anyone have any idea ?
Regards
Bussiere

Using Test as your class name is a bad idea. It's an existing constant (referring to a module) as soon you require test/unit
require "test/unit"
Test.class # => Module
Use a different name for your test case.

Using Test as your class name is a bad idea.
Wrong ! Today a new version of the rspec-rails gem has been released fixing this issue in some cases.
You can take a look at the the changelog file:
Fix "Test is not a class (TypeError)" error when using a custom Test class in Rails 4.1 and 4.2. (Aaron Kromer, #1295)

Related

"No tests." error in ruby unit tests

My original test for user.rb looks like this:
require "test/unit"
require "minitest/autorun"
require "rack/test"
require_relative "../lib/kimsin.rb"
ENV['RACK_ENV'] = 'test'
class UserTests < Test::Unit::TestCase
include Rack::Test::Methods
include Kimsin
def app
Sinatra::Application
end
def test_user
#user = User.create :username => "barerd", :password => "abcdef"
get "/users"
assert_equal #user.username, "barerd"
refute_match #user.password, "abcdef"
end
end
The test ran and obviously failed as there was no User class. When I added the User class like below:
module Kimsin
require "data_mapper"
require "dm-migrations"
DataMapper.setup :default, "sqlite:///users.db"
class User
include DataMapper::Resource
include BCrypt
property :id, Serial
property :username, String, :required => true
property :password, String, :required => true
property :salt, String, :default => "876587349506434245565664566"
property :crypto, String, :default => BCrypt::Password.create password + salt
end
User.auto_migrate!
end
it throws a "No tests." error. Actually, not only this one but all tests throw the same error now. I suspected that this has sth to do with ruby in general, because it happened after I gem installed dm-core and at the beginning it threw an error:
"Error loading RubyGems plugin "/home/barerd/.rvm/gems/ruby-1.9.3-p125/gems/rubygems-bundler-0.2.8/lib/rubygems_plugin.rb": cannot load such file -- rubygems_bundler/rubygems_bundler_installer (LoadError)"
But when I try to run tests of other apps, they all work fine.
I use rvm 1.11.6 (stable) and ruby 1.9.3p125 (2012-02-16 revision 34643) [i386-cygwin] on a windows 7 by the way. Any clue to the error?
To note, the core module file kimsin.rb is as follows:
require "sinatra"
require "erb"
require "bcrypt"
require_relative "../lib/kimsin/version"
require_relative "../lib/kimsin/user"
use Rack::Session::Pool, :expire_after => 2592000
set :session_secret, "n9c0431qt043fcwo4ponm3w5483qprutc3q9pfw3r0swaypedx2qafec2qdomvuj8cy4nawscerf"
module Kimsin
get "/" do
title = "Kimsin?"
erb :index, :locals => {:title => title}
end
end

S3 Obfuscated const_missing_from_s3_library error

I'm using the typical Mac/Ruby 1.9.3p125
irb>
require 'aws/s3'
AWS::S3::Base.establish_connection!(:access_key_id => 'AccessKey',:secret_access_key => 'SecretKey' )
Service.buckets
(Same error with Bucket.find or almost anything!)
Gives me:
NameError: uninitialized constant Service
from ~/.rvm/gems/ruby-1.9.3-p125/gems/aws-s3-0.6.2/lib/aws/s3/extensions.rb:206
:in `const_missing_from_s3_library'
from (irb):23
from ~/.rvm/rubies/ruby-1.9.3-p125/bin/irb:16:in `<main>'
I'm following documentation almost to spec but I'm so confused as to why this is happening?
You need to either include AWS::S3 in your class or do AWS::S3.Service.
Here's some code samples:
require 'aws/s3'
class MyClass
include AWS::S3
AWS::S3::Base.establish_connection!(:access_key_id => 'AccessKey',:secret_access_key => 'SecretKey' )
Service.buckets
end
or
require 'aws/s3'
class MyClass
AWS::S3::Base.establish_connection!(:access_key_id => 'AccessKey',:secret_access_key => 'SecretKey' )
AWS::S3::Service.buckets
end

In selenium ruby tests can I combine the setup and teardown methods into one location for all my tests?

In my ruby Selenium Tests there is a lot of the same code in every test. How can I best share code between tests? For example my setup and teardown methods are the same in every file, how can I remove them from every file into one shared file or is that even possible?
def setup
#verification_errors = []
#selenium = Selenium::Client::Driver.new \
:host => "#$sell_server",
:port => 4444,
:browser => "#$browser",
:url => "http://#$network.#$host:2086/",
:timeout_in_second => 60
#selenium.start_new_browser_session
end
def teardown
#selenium.close_current_browser_session
assert_equal [], #verification_errors
end
I've tried putting setup in a shared module and a required file but both present different problems with inheritance of the other methods that need access to the #selenium object that is started. What would be a good design if there is one for sharing the code?
I'm not really sure what test framework you're using, but in rspec you could place it into your spec_helper file and just do a before(:each) / after(:each). I'd check the callback documentation for your framework of choice.
For test unit framework - it seems to work to create a SharedTest class to inherit from Test::Unit::Testcase with setup and teadown methods. Then just subclass the test files SharedTest. The only negative consequence I've found is I had to add a test_default method that does nothing in SharedTest to get it to work. If I name my test method test_default that overides it and seems ok, but not very descriptive...
sharedtest.rb
class SharedTest < Test::Unit::Testcase
def setup
#verification_errors = []
#selenium = Selenium::Client::Driver.new \
:host => "#$sell_server",
:port => 4444,
:browser => "#$browser",
:url => "http://#$network.#$host:2086/",
:timeout_in_second => 60
#selenium.start_new_browser_session
end
def teardown
#selenium.close_current_browser_session
assert_equal [], #verification_errors
end
def test_default
#puts self
end
end
T01_testcasename.rb
class Test_01_whatever < SharedTest
def test_default
#test code
end
I'm still open to better solutions but this seems to be working for me.

How do I use selenium with Ruby?

I made some tests with the Firefox Selenium and then had it exported to Ruby. Although the tests all ran fine in Firefox, I am having trouble running the same suite in Ruby.
I tried to run one of the example programs they have and I also get the same connection refused error. Here is the error I got when trying to run their google_test suite.
tellingsen$ ruby google_test.rb
Loaded suite google_test
Started
E
Finished in 0.001558 seconds.
1) Error:
test_page_search(ExampleTest):
Errno::ECONNREFUSED: Connection refused - connect(2)
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:560:in `initialize'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:560:in `open'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:560:in `connect'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/timeout.rb:62:in `timeout'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/timeout.rb:93:in `timeout'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:560:in `connect'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:553:in `do_start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:542:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:1035:in `request'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:845:in `post'
/Users/tellingsen/.gem/ruby/1.8/gems/selenium-client-1.2.18/lib/selenium/client/protocol.rb:89:in `http_post'
/Users/tellingsen/.gem/ruby/1.8/gems/selenium-client-1.2.18/lib/selenium/client/protocol.rb:12:in `remote_control_command'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/timeout.rb:62:in `timeout'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/timeout.rb:93:in `timeout'
/Users/tellingsen/.gem/ruby/1.8/gems/selenium-client-1.2.18/lib/selenium/client/protocol.rb:11:in `remote_control_command'
/Users/tellingsen/.gem/ruby/1.8/gems/selenium-client-1.2.18/lib/selenium/client/protocol.rb:19:in `string_command'
/Users/tellingsen/.gem/ruby/1.8/gems/selenium-client-1.2.18/lib/selenium/client/base.rb:85:in `start_new_browser_session'
google_test.rb:21:in `setup'
1 tests, 0 assertions, 0 failures, 1 errors
Can someone help me with this?
Note:
Mac OS: 10.6.4
Macbook Pro
Ruby: 1.8.7
gem: selenium-client 1.2.18
EDIT
Here is the google_test.rb that I tried
#!/usr/bin/env ruby
#
# Sample Test:Unit based test case using the selenium-client API
#
require "test/unit"
require "rubygems"
gem "selenium-client", ">=1.2.18"
require "selenium/client"
class ExampleTest < Test::Unit::TestCase
attr_reader :browser
def setup
#browser = Selenium::Client::Driver.new \
:host => "localhost",
:port => 4444,
:browser => "*firefox",
:url => "http://www.google.com",
:timeout_in_second => 60
browser.start_new_browser_session
end
def teardown
browser.close_current_browser_session
end
def test_page_search
browser.open "/"
assert_equal "Google", browser.title
browser.type "q", "Selenium seleniumhq"
browser.click "btnG", :wait_for => :page
assert_equal "Selenium seleniumhq - Google Search", browser.title
assert_equal "Selenium seleniumhq", browser.field("q")
assert browser.text?("seleniumhq.org")
assert browser.element?("link=Cached")
end
end
I figured it out after a few hours of searching on forums and through google.
What I needed to do was have the selenium server running for it to work. I was able to download it from this site http://seleniumhq.org/download/ (current: Selenium RC February 23, 2010 1.0.3).
From there I opened up a new terminal and did
cd Downloads/selenium-remote-control-1.0.3/selenium-server-1.0.3
java -jar selenium-server.jar
Then ran my ruby generated script with another terminal window
ruby google_test.rb
And it worked!
This is Selenium Webdriver example for simple google search
Save as google_search.rb
require "selenium-webdriver"
require "test/unit"
class GoogleSearch < Test::Unit::TestCase
def setup
#driver = Selenium::WebDriver.for :firefox
#base_url = "http://www.google.com/"
#driver.manage.timeouts.implicit_wait = 30
#verification_errors = []
end
def teardown
#driver.quit
assert_equal [], #verification_errors
end
def test_google_search
#driver.get(#base_url)
#driver.find_element(:name, "q").clear
#driver.find_element(:name, "q").send_keys "Thiyagarajan Veluchamy"
#driver.find_element(:name, "btnK").click
end
def element_present?(how, what)
#driver.find_element(how, what)
true
rescue Selenium::WebDriver::Error::NoSuchElementError
false
end
def verify(&blk)
yield
rescue Test::Unit::AssertionFailedError => ex
#verification_errors << ex
end
end
$ruby google_search.rb
Here is a much simpler version of the script:
require "selenium-webdriver"
#driver = Selenium::WebDriver.for :chrome
#base_url = "http://www.google.com/"
#driver.get(#base_url)
#driver.find_element(:name, "q").send_keys "Stack Overflow"
Methods available on the #driver object can be found here: http://selenium.googlecode.com/svn/trunk/docs/api/rb/Selenium/WebDriver/Driver.html
find_element gives you access to the Element class. Methods available on the Element class can be found here:
http://selenium.googlecode.com/svn/trunk/docs/api/rb/Selenium/WebDriver/Element.html

Selenium Ruby Reporting

I'm trying to set the environment for testing using Selenium and selenium-client gem.
I prefer unit test style over RSpec style of tests.
Do I have to build my own system for reporting then?
How can I add exception handling without having begin-rescue-end in each test? Is there any way to do that using mixins?
I'm not sure I understand what your question means in terms of reporting but the selenium-client gem handles both BDD and UnitTesting.
Below is code copied from the rubyforge page:
require "test/unit"
require "rubygems"
gem "selenium-client", ">=1.2.16"
require "selenium/client"
class ExampleTest < Test::Unit::TestCase
attr_reader :browser
def setup
#browser = Selenium::Client::Driver.new \
:host => "localhost",
:port => 4444,
:browser => "*firefox",
:url => "http://www.google.com",
:timeout_in_second => 60
browser.start_new_browser_session
end
def teardown
browser.close_current_browser_session
end
def test_page_search
browser.open "/"
assert_equal "Google", browser.title
browser.type "q", "Selenium seleniumhq"
browser.click "btnG", :wait_for => :page
assert_equal "Selenium seleniumhq - Google Search", browser.title
assert_equal "Selenium seleniumhq", browser.field("q")
assert browser.text?("seleniumhq.org")
assert browser.element?("link=Cached")
end
end
As for exception handling, UnitTesting handles the exceptions with an Error message.
That being said, I may have misunderstood your question.
Initial build of Extent is available for Ruby. You can view the sample here. Latest source is available at github.
Sample code:
# main extent instance
extent = RelevantCodes::ExtentReports.new('extent_ruby.html')
# extent-test
extent_test = extent.start_test('First', 'description string')
# logs
extent_test.log(:pass, 'step', 'details')
extent.end_test(extent_test)
# flush to write everything to html file
extent.flush

Resources