Question

I am just starting to figure how to create unit tests using "test/unit". I copied the code generated by Selenium IDE and paste it into my Ruby test method.

But when running it with Ruby.exe, for some reason it is throwing an error:

Finished tests in 31.835891s, 0.0314 tests/s, 0.0942 assertions/s.

  1) Error:
test_method(MyTestClass):
NameError: uninitialized constant Test::Unit::AssertionFailedError
    teste-noticia.rb:30:in `rescue in verify'
    teste-noticia.rb:29:in `verify'
    teste-noticia.rb:42:in `test_method'

1 tests, 3 assertions, 0 failures, 1 errors, 0 skips

Anyone could help me to how assert correctly desired strings? Any good practice is welcome ;-).

Here is the code:

# encoding: utf-8
require "selenium-webdriver"
require "test/unit"

class MyTestClass  < Test::Unit::TestCase

  def setup
      @driver = Selenium::WebDriver.for :firefox
      @base_url = "http://www.yoursite.com"
      @driver.manage.timeouts.implicit_wait = 30
      @verification_errors = []
      @wait = Selenium::WebDriver::Wait.new :timeout => 10
    end


    def teardown
      @driver.quit
      assert_equal [], @verification_errors
    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

    #your test methods go here
    def test_method
        @driver.get(@base_url + "/my-desired-path")

        verify { assert_equal "Obama wins and will move U.S. forward", @driver.find_element(:css, "h1").text }    

    end
end

EDIT

My local gems:

C:\Users\wmj>gem list

*** LOCAL GEMS ***

addressable (2.3.2)
bigdecimal (1.1.0)
childprocess (0.3.6)
ffi (1.1.5 x86-mingw32)
io-console (0.3)
json (1.5.4)
libwebsocket (0.1.5)
minitest (2.5.1)
multi_json (1.3.7)
rake (0.9.2.2)
rdoc (3.9.4)
rubyzip (0.9.9)
selenium-webdriver (2.26.0)
test-unit (2.5.2)
Was it helpful?

Solution

I believe the issue is that you have required the 'minitest' gem, but are trying to use the classes in the 'test-unit' gem. 'Minitest' is installed by default in Ruby 1.9 instead of 'Test-Unit' (which was installed by default in 1.8). Minitest is only partially backwards compatible with Test-Unit.

Possible solutions:

Switch to Minitest:

It is the Test::Unit::AssertionFailedError in the verify method that is causing the exception. You could change it to the minitest equivalent, which appears to be MiniTest::Assertion. So your verify method would become:

def verify(&blk)
  yield
  rescue MiniTest::Assertion => ex
  @verification_errors << ex
end

Use Test-Unit instead of Minitest:

Assuming you have the test-unit gem already installed (gem install test-unit), manually specify that you want to use that gem when doing require 'test/unit':

gem "test-unit"
require "test/unit"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top