简体   繁体   English

使用Test :: Unit,如何在所有测试之前运行一些代码(但不是每次测试)?

[英]With Test::Unit, how can I run a bit of code before all tests (but not each test)?

In my test app, which uses test::unit, I need to start by pulling a bunch of data from various sources. 在我的测试应用程序中,它使用了test :: unit,我需要从各种来源中提取大量数据。 I'd like to only do this once - the data is only read, not written, and doesn't change between tests, and the loading (and error checking for the loading), takes some time. 我只想这样做一次 - 数据只读取,不写入,并且在测试之间不会改变,加载(以及加载的错误检查)需要一些时间。

There are values that I DO want reset every time, and those are easy enough, but what if I want persistant accessible values? 我希望每次都重置一些值,这些值很容易,但是如果我想要持久的可访问值呢? What's the best way to do this? 最好的方法是什么?

I'm especially interested in solutions that would let my push those assignments to some module that can be included in all my tests, since they all need access to this data. 我特别感兴趣的是能够将这些分配推送到可以包含在我的所有测试中的某些模块的解决方案,因为它们都需要访问这些数据。

Why do you need it inside the test? 为什么在测试中需要它? You could define it gloabl: 你可以定义它gloabl:

gem 'test-unit'#, '>= 2.1.1' #startup
require 'test/unit'

GLOBAL_DATA = 11

class My_Tests < Test::Unit::TestCase

  def test_1()
    puts "Testing startup 1"
    assert_equal(11, GLOBAL_DATA)
  end
end

GLOBAL_DATA could be a (singleton)-class (respective an instance). GLOBAL_DATA可以是(单例)类(相应的实例)。

If you have only one testclass, you may use TestCase.startup: 如果您只有一个测试类,则可以使用TestCase.startup:

gem 'test-unit'#, '>= 2.1.1' #startup
require 'test/unit'


class My_Tests < Test::Unit::TestCase
  def self.startup
    puts "Define global_data "
    @@global_data = 11
  end

  def test_1()
    puts "Testing 1"
    assert_equal(11,     @@global_data = 11)
  end
  def test_2()
    puts "Testing 2"
    assert_equal(11,     @@global_data = 11)
  end
end

You can just put them at the top of the class. 你可以把它们放在班级的顶端。 They will get executed, and then your tests will get executed. 它们将被执行,然后您的测试将被执行。

You could do this in the setup method: 您可以在设置方法中执行此操作:

  def setup
    if !defined?(@@initial_data)
      # Whatever you need to do to get your initial data
      @@initial_data = foo
    end
    @other_data = bar
  end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 MiniTest 的所有测试中在每个测试用例之前运行代码? - How to run code before each test case in all tests in MiniTest? Ruby单元测试:在每次测试失败后运行一些代码 - Ruby unit tests: run some code after each failed test 我可以确保所有测试都包含测试/单元中的断言吗? - Can I ensure all tests contain an assertion in test/unit? 如何修复运行Test :: Unit测试时产生的警告 - How can I fix this warning produced when I run my Test::Unit tests 在Ruby中,如何控制Test :: Unit测试的运行顺序? - In Ruby, how to I control the order in which Test::Unit tests are run? Ruby on Rails2.3.8:单元测试:Rails / Ruby已设置为在每次测试之前运行。 在所有测试之前运行的方法呢? - Ruby on Rails2.3.8: Unit Testing: Rails/Ruby has setup to run before each test. What about a method that runs before all tests? 在代码中运行Ruby单元测试(Test :: Unit) - Run Ruby Unit Tests(Test::Unit) inside the Code Ruby:当我使用Test :: Unit运行测试时,为什么没有点显示? - Ruby: Why are no dots showing when I run tests with Test::Unit? 在Rails中,如何在特定的Rspec测试之前加载所有代码? - In Rails, how can I eager load all code before a specific Rspec test? 如何运行Test :: Unit套件以完成测试/失败计数? - How do I run a Test::Unit suite to completion with a tally of tests/failures?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM