0

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.

4

1 回答 1

1

问题是您在另一个类的实例中调用您的断言。如果我没记错的话,assert 会增加其类实例中的断言计数。因此,您的 RubyDriver 实例而不是 RubyTest 实例中的断言计数正在增加。因此,您不会收到任何断言报告。

您需要在测试/单元运行的实际测试用例(即test_methodRubyTest)中进行断言。

例如,您可以让 RubyDriver 包含您的驱动逻辑和用于检索要测试的值的逻辑。然后 RubyTest 将调用 RubyDriver 来设置/获取值并包含您的测试逻辑。

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

另一种解决方案可能是将测试用例 (RubyTest) 传递给 RubyDriver。然后让 RubyDriver 使用 RubyTest 实例调用断言方法。

这是一个简化的工作示例,您可以在其中看到您的断言计数已正确更新。请注意,RubyTest 实例被传递给 RubyDriver 并存储在 @testcase 变量中。然后所有断言都在 @testcase 的上下文中运行 - 例如@testcase.assert(false),这确保了原始测试用例的断言计数被更新。

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

我将 RubyDriver 保留为 Test::Unit::TestCase 的子类,尽管这似乎有点奇怪,除非您在 RubyDriver 中也有实际测试。

于 2013-10-15T11:18:45.733 回答