开发者

Mapping multiple test classes to the same file for autotest

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 docume开发者_运维知识库nt 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?


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:

$LOAD_PATH.unshift File.expand_path('../', File.dirname(__FILE__))
Autotest.add_discovery { "my_rules" }

Add autotest/my_rules.rb:

require 'autotest'

class Autotest::My_rules < Autotest
  def initialize
    super

    add_mapping(%r%^lib/valid.rb$%, true) { |filename, _|
      files_matching %r%^test/test_.*\.rb$%
    }
  end
end

This will add a custom mapping: whenever lib/valid.rb files has changed, re-run all test_*.rb files in test directory.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜