简体   繁体   English

如何使用Ruby的Test :: Unit只测试一个方法?

[英]How can I test only a method with Ruby's Test::Unit?

Imagine I have a test like this: 想象一下,我有这样的测试:

class MyUnitTest < Test::Unit::TestCase
  def test_first
    # test code here
  end

  def test_second
    # test code here
  end

  def test_third
    # test code here
  end
end

My test cases are destructive, and I need to regenerate the input in between tests. 我的测试用例是破坏性的,我需要在测试之间重新生成输入。 Therefore, it would be useful to run only one test case at a time. 因此,一次只运行一个测试用例会很有用。 Currently, my approach is to comment tests that I don't want executed, but surely there must be a better way? 目前,我的方法是评论我不想执行的测试,但肯定必须有更好的方法吗?

So, for example, how do I run only test_first when I execute my test? 那么,例如,在执行测试时如何仅运行test_first

Use --name PATTERN argument in order to filter out test names you want to run. 使用--name PATTERN参数可以过滤掉要运行的测试名称。

D:\Projects>ruby test.rb
Loaded suite test
Started
...
Finished in 0.000000 seconds.

3 tests, 0 assertions, 0 failures, 0 errors, 0 skips

Test run options: --seed 39768

D:\Projects>ruby test.rb -n test_first
Loaded suite test
Started
.
Finished in 0.000000 seconds.

1 tests, 0 assertions, 0 failures, 0 errors, 0 skips

Test run options: --seed 53891 --name "test_first"

Can you regenerate the input with ruby? 你能用ruby重新生成输入吗? Then you could use the setup-method. 然后你可以使用setup-method。

gem 'test-unit', '>= 2.1.1' #startup
require 'test/unit'

class Test_setup < Test::Unit::TestCase
  def setup
    #Your regeneration of the input
    puts "Setup"
  end

  def teardown
    #Clean actions
    puts "End"
  end

  def test_1()
    puts "Testing setup 1"
  end      
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Ruby的Test :: Unit :: TestCase中,如何覆盖initialize方法? - In Ruby's Test::Unit::TestCase, how do I override the initialize method? Ruby Test :: Unit的当前测试方法名称 - Ruby Test::Unit's current test method name 如何使用Ruby / Selenium / Test-Unit将库存导入Registroid POS - How can I import inventory into Registroid P.O.S. using Ruby / Selenium / Test-Unit 如何在Ruby中对超时异常的方法进行单元测试 - How to unit test a method for timeout exception in ruby 如何测试仅调用系统命令的ruby方法? - How would I test a ruby method that only calls a system command? 如何对一个在循环中使用stdin的方法进行单元测试? - How can I unit test a method that takes stdin within a loop? 如何使用 Ruby 测试/单元执行单个测试? - How do I execute a single test using Ruby test/unit? 在Ruby的Test :: Unit :: TestCase中,是否有一种方法可以让测试方法访问即将测试的方法总数? - In Ruby's Test::Unit::TestCase, is there a way for a test method to access the total number of methods about to be tested? 我如何获得RSpec的共享示例,例如Ruby Test :: Unit中的行为? - How do i get RSpec's shared examples like behavior in Ruby Test::Unit? 在Ruby中,如何控制Test :: Unit测试的运行顺序? - In Ruby, how to I control the order in which Test::Unit tests are run?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM