0

给定以下代码:

require 'minitest/autorun'
require '<some other stuff>

class test <  MiniTest::Unit::TestCase
  def setup
<setup some stuff>
  end
  def teardown
     <teardown some stuff>
  end

  def test1
    <test1 code>
  end
  def test2
    <test2 code>
  end
end

如何使 minitest 使用初始设置同时运行 test1 和 test2?我问的原因是因为 setup 实例化了一个 Selenium Webdriver 实例并进行了一些耗时的登录/设置,并且我想使用相同的实例(而不是每次都实例化一个新实例)来缩短时间测试功能。

4

4 回答 4

3

在这个问题中有很多假设,我将尝试解决。首先,Minitest 不会并行运行测试。默认情况下,Minitest 以随机顺序运行测试。test_order您可以通过覆盖测试类上的方法来更改测试的运行顺序。

class MyTest <  MiniTest::Unit::TestCase
  def self.test_order
    :sorted # Or :alpha, they both behave the same
  end
end

但是,这不会让你达到我认为你所要求的。这只会使它test1总是在之前运行test2。这仅在您编写了需要按顺序运行的测试时才有用。(字母顺序,不一定是它们在类中定义的顺序。)

测试类中的实例变量不在测试之间共享。意思是,运行在与test1运行不同的实例上。为了在测试之间共享对象,您需要在全局或类变量中设置它们。MyTesttest2

class MyTest <  MiniTest::Unit::TestCase
  def setup
    @@this_thing ||= begin
      # Really expensive operation here
    end
  end
end

希望这可以帮助。

于 2013-09-10T19:47:27.060 回答
0

从测试的角度来看,这是个坏主意,但是

您可以在一个测试中进行两项测试并重复使用设置

或者将其从设置和拆卸中取出,并添加一个辅助方法来设置它(如果还没有的话)并从测试中调用它。运行的第一个测试会受到打击,其他的只是重用它。

但是,您应该做的是单元测试中的模拟或存根,并将真正的交易留给集成测试。

于 2013-08-21T16:39:39.647 回答
0

您可以通过创建具有 before 和 after all 测试方法的自定义测试运行器类型来做您想做的事情。然后,您可以在所有测试之前创建一个 selenium-webdriver 实例,并在所有测试之后关闭它。

这是在所有测试之前启动浏览器并转到 Google 的示例。然后每个测试重复使用相同的浏览器。

require 'minitest/autorun'
require 'selenium-webdriver'

#Add before and after suite hooks using a custom runner
class MyMiniTest
  class Unit < MiniTest::Unit
    def _run_suite(suite, type)
      begin
        suite.before_suite if suite.respond_to?(:before_suite)
        super(suite, type)
      ensure
        suite.after_suite if suite.respond_to?(:after_suite)
      end
    end
  end
end
MiniTest::Unit.runner = MyMiniTest::Unit.new

class GoogleTest < MiniTest::Unit::TestCase
  def self.before_suite
    p "before all tests"
    @@driver = Selenium::WebDriver.for :firefox
    @@driver.navigate.to 'http://www.google.com'
  end

  def self.after_suite
    p "after all tests"
    @@driver.quit
  end

  def setup
    p 'setup before each test'
  end

  def teardown
    p 'teardown after each test'
  end

  def test1
    p 'test1'
    assert_equal(0, @@driver.find_elements(:xpath, '//div').length)
  end

  def test2
    p 'test2'
    assert_equal(0, @@driver.find_elements(:xpath, '//a').length)
  end
end

您可以看到输出运行的顺序:

"before all tests"
"setup before each test"
"test1"
"teardown after each test"
"setup before each test"
"test2"
"teardown after each test"
"after all tests"

请注意,您要跨测试共享的变量需要是类变量(即以@@ 为前缀)。

于 2013-08-21T16:31:51.547 回答
0

您需要 Minitest,还是可以更改为测试单元?

使用 test-unit 你可以使用'Test::Unit::TestCase.startup andTest::Unit::TestCase.shutdown':

gem 'test-unit'#, '>= 2.1.1' #startup
require 'test/unit'
#~ require '<some other stuff>

class MyTest <  Test::Unit::TestCase
  def self.startup
    puts '<setup some stuff>'
  end
  def self.shutdown
     puts '<teardown some stuff>'
  end

  def test1
    puts '<test1 code>'
  end
  def test2
    puts '<test2 code>'
  end
end

另请参阅can't get test unit startup to work in ruby​​ 1.9.2

于 2013-08-21T18:44:38.953 回答