I am new into watir and I am using testunit for assertion. My script looks like this: Script1 -- has a test method which calls Script2 Script2 -- does all the work and validation. This has all assertion

When i run my test case I have to run Script1, it runs successfully but result shows 1 tests, 0 assertions, 0 failures, 0 errors, 0 skips.

Here is my code:

This is in my first file

require_relative 'RubyDriver'
require 'test/unit'
class RubyTest < Test::Unit::TestCase
def test_method
    driver = RubyDriver.new("/home/pratik/study/UIAutomation/WatirScript.xlsx")
    driver.call_driver
end
   end

And this is part of anotner file

require_relative 'ExcelReader'
require_relative 'RubyUtility'
require "watir-webdriver"
require 'test/unit'

class RubyDriver < Test::Unit::TestCase
def take_action
value_property = @rubyutil.get_property("#{value}")
if value_property
    value_prop = value_property.first.strip
    value_value = value_property.last.strip
end

case "#{@rubyutil.get_string_upcase("#{keyword}")}"

when "VERIFY"
    puts "verifying"
    puts value_prop
    puts value_value
    object.wait_until_present
    object.flash    
    if value_prop == "text"
        assert_equal(object.text, value_value,"Text does not match")
        # puts object.text.should == value_value
    elsif value_prop == "exist"
        value_boolean = value_value == "true" ? true : false
        assert_equal(object.exists?,value_boolean,"Object does not exist")
        # puts object.exists?.should == value_value
    end

Everything is working fine except report which shows as

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

Where is my number of assertions.

Any help please.

有帮助吗?

解决方案

The problem is that you are calling your assertions within an instance of another class. If I recall correctly, assert increments the assertion count within its class instance. Therefore your assertion count is being incremented in your RubyDriver instance rather than the RubyTest instance. As a result, you get no assertions reported.

You need to do the assertions within the actual test case (ie test_method of RubyTest) that is being run by test/unit.

As an example, you could make RubyDriver include your driving logic and logic for retrieving values to test. RubyTest would then call RubyDriver to setup/get values and include your test logic.

class RubyTest < Test::Unit::TestCase
  def test_method
    # Do some setup using RubyDriver instance
    driver = RubyDriver.new("/home/pratik/study/UIAutomation/WatirScript.xlsx")
    driver.call_driver

    # Use RubyDriver to get some value you want to test
    some_value_to_test = driver.get_value_to_test

    # Do the assertion of the value within RubyTest
    assert_equal(true, some_value_to_test, "Object does not exist")
  end
end

An alternative solution might be to pass the test case (RubyTest) to RubyDriver. Then have RubyDriver call the assertion methods using the RubyTest instance.

Here is a simplified working example where you can see that your assertion count is correctly updated. Note that the RubyTest instance is passed to RubyDriver and stored in the @testcase variable. All assertions are then run with the context of the @testcase - eg @testcase.assert(false), which ensures that the original test cases' assertion counts are updated.

require 'test/unit'

class RubyDriver < Test::Unit::TestCase
    def initialize(file, testcase)
        @testcase = testcase
        super(file)     
    end

    def action
        @testcase.assert(false)
    end

end

class RubyTest < Test::Unit::TestCase
    def test_method
        driver = RubyDriver.new("/home/pratik/study/UIAutomation/WatirScript.xlsx", self)
        driver.action
    end
end

I left the RubyDriver as a sub-class of Test::Unit::TestCase, though it seems a bit odd unless you also have actual tests in the RubyDriver.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top