ruby - Unit tests stops half way when adding certain assertions -
i trying write unit testing ruby script. however, not working think should work, , tests, stops half way through unit test.
this method testing.
#!/usr/bin/env ruby require 'ptools' require 'test/unit' class inputvalidators # checks whether input file exist. def input_file_validator(input_file) begin raise argumenterror, "error: input file \"#{input_file}\" not exist. \n" unless file.exist?(input_file) raise argumenterror, "error: input file empty. please correct , try again. \n" if file.zero?(input_file) raise argumenterror, "error: input file in binary format - text based input files supported. \n" if file.binary?(input_file) rescue exception => e puts # empty line puts e.message puts # 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 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")) # binary file end end
now, if leave script above, unit test work perfectly...
current output:
run options: # running tests: [1/1] unittests#test_input_file_validator_1 error: input file "./test_inputs/missing_input.fna" 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]
however, if uncomment 1 of other asserts, unit test stops , doesn't complete.
output (when uncommenting 1 or both of assertions in above script):
run options: # running tests: [1/1] unittests#test_input_file_validator_1 error: input file "./test_inputs/missing_input.fna" not exist. error: input file empty. please correct , try again.
i have no idea doing wrong on appreciated. let me know if need more info.
well, if run exit
, not rescuing exception, process stops running.
i guess assert_raise
capture error or other magic complete process. running at_exit
hooks might of magic tricks.
despite this, it's considered bad practice use exceptions workflows. not recommend raising error , catching exit process. use abort
message.
Comments
Post a Comment