0

多くのテストケースで使用したいクラスがあります。

require 'rubygems'
require 'test/unit'
require 'watir'

class Tests < Test::Unit::TestCase
  def self.Run(browser)
    #  make sure Summary of Changes exists
    assert( browser.table(:class, "summary_table_class").exists? )
    # make sure Snapshot of Change Areas exists
    assert( browser.image(:xpath, "//div[@id='report_chart_div']/img").exists?  )
    # make sure Integrated Changes table exists
    assert( browser.table(:id, 'change_table_html').exists? )
  end
end

ただし、私のテストケースの1つで実行すると、次のようになります。

require 'rubygems'
require 'test/unit'
require 'watir'
require 'configuration'
require 'Tests'

class TwoSCMCrossBranch < Test::Unit::TestCase
  def test_two_scm_cross_branch
    test_site = Constants.whatsInUrl
    puts " Step 1: go to the test site: " + test_site
    ie = Watir::IE.start(test_site)

    Tests.Run(ie)

  end
end

エラーが発生します:

NoMethodError: undefined method `assert' for Tests:Class
    C:/p4/dev/webToolKit/test/webapps/WhatsIn/ruby-tests/Tests.rb:8:in `Run'

何が欠けていますか?ありがとう!

4

2 に答える 2

3

assert()はTestCaseのインスタンスメソッドであるため、テストのインスタンスでのみ使用できます。あなたはそれをクラスメソッド内で呼び出しているので、Rubyは存在しないテストでクラスメソッドを探しています。

これを行うためのより良い方法は、Testsをモジュールにし、Runメソッドをインスタンスメソッドにすることです。

module Tests
  def Run(browser)
    ...
  end
end

次に、テストモジュールをテストクラスに含めます。

class TwoSCMCrossBranch < Test::Unit::TestCase
  include Tests

  def test_two_scm_cross_branch
    test_site = Constants.whatsInUrl
    puts " Step 1: go to the test site: " + test_site
    ie = Watir::IE.start(test_site)

    Run(ie)
  end
end

これにより、Runメソッドがテストで使用可能になり、Run()はテストクラスでassert()メソッドを検索します。

于 2011-01-25T17:16:50.573 に答える
1

assertsすべてを一緒に削除して、を使用するだけの価値があるかもしれません.exists?

于 2011-01-25T16:49:29.657 に答える