如何使用 Test::Unit 跳过某些测试
在我的一个项目中,我需要与多个后端系统协作。其中一些缺乏文档,部分原因是我有一些测试代码与一些测试服务器交互,只是为了看看一切都按预期工作。但是,访问这些服务器非常慢,因此我不想每次运行测试套件时都运行这些测试。
我的问题是如何处理您想跳过某些测试的情况。目前,我使用环境变量“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
上一个答案中提到的功能包括
omit()
方法和omit_if()
以及
来自: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 andomit_if()
And
From: http://test-unit.rubyforge.org/test-unit/en/Test/Unit/TestCaseOmissionSupport.html#omit-instance_method
测试单元 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.
我对以下内容感到困惑,这仍然会向控制台引发错误:
您可以通过将要跳过的代码包装在传递给
omit
的块中来避免这种情况:这实际上会按预期跳过测试,并输出“遗漏:测试原因”到控制台。不幸的是,您必须缩进现有代码才能使这项工作正常进行,我很高兴了解更好的方法来做到这一点,但这确实有效。
I was confused by the following, which still raises an error to the console:
You can avoid that by wrapping the code to skip in a block passed to
omit
: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.