将多个测试类映射到同一文件以进行自动测试
我在我的项目中使用 独立自动测试 以及 minitest。在我的一个项目中,我有一个文件 (validation.rb
),用于根据不同的内部格式级别验证文档。 (2 级文档比 1 级文档具有更多功能。)
测试特定级别的验证需要重复加载已知有效的文档,以损坏的方式巧妙地对其进行变异,然后确保它被损坏。简而言之:
class TestValidation < MiniTest::Unit::TestCase
def setup
@l1 = Document.load( L1DOC )
end
def test_valid
assert @l1.valid_level_1?
end
def test_unbalanced_data
@l1.instance_eval{ @tracks[0].data.pop }
refute @l1.valid_level_1?, "Validation must ensure that all tracks have the same amount of data"
end
# many more tests for level 1 here
end
问题是自动测试(据我所知)知道根据测试类的名称运行哪些测试。当 validation.rb
更改时,TestValidation
将自动运行其测试。
如果没有自动测试,我会将上面的类命名为 TestL1Validation
,并创建一个新的类 TestL2Validation
来加载不同的文档。但是,这样做会破坏自动测试,除非我将验证分解为 l1validation.rb
和 l2validation.rb
。
如何命名我的文件或测试,或者设置我的测试,以便在单个源文件更改时自动测试将运行多个测试类?
I'm using standalone autotest in my projects along with minitest. In one of my projects, I have a single file (validation.rb
) that validates a document to different internal format levels. (A Level 2 document has more features than a Level 1 document.)
Testing the validation for a particular level requires repeatedly loading in a known-valid document, subtly mutating it in broken way, and then ensuring that it is broken. In short:
class TestValidation < MiniTest::Unit::TestCase
def setup
@l1 = Document.load( L1DOC )
end
def test_valid
assert @l1.valid_level_1?
end
def test_unbalanced_data
@l1.instance_eval{ @tracks[0].data.pop }
refute @l1.valid_level_1?, "Validation must ensure that all tracks have the same amount of data"
end
# many more tests for level 1 here
end
The problem is that autotest (as far as I can tell) knows which tests to run based on the name of the test classes. TestValidation
will have its tests automatically run when validation.rb
is changed.
Without autotest, I would have named the above class TestL1Validation
, and created a new class TestL2Validation
that loaded a different document. Doing this breaks autotest, however, unless I break out my validation into l1validation.rb
and l2validation.rb
.
How can I name my files or tests, or set up my tests, so that autotest will run multiple test classes when a single source file changes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以添加自定义映射以进行自动测试。这是一种方法:
创建与 lib 和 test 目录同一级别的 autotest 目录。
添加 autotest/discover.rb:
添加 autotest/my_rules.rb:
这将添加自定义映射:每当 lib/valid.rb 文件发生更改时,重新运行测试目录中的所有 test_*.rb 文件。
You can add custom mappings for autotest. Here's one way of doing it:
Create autotest directory at the same level as lib and test directories.
Add autotest/discover.rb:
Add autotest/my_rules.rb:
This will add a custom mapping: whenever lib/valid.rb files has changed, re-run all test_*.rb files in test directory.