使用 Test::Unit,如何在所有测试(但不是每个测试)之前运行一些代码? [英] With Test::Unit, how can I run a bit of code before all tests (but not each test)?

查看:31
本文介绍了使用 Test::Unit,如何在所有测试(但不是每个测试)之前运行一些代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我使用 test::unit 的测试应用程序中,我需要首先从各种来源提取一堆数据.我只想这样做一次 - 数据只读取,不写入,并且在测试之间不会更改,加载(以及加载的错误检查)需要一些时间.

In my test app, which uses test::unit, I need to start by pulling a bunch of data from various sources. 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:

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 可以是(单例)类(各自的实例).

GLOBAL_DATA could be a (singleton)-class (respective an instance).

如果你只有一个测试类,你可以使用 TestCase.startup:

If you have only one testclass, you may use 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

这篇关于使用 Test::Unit,如何在所有测试(但不是每个测试)之前运行一些代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆