将多个测试类映射到同一文件以进行自动测试

发布于 2024-10-08 05:41:12 字数 1098 浏览 3 评论 0原文

我在我的项目中使用 独立自动测试 以及 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.rbl2validation.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 技术交流群。

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

发布评论

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

评论(1

喵星人汪星人 2024-10-15 05:41:12

您可以添加自定义映射以进行自动测试。这是一种方法:
创建与 lib 和 test 目录同一级别的 autotest 目录。

添加 autotest/discover.rb:

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

添加 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

这将添加自定义映射:每当 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:

$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.

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