Unit tests stops half way when adding certain assertions - ruby

I am trying to write some unit testing for my ruby script. However, it is not working as I think it should work, and with certain tests, it just stops half way through the unit test.
This is the method I am currently testing.
#!/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
Now, if I leave the script as above, the 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" 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]
However, if I even uncomment one of the other asserts, the unit test just stops and doesn't complete.
Output (when uncommenting just one or both of the assertions in the above script):
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.
I have no idea what I am doing wrong so any help on this would be most appreciated.
Let me know if you need more info.

well, if you run exit and you are not rescuing from that exception, your process just stops running.
i guess that assert_raise does actually capture that error or does some other magic to complete the process. running at_exit hooks might be some of those magic tricks.
despite all this, it's considered a bad practice to use exceptions for workflows. so i would not recommend raising an error and then catching it immediately just to exit the process. i usually just use abort with a message.

Related

Test CLI with parameters

I assume this is very newbie stuff but I'm learning Ruby by doing, and I'm developing a small CLI tool that receives a couple of parameters in order to do its stuff properly. This is my current workflow:
I want to test (using Minitest) all the possible flows:
Exits with 0 and help message is shown if ARGV.count != 2
Exits with 1 if first param is not correct
Exits with 1 if second param is not correct
Exits with 1 if both params are not correct
Exits with 0 and does stuff if all params are correct
Now, if I run tests the only thing I see is the help output as there is no parameter being passed.
So, a couple of questions:
How can I pass arguments to the main program in tests?
How can I test the output? (I'm using puts)
Thanks!
nice diagram!
you can either use helpers like aruba https://github.com/cucumber/aruba
or dig into ruby internals in order to bend it to your will!
# test.rb
pseudoIO = StringIO.new
$stdout = pseudoIO
puts "hi #{ARGV.join(', ')}"
ARGV.replace ["file1"]
puts "now its #{ARGV.join(', ')}"
abort "captured: #{pseudoIO.string}"
output should be
ruby text.rb "whutup"
# => captured: hi whutup
# => now its file1

How do I unit test a Ruby app for return codes

I'm not terribly familiar with Ruby testing and my searches haven't yielded an answer to my specific question.
Currently, I have an app that raises StandardError to exit on certain conditions. The drawback to this is that the exit code is always 1.
I want to use exit() to provide unique codes to the exit conditions.
Currently, the project spec tests the StandardError mechanism as follows:
it "should raise an exception if no unignored project coverage file files were found" do
fixtures_project.ignore_list = ["*fixturesTests*", "*fixtures*"]
expect {fixtures_project.coverage_files}.to raise_error(StandardError)
end
I want to do something like assert($?.exit == 102), but I still need the fixtures_project.coverage_files to fire before hand in order to get the exit code.
I've tried variations of assert_equal(102, fixtures_project.coverage_files, "Hello there") inside the it/end with no luck. I'm sure this is probably simple Ruby, but I haven't grokked this bit yet.
You were right in trying to use $? object. Just call $?.exitstatus to get your exit status.
it "should raise an exception if no unignored project coverage file files were found" do
fixtures_project.ignore_list = ["*fixturesTests*", "*fixtures*"]
expect {fixtures_project.coverage_files}.to raise_error(StandardError)
expect($?.exitstatus).to eq(102)
end

Equivalent to Perl's END block in Ruby

Is there a Perl equivalent END block in Ruby? In Perl, if I specify an END block, the code in that block will get executed no matter where the program bails out. It is great functionality for closing open file handles. Does Ruby support similar functionality? I tried Ruby's "END{}" block but that doesnt seem to get called if I had an exit in the code due to an error.
Thanks!
Use at_exit, which will run regardless of whether an exception was raised or not:
at_exit { puts 'exited!' }
raise
prints "exited" as expected.
You should only consider this if you cannot use an ensure, as at_exit causes logic to reside far away from where the actual exit occurs.
Yes. A block may have an 'ensure' clause. Here's an example:
begin
# This will cause a divide by zero exception
puts 3 / 0
rescue Exception => e
puts "An error occurred: #{e}"
ensure
puts "I get run anyway"
end
Running this produces:
An error occurred: divided by 0
I get run anyway

Re-run failed unit tests in ruby

I have some unit tests written using Test::Unit::TestCase, with XML generated by ci_reporter. However, due to circumstances beyond my control, they may occasionally fluctuate, and randomly fail. I'd like to detect when a test fails, and attempt to re-run it.
I tried doing this by monkey-patching 'teardown' to check 'passed?', and re-running the tests on a failure. However, the XML output will still show the first failed case, and not the second (now passing) run.
This sounds a bit like the opposite of Multiple tests with minitest
Perhaps this is a possibility: Copy your test case in an own file. As an example, try the following test:
#store it as file 'testcase.rb'
gem 'test-unit'
require 'test/unit'
class X < Test::Unit::TestCase
def test_1
num = rand(10)
assert_true( num < 2, "Value is #{num}")
end
end
Then define your test call in a rake task:
require 'rake'
TEST_REPETION = 10
task :test do
success = false
TEST_REPETION.times{
stdout = `ruby testcase.rb`
if stdout =~ /Failure/
puts "Failure occured - redo the test"
else
puts 'Tests ok'
success = true
exit
end
}
puts "Stopped after #{TEST_REPETION} tries" unless success
end
Now the test is called, until the test succeed or TEST_REPETION are done.
Remarks:
Rake isn't needed, you may do the call without rake (My template was a rake task)
This works only, if your xml changes for each run (it must be regenerated before the test. else you test always the same).
You may store the test result (stdout) in a file and use it later to analyze, which tests failed and try to retest them.

check for (the absence of) `puts` in RSpec

I am using rspec for my test in a ruby project, and I want to spec that my program should not output anything when the -q option is used. I tried:
Kernel.should_not_receive :puts
That did not result in a failed test when there was output to the console.
How do I verify the absents of text output?
puts uses $stdout internally. Due to the way it works, the easiest way to check is to simply use: $stdout.should_not_receive(:write)
Which checks nothing is written to stdout as expected.
Kernel.puts (as above) would only result in a failed test when it
is explictely called as such (e.g. Kernel.puts "Some text"), where
as most cases it's call in the scope of the current object.
The accepted answer above is incorrect. It "works" because it doesn't receive a :write message but it might have received a :puts message.
The correct line should read:
$stdout.should_not_receive(:puts)
Also you need to make sure you put the line before the code that will write to STDIO. For instance:
it "should print a copyright message" do
$stdout.should_receive(:puts).with(/copyright/i)
app = ApplicationController.new(%w[project_name])
end
it "should not print an error message" do
$stdout.should_not_receive(:puts).with(/error/i)
app = ApplicationController.new(%w[project_name])
end
That's an actual working RSpec from a project

Resources