如何使用 Test::Unit 跳过某些测试

发布于 2024-09-03 03:49:50 字数 368 浏览 6 评论 0原文

在我的一个项目中,我需要与多个后端系统协作。其中一些缺乏文档,部分原因是我有一些测试代码与一些测试服务器交互,只是为了看看一切都按预期工作。但是,访问这些服务器非常慢,因此我不想每次运行测试套件时都运行这些测试。

我的问题是如何处理您想跳过某些测试的情况。目前,我使用环境变量“BACKEND_TEST”和条件语句,该条件语句检查是否为我想跳过的每个测试设置了该变量。但有时我想跳过测试文件中的所有测试,而不必在每个测试的开头添加额外的行。

需要与测试服务器交互的测试并不多,因为我在其他情况下使用了flexmock。然而,你不能嘲笑自己脱离现实。

正如您从这个问题的标题中看到的,我正在使用 Test::Unit。此外,如果有什么区别的话,该项目是一个 Rails 项目。

In one of my projects I need to collaborate with several backend systems. Some of them somewhat lacks in documentation, and partly therefore I have some test code that interact with some test servers just to see everything works as expected. However, accessing these servers is quite slow, and therefore I do not want to run these tests every time I run my test suite.

My question is how to deal with a situation where you want to skip certain tests. Currently I use an environment variable 'BACKEND_TEST' and a conditional statement which checks if the variable is set for each test I would like to skip. But sometimes I would like to skip all tests in a test file without having to add an extra row to the beginning of each test.

The tests which have to interact with the test servers are not many, as I use flexmock in other situations. However, you can't mock yourself away from reality.

As you can see from this question's title, I'm using Test::Unit. Additionally, if it makes any difference, the project is a Rails project.

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

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

发布评论

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

评论(3

恋竹姑娘 2024-09-10 03:49:50

上一个答案中提到的功能包括 omit() 方法和 omit_if()

def test_omission
  omit('Reason')
  # Not reached here
end

以及

def test_omission
  omit_if("".empty?)
  # Not reached here
end

来自:http://test-unit.rubyforge.org/test-unit/en/Test/Unit/TestCaseOmissionSupport.html#omit-instance_method

The features referred to in the previous answer include the omit() method and omit_if()

def test_omission
  omit('Reason')
  # Not reached here
end

And

def test_omission
  omit_if("".empty?)
  # Not reached here
end

From: http://test-unit.rubyforge.org/test-unit/en/Test/Unit/TestCaseOmissionSupport.html#omit-instance_method

执妄 2024-09-10 03:49:50

测试单元 2.x 的新功能建议test-unit 2.x(gem 版本,而不是 ruby​​ 1.8 标准库)允许您省略测试。

New Features Of Test Unit 2.x suggests that test-unit 2.x (the gem version, not the ruby 1.8 standard library) allows you to omit tests.

说谎友 2024-09-10 03:49:50

我对以下内容感到困惑,这仍然会向控制台引发错误:

def test_omission
  omit('Reason')
  # Not reached here
end

您可以通过将要跳过的代码包装在传递给 omit 的块中来避免这种情况:

def test_omission
  omit 'Reason' do
    # Not reached here
  end
end

这实际上会按预期跳过测试,并输出“遗漏:测试原因”到控制台。不幸的是,您必须缩进现有代码才能使这项工作正常进行,我很高兴了解更好的方法来做到这一点,但这确实有效。

I was confused by the following, which still raises an error to the console:

def test_omission
  omit('Reason')
  # Not reached here
end

You can avoid that by wrapping the code to skip in a block passed to omit:

def test_omission
  omit 'Reason' do
    # Not reached here
  end
end

That actually skips the test as expected, and outputs "Omission: Test Reason" to the console. It's unfortunate that you have to indent existing code to make this work, and I'd be happy to learn of a better way to do it, but this works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文