我正在尝试为我的 ruby 脚本编写一些单元测试。但是,它并没有像我认为的那样工作,并且在某些测试中,它只是在单元测试的中途停止。
这是我目前正在测试的方法。
#!/usr/bin/env ruby
require 'ptools'
require 'test/unit'
class InputValidators
# checks whether the input file exist.
def input_file_validator(input_file)
begin
raise ArgumentError, "Error: Input file \"#{input_file}\" does not exist. \n" unless File.exist?(input_file)
raise ArgumentError, "Error: Input file is empty. Please correct this and try again. \n" if File.zero?(input_file)
raise ArgumentError, "Error: Input file is in binary format - only text based input files are supported. \n" if File.binary?(input_file)
rescue Exception => e
puts # a empty line
puts e.message
puts # a empty line
Process.exit(true)
end
end
end
class UnitTests < Test::Unit::TestCase
def test_input_file_validator_1
test_validators = InputValidators.new
assert_equal(nil, test_validators.input_file_validator("./test_inputs/genetic.fna")) #file is present
assert_raise( SystemExit ) {test_validators.input_file_validator("./test_inputs/missing_input.fna")} # file doesn't exist
# assert_equal(nil, test_validators.input_file_validator("./test_inputs/empty_file.fna")) # empty file
# assert_equal(nil, test_validators.input_file_validator("./test_inputs/binary_file.fna")) # a binary file
end
end
现在,如果我按上述方式保留脚本,则单元测试可以完美运行......
电流输出:
Run options:
# Running tests:
[1/1] UnitTests#test_input_file_validator_1
Error: Input file "./test_inputs/missing_input.fna" does not exist.
Finished tests in 0.004222s, 236.8797 tests/s, 473.7593 assertions/s.
1 tests, 2 assertions, 0 failures, 0 errors, 0 skips
ruby -v: ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-linux]
但是,如果我什至取消注释其他断言之一,单元测试就会停止并且不会完成。
输出(取消注释上述脚本中的一个或两个断言时):
Run options:
# Running tests:
[1/1] UnitTests#test_input_file_validator_1
Error: Input file "./test_inputs/missing_input.fna" does not exist.
Error: Input file is empty. Please correct this and try again.
我不知道我做错了什么,所以对此的任何帮助将不胜感激。如果您需要更多信息,请告诉我。