public
Created

I put this at the end of my class files so that running the file directly runs the test for the file.

  • Download Gist
in_line_test_code.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 
class Someclass
...
end
 
########## inline test running
if __FILE__==$PROGRAM_NAME
require_relative '../path_to_test.rb'
end
 
# This will run the relevant test if you run the class file. It will not run that code if the class file is just required from elsewhere, though.
 
#file: path_to_test.rb
# This part is also super easy for unit tests...
 
require 'test/unit'
class SomeclassTest < Test::Unit::TestCase
def setup
# setup code
end
def test_some_feature
assert something_is_true, "it's not true!"
assert_equal value_youre_expecting, Someclass.new.someemethod("params")
# etc.
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.