如何在 ruby​​ 模块中包含单元测试?

发布于 2024-08-13 06:31:14 字数 603 浏览 8 评论 0 原文

我试图将模块的单元测试包含在与模块本身相同的源文件中,遵循 Perl modulino 模型。

#! /usr/bin/env ruby

require 'test/unit'

module Modulino
    def modulino_function
        return 0
    end
end

class ModulinoTest < Test::Unit::TestCase
    include Modulino
    def test_modulino_function
        assert_equal(0, modulino_function)
    end
end

现在,我可以运行执行此源文件的单元测试。

但是,当我从另一个脚本请求/加载它们时,它们也会运行。如何避免这种情况?

是否有更惯用的方法可以使用 Ruby 实现此目的,除非不鼓励这种做法?

I'm trying to include the unit tests for a module in the same source file as the module itself, following the Perl modulino model.

#! /usr/bin/env ruby

require 'test/unit'

module Modulino
    def modulino_function
        return 0
    end
end

class ModulinoTest < Test::Unit::TestCase
    include Modulino
    def test_modulino_function
        assert_equal(0, modulino_function)
    end
end

Now, I can run the unit-tests executing this source file.

But, they are also run when I require/load them from another script. How can this be avoided ?

Is there a more idiomatic way to achieve this with Ruby, unless this practice is discouraged ?

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

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

发布评论

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

评论(4

财迷小姐 2024-08-20 06:31:14

就我个人而言,我从未听说过有人尝试用 Ruby 来做到这一点。这绝对不是标准做法。也就是说,您也许可以利用这个技巧:

if __FILE__ == $0
  # Do something.. run tests, call a method, etc. We're direct.
end

只有直接执行文件时,if 块中的代码才会执行​​,而其他库或应用程序需要时则不会执行。

更多 ruby​​ 技巧在这里: http://www.rubyinside.com/21-ruby-技巧-902.html

Personally I've never heard of anyone trying to do this in Ruby. It's definitely not a standard practice. That said you may be able to leverage this trick:

if __FILE__ == $0
  # Do something.. run tests, call a method, etc. We're direct.
end

The code in the if block will only execute if the file is executed directly, not if it's required by another library or application.

More ruby tricks here: http://www.rubyinside.com/21-ruby-tricks-902.html

完美的未来在梦里 2024-08-20 06:31:14

实际上,这在 Ruby 中并不少见,尽管这在 Rails 中肯定不是常见的做法。

您可能遇到的问题之一与 这篇文章表明模块确实应该包含在类中以便测试它们。当然可以通过将模块包含在测试用例中来测试模块,但是您正在测试该模块在混合到 Test::Unit::TestCase 中时是否可以工作,而不是在将其混合到更有用的东西中时它会工作。

因此,单元测试可能应该存在于类文件中,或者如果您只想使用通用方法,请使用类函数而不是模块。

It's actually not that uncommon in Ruby though it's certainly not the common practice in Rails.

One of the issues you may be running into is the same as this post which is that modules really should be included in classes in order to test them. It's certainly possible to test a module by including it in your test case but you're testing whether the module works when mixed into Test::Unit::TestCase, not that it'll work when you mix it into something more useful.

So unit tests should probably live on the class file or if you just want generally available methods use a class function instead of a module.

牵强ㄟ 2024-08-20 06:31:14

您可以使用 minitest 在模块源代码本身中包含单元测试。

试试这个例子:

class Foo < String
end

if $0 == __FILE__
    require 'minitest/autorun'
    require 'minitest/pride'

    class FooTest < MiniTest::Unit::TestCase
        def test_foo_instantiation
            foo = Foo.new()
            assert_instance_of Foo, foo
        end

        def test_foo_parent_class
            foo = Foo.new()
            assert_kind_of String, foo
        end
    end
end

这里我创建了一个名为 Foo 的类,它继承自 String 类。
然后我创建了两个单元测试。在第一个测试中,我检查是否可以实例化
Foo 类的对象。在第二个测试中,我检查 Foo 类的实例化对象是否是一种 String。

如果此代码编写在名为 foo.rb 的文件中,我可以简单地使用以下命令运行测试:

ruby foo.rb

Minitest 执行速度很快。 “pride”模块允许您以彩色字体输出测试结果,非常养眼。

You can include unit tests inside the module source code itself using minitest.

Try this example:

class Foo < String
end

if $0 == __FILE__
    require 'minitest/autorun'
    require 'minitest/pride'

    class FooTest < MiniTest::Unit::TestCase
        def test_foo_instantiation
            foo = Foo.new()
            assert_instance_of Foo, foo
        end

        def test_foo_parent_class
            foo = Foo.new()
            assert_kind_of String, foo
        end
    end
end

Here I created a class called Foo, which inherits from the class String.
Then I created two unit tests. In the first test, I check that I can instantiate
an object of class Foo. In the second test, I check that the instantiated object of class Foo is a kind of String.

If this code is written in a file called foo.rb, I can simply run the tests using this command:

ruby foo.rb

Minitest is fast to execute. The "pride" module allows you to output the test result in color fonts, which is nice on the eye.

半夏半凉 2024-08-20 06:31:14

刚刚找到一种方法来防止在脚本需要模块时执行单元测试。 .../lib/ruby/1.8/test/ 中的 unit.rb 中有一个标志设置为 true。

结合 samg 技巧(再次感谢),我们可以写:

if (__FILE__ != $0)
    Test::Unit.run = true  ### do not run the unit tests
end

Just found one way to prevent the unit test from being executed when the module is required from a script. There is a flag in unit.rb in .../lib/ruby/1.8/test/ to set to true.

Combined with samg trick (thanks again), we can write:

if (__FILE__ != $0)
    Test::Unit.run = true  ### do not run the unit tests
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文