Ruby Minitest:套件级别还是类级别设置?

发布于 2024-11-25 04:30:17 字数 314 浏览 1 评论 0原文

使用内置的 Ruby Minitest 框架,有没有办法在整个套件运行之前运行一些代码,甚至在整个 TestClass 运行之前运行一次?我在 这个问题的答案中看到 Test::Unit: :after_tests 可用于在运行所有测试后运行代码;在所有测试运行之前是否有类似的方法来运行代码?

我想使用此功能在测试运行之前初始化测试数据库,并在测试全部运行后将其拆除。

谢谢!

Using the built-in Ruby Minitest framework, is there a way to run some code once before the entire suite runs, or even once before an entire TestClass runs? I see in the answer to this question that Test::Unit::after_tests can be used to run code after all tests have been run; is there a similar method to run code before all tests have run?

I would like to use this functionality to initialize a test database before the tests run and tear it down after they have all run.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放我走吧 2024-12-02 04:30:17

这是从 MiniTest docs(在可自定义测试运行器类型下)修改的。

class Burger
  def initialize
    puts "YOU CREATED A BURGER"
  end

  def has_cheese?
    true
  end

  def has_pickle?
    false
  end
end

gem 'minitest'

require 'minitest/unit'
MiniTest::Unit.autorun

class MyMiniTest
  class Unit < MiniTest::Unit

    def before_suites
      # code to run before the first test
      p "Before everything"
    end

    def after_suites
      # code to run after the last test
      p "After everything"
    end

    def _run_suites(suites, type)
      begin
        before_suites
        super(suites, type)
      ensure
        after_suites
      end
    end

    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 BurgerTest < MiniTest::Unit::TestCase

  def self.before_suite
    p "hi"
  end

  def self.after_suite
    p "bye"
  end

  def setup
    @burger = Burger.new
  end

  def test_has_cheese
    assert_equal true, @burger.has_cheese?
  end

  def test_has_pickle
    assert_equal false, @burger.has_pickle?
  end

end

请注意,我包含了 gem 'minitest' 来使用 gem,而不是没有 MiniTest::Unit.runner 方法的捆绑版本。这是输出。

Run options: --seed 49053

# Running tests:

"Before everything"
"hi"
YOU CREATED A BURGER
.YOU CREATED A BURGER
."bye"
"After everything"


Finished tests in 0.000662s, 3021.1480 tests/s, 3021.1480 assertions/s.

2 tests, 2 assertions, 0 failures, 0 errors, 0 skips

所以它调用 #setup 两次,但 .before_suite.after_suite 仅调用一次,我认为这就是您正在寻找的。

This is modified from the MiniTest docs (under Customizable Test Runner Types).

class Burger
  def initialize
    puts "YOU CREATED A BURGER"
  end

  def has_cheese?
    true
  end

  def has_pickle?
    false
  end
end

gem 'minitest'

require 'minitest/unit'
MiniTest::Unit.autorun

class MyMiniTest
  class Unit < MiniTest::Unit

    def before_suites
      # code to run before the first test
      p "Before everything"
    end

    def after_suites
      # code to run after the last test
      p "After everything"
    end

    def _run_suites(suites, type)
      begin
        before_suites
        super(suites, type)
      ensure
        after_suites
      end
    end

    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 BurgerTest < MiniTest::Unit::TestCase

  def self.before_suite
    p "hi"
  end

  def self.after_suite
    p "bye"
  end

  def setup
    @burger = Burger.new
  end

  def test_has_cheese
    assert_equal true, @burger.has_cheese?
  end

  def test_has_pickle
    assert_equal false, @burger.has_pickle?
  end

end

Note that you I included gem 'minitest' to use the gem instead of the bundled version which didn't have the MiniTest::Unit.runner method. Here's the output.

Run options: --seed 49053

# Running tests:

"Before everything"
"hi"
YOU CREATED A BURGER
.YOU CREATED A BURGER
."bye"
"After everything"


Finished tests in 0.000662s, 3021.1480 tests/s, 3021.1480 assertions/s.

2 tests, 2 assertions, 0 failures, 0 errors, 0 skips

So it calls #setup twice, but .before_suite and .after_suite only once, which is what you are looking for I think.

以往的大感动 2024-12-02 04:30:17

获取 MiniTest 套件中所有测试运行前后时间的另一种方法是将 if 块放在 setup & 中。 teardown 方法来控制这些块仅被调用一次。

通过这种方式,您可以在测试套件开始时加载一次浏览器和其他依赖项(例如页面对象),然后在所有测试完成后关闭浏览器。

以下是使用 MiniTest 5.5.1 和 Watir 的示例:

class CoolTests < Minitest::Test

  @@setupComplete  = false  # tracks whether 1-time setup has completed, so we only instantiate a browser and dependent pages/modules one time per suite run
  @@testsRun       = 0      # tracks how many tests have run so we can close the browser when all tests complete

  def setup                                                    # Minitest#setup runs before every #test method
    @@testsRun+=1                                              # increment tetsRun indicating that a test has run
    if (!@@setupComplete)                                      # we load the browser and necessary page objects here one-time if we haven't already
        @@driver = Watir::Browser.new :chrome                  # instantiate new chrome browser
        @@driver.window.maximize                               # maximize the browser window so we expect to test against Desktop UI/UX rather than Mobile UI/UX
        @@setupComplete = true                                 # setupComplete is now true as we've loaded up everything we need for our tests
    end
  end

  def teardown                                                 # Minitest#teardown runs after every #test method
    if (@@testsRun == CoolTests.runnable_methods.length)   # if we've run all the tests in the suite we are finished and can then close the browser
        @@driver.quit
    end
  end

  #Tests

  def test_one
    p __method__
    @@driver.goto('www.google.com')
    assert_equal 'Google', @@driver.title, 'browser should be at google.com'
  end

  def test_two
    p __method__
    @@driver.goto('www.bing.com')
    assert_equal 'Bing', @@driver.title, 'browser should be at bing.com'
  end

An alternative way to get a handle on the time before and after all the tests in a MiniTest suite have run is to put if blocks in the setup & teardown methods to control that these blocks only get called once.

In this way, you can load a browser and other dependencies such as page objects just once at the beginning of your test suite and then close the browser at the end when all tests have completed.

Here's an example of this using MiniTest 5.5.1 and Watir:

class CoolTests < Minitest::Test

  @@setupComplete  = false  # tracks whether 1-time setup has completed, so we only instantiate a browser and dependent pages/modules one time per suite run
  @@testsRun       = 0      # tracks how many tests have run so we can close the browser when all tests complete

  def setup                                                    # Minitest#setup runs before every #test method
    @@testsRun+=1                                              # increment tetsRun indicating that a test has run
    if (!@@setupComplete)                                      # we load the browser and necessary page objects here one-time if we haven't already
        @@driver = Watir::Browser.new :chrome                  # instantiate new chrome browser
        @@driver.window.maximize                               # maximize the browser window so we expect to test against Desktop UI/UX rather than Mobile UI/UX
        @@setupComplete = true                                 # setupComplete is now true as we've loaded up everything we need for our tests
    end
  end

  def teardown                                                 # Minitest#teardown runs after every #test method
    if (@@testsRun == CoolTests.runnable_methods.length)   # if we've run all the tests in the suite we are finished and can then close the browser
        @@driver.quit
    end
  end

  #Tests

  def test_one
    p __method__
    @@driver.goto('www.google.com')
    assert_equal 'Google', @@driver.title, 'browser should be at google.com'
  end

  def test_two
    p __method__
    @@driver.goto('www.bing.com')
    assert_equal 'Bing', @@driver.title, 'browser should be at bing.com'
  end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文