gpt4 book ai didi

ruby - 添加某些断言时单元测试中途停止

转载 作者:太空宇宙 更新时间:2023-11-03 16:03:36 25 4
gpt4 key购买 nike

我正在尝试为我的 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.

我不知道我做错了什么,所以对此提供任何帮助将不胜感激。如果您需要更多信息,请告诉我。

最佳答案

好吧,如果您运行 exit 而您没有从该异常中解救出来,您的进程就会停止运行。

我猜想 assert_raise 确实捕获了那个错误或者做了一些其他的魔法来完成这个过程。运行 at_exit Hook 可能是其中的一些魔术。

尽管如此,对工作流使用异常被认为是一种不好的做法。所以我不建议提出错误然后立即捕获它只是为了退出该过程。我通常只是在消息中使用 abort

关于ruby - 添加某些断言时单元测试中途停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19227398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com