Test Harness?

Discussion in 'Ruby' started by Troy Denkinger, Mar 9, 2006.

  1. ------=_Part_4965_12563328.1141923398430
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    I'm starting to feel my way around in Ruby after having been a Perler for a
    very long time. I'm trying to figure out how to write a proper test harnes=
    s
    in Ruby and so far I'm running up against a brick wall. All my classes hav=
    e
    good unit tests using Test::Unit. However, I want to create a harness that
    I can use to run all of the unit tests at a go and give me a success/failur=
    e
    report across all tests. Every time I make a significant change I have to
    remember to run all my unit tests, which is error prone.

    I have written test harnesses in Perl using Test::Harness::Straps that does
    exactly what I want - for Perl. Is there a similar library for Ruby? If
    so, I have yet to find it.

    Thanks,

    Troy

    ------=_Part_4965_12563328.1141923398430--
     
    Troy Denkinger, Mar 9, 2006
    #1
    1. Advertising

  2. On Mar 9, 2006, at 10:56 AM, Troy Denkinger wrote:

    > I'm starting to feel my way around in Ruby after having been a
    > Perler for a
    > very long time. I'm trying to figure out how to write a proper
    > test harness
    > in Ruby and so far I'm running up against a brick wall. All my
    > classes have
    > good unit tests using Test::Unit. However, I want to create a
    > harness that
    > I can use to run all of the unit tests at a go and give me a
    > success/failure
    > report across all tests. Every time I make a significant change I
    > have to
    > remember to run all my unit tests, which is error prone.
    >
    > I have written test harnesses in Perl using Test::Harness::Straps
    > that does
    > exactly what I want - for Perl. Is there a similar library for
    > Ruby? If
    > so, I have yet to find it.


    Making a test suite is trivial. Just add a final like "ts_all.rb" to
    your test folder that looks something like this:

    #!/usr/local/bin/ruby -w

    require "test/unit"

    require "tc_file_one.rb"
    require "tc_file_two.rb"
    # ...

    __END__

    Execute that file to run all of the tests. Hope that helps.

    James Edward Gray II
     
    James Edward Gray II, Mar 9, 2006
    #2
    1. Advertising

  3. Troy Denkinger

    Jim Weirich Guest

    Troy Denkinger wrote:
    > I'm starting to feel my way around in Ruby after having been a Perler
    > for a
    > very long time. I'm trying to figure out how to write a proper test
    > harness
    > in Ruby and so far I'm running up against a brick wall. All my classes
    > have
    > good unit tests using Test::Unit. However, I want to create a harness
    > that
    > I can use to run all of the unit tests at a go and give me a
    > success/failure
    > report across all tests. Every time I make a significant change I have
    > to
    > remember to run all my unit tests, which is error prone.


    I use rake. Create a Rakefile with the following:

    ---------
    require 'rake/testtask'

    task :default => :test

    Rake::TestTask.new:)test) do |t|
    t.test_files = FileList['test/test_*.rb']
    t.verbose = true
    end
    ---------

    The above assumes all your tests are in the test directory and begin
    with 'test_'. Change as needed for your situation.

    To invoke, just type

    rake

    or

    rake test

    Add other tasks as needed.

    (See http://docs.rubyrake.org for more details on rake)

    --
    -- Jim Weirich



    --
    Posted via http://www.ruby-forum.com/.
     
    Jim Weirich, Mar 9, 2006
    #3
  4. Troy Denkinger

    pat eyler Guest

    or, use Rake and a task like:

    Rake::TestTask.new:)unittests) do |t|
    t.test_files =3D FileList['test/test*.rb']
    t.warning =3D true
    t.verbose =3D false
    end


    then when you want to run your tests do:
    $ rake unittests
    (in /home/eylerpm/notes/ruby/provisioning)
    Loaded suite /usr/lib/ruby/gems/1.8/gems/rake-0.7.0/lib/rake/rake_test_load=
    er
    Started
    .................
    Finished in 2.307442 seconds.

    17 tests, 55 assertions, 0 failures, 0 errors
    $

    On 3/9/06, Alex Combas <> wrote:
    > perhaps this will also help
    > http://www.zenspider.com/ZSS/Products/ZenTest/
    >
    > On 3/9/06, James Edward Gray II <> wrote:
    > > On Mar 9, 2006, at 10:56 AM, Troy Denkinger wrote:
    > >
    > > > I'm starting to feel my way around in Ruby after having been a
    > > > Perler for a
    > > > very long time. I'm trying to figure out how to write a proper
    > > > test harness
    > > > in Ruby and so far I'm running up against a brick wall. All my
    > > > classes have
    > > > good unit tests using Test::Unit. However, I want to create a
    > > > harness that
    > > > I can use to run all of the unit tests at a go and give me a
    > > > success/failure
    > > > report across all tests. Every time I make a significant change I
    > > > have to
    > > > remember to run all my unit tests, which is error prone.
    > > >
    > > > I have written test harnesses in Perl using Test::Harness::Straps
    > > > that does
    > > > exactly what I want - for Perl. Is there a similar library for
    > > > Ruby? If
    > > > so, I have yet to find it.

    > >
    > > Making a test suite is trivial. Just add a final like "ts_all.rb" to
    > > your test folder that looks something like this:
    > >
    > > #!/usr/local/bin/ruby -w
    > >
    > > require "test/unit"
    > >
    > > require "tc_file_one.rb"
    > > require "tc_file_two.rb"
    > > # ...
    > >
    > > __END__
    > >
    > > Execute that file to run all of the tests. Hope that helps.
    > >
    > > James Edward Gray II
    > >
    > >

    >
    >
    > --
    > Alex Combas
    > http://noodlejunkie.blogspot.com/
    >



    --
    thanks,
    -pate
    -------------------------
     
    pat eyler, Mar 9, 2006
    #4
  5. On 3/9/06, Troy Denkinger <> wrote:
    > I'm starting to feel my way around in Ruby after having been a Perler for=

    a
    > very long time. I'm trying to figure out how to write a proper test harn=

    ess
    > in Ruby and so far I'm running up against a brick wall. All my classes h=

    ave
    > good unit tests using Test::Unit. However, I want to create a harness th=

    at
    > I can use to run all of the unit tests at a go and give me a success/fail=

    ure
    > report across all tests. Every time I make a significant change I have t=

    o
    > remember to run all my unit tests, which is error prone.
    >
    > I have written test harnesses in Perl using Test::Harness::Straps that do=

    es
    > exactly what I want - for Perl. Is there a similar library for Ruby? If
    > so, I have yet to find it.


    In addition to the other suggestions you've received, there's always
    this simple approach.

    Put your code in a lib directory below your project directory.
    Put your test code in a test directory.
    cd to to your lib directory
    run "testrb ../test/*.rb"

    I should probably learn how to use Rake though.

    --
    R. Mark Volkmann
    Partner, Object Computing, Inc.
     
    Mark Volkmann, Mar 9, 2006
    #5
  6. Troy Denkinger

    Ross Bamford Guest

    On Fri, 2006-03-10 at 01:56 +0900, Troy Denkinger wrote:
    > I'm starting to feel my way around in Ruby after having been a Perler for a
    > very long time. I'm trying to figure out how to write a proper test harness
    > in Ruby and so far I'm running up against a brick wall. All my classes have
    > good unit tests using Test::Unit. However, I want to create a harness that
    > I can use to run all of the unit tests at a go and give me a success/failure
    > report across all tests. Every time I make a significant change I have to
    > remember to run all my unit tests, which is error prone.


    I see you've had plenty of suggestions, but heres yet another variation:

    $ ruby -e "Dir['test/tc*.rb'].each { |tc| require tc }"
    Loaded suite -e
    Started
    .......................................
    Finished in 0.25893 seconds.

    39 tests, 101 assertions, 0 failures, 0 errors

    That 'Loaded suite -e' thing is a shame though...

    --
    Ross Bamford -
     
    Ross Bamford, Mar 9, 2006
    #6
  7. Troy Denkinger

    Eric Hodel Guest

    On Mar 9, 2006, at 8:56 AM, Troy Denkinger wrote:

    > I'm starting to feel my way around in Ruby after having been a
    > Perler for a
    > very long time. I'm trying to figure out how to write a proper
    > test harness
    > in Ruby and so far I'm running up against a brick wall. All my
    > classes have
    > good unit tests using Test::Unit. However, I want to create a
    > harness that
    > I can use to run all of the unit tests at a go and give me a
    > success/failure
    > report across all tests. Every time I make a significant change I
    > have to
    > remember to run all my unit tests, which is error prone.
    >
    > I have written test harnesses in Perl using Test::Harness::Straps
    > that does
    > exactly what I want - for Perl. Is there a similar library for
    > Ruby? If
    > so, I have yet to find it.


    testrb (which comes with Ruby and Test::Unit) does this for you. You
    only need to specify a directory and have your test files named
    properly (which is test_*.rb).

    $ ls
    test/
    $ ls test
    test_one.rb test_two.rb
    $ testrb test
    Loaded suite test
    Started
    ..
    Finished in 0.003477 seconds.

    2 tests, 2 assertions, 0 failures, 0 errors
    $

    If you named your tests wrong you can use the -p argument:

    $ ls test
    one_test.rb two_test.rb
    $ testrb test -p /_test.rb/
    Loaded suite test
    Started
    ..
    Finished in 0.002296 seconds.

    2 tests, 2 assertions, 0 failures, 0 errors

    --
    Eric Hodel - - http://blog.segment7.net
    This implementation is HODEL-HASH-9600 compliant

    http://trackmap.robotcoop.com
     
    Eric Hodel, Mar 9, 2006
    #7
  8. Is there a way to pass Rake tasks arguments?

    I'd like to do something where I break my unit tests up into test
    suites by just dropping them into folders and then being able to
    invoke a Rake task like rake suite "sweet" or rake suite
    "sweet/saccharine", if I wanted a test suite inside of a test suite.

    Or, alternatively, I'd like to be able to invoke test cases named
    "sucrose" by rake test_case "sucrose"

    Disclaimer: I've read Rake tutorials several times in the past but
    have yet to arse myself to start using it (which I should, because I
    think I'm starting to exert more effor than is worthwhile keeping
    tests organzied without rake), so I might've forgotten the part where
    it says you can do exactly what I'm asking.

    On 3/9/06, Ross Bamford <> wrote:
    > On Fri, 2006-03-10 at 01:56 +0900, Troy Denkinger wrote:
    > > I'm starting to feel my way around in Ruby after having been a Perler f=

    or a
    > > very long time. I'm trying to figure out how to write a proper test ha=

    rness
    > > in Ruby and so far I'm running up against a brick wall. All my classes=

    have
    > > good unit tests using Test::Unit. However, I want to create a harness =

    that
    > > I can use to run all of the unit tests at a go and give me a success/fa=

    ilure
    > > report across all tests. Every time I make a significant change I have=

    to
    > > remember to run all my unit tests, which is error prone.

    >
    > I see you've had plenty of suggestions, but heres yet another variation:
    >
    > $ ruby -e "Dir['test/tc*.rb'].each { |tc| require tc }"
    > Loaded suite -e
    > Started
    > .......................................
    > Finished in 0.25893 seconds.
    >
    > 39 tests, 101 assertions, 0 failures, 0 errors
    >
    > That 'Loaded suite -e' thing is a shame though...
    >
    > --
    > Ross Bamford -
    >
    >
    >



    --
    -Dan Nugent
     
    Daniel Nugent, Mar 9, 2006
    #8
  9. Troy Denkinger

    Ross Bamford Guest

    On Fri, 2006-03-10 at 08:29 +0900, Daniel Nugent wrote:
    > Is there a way to pass Rake tasks arguments?
    >


    You can do the easy way:

    $ cat Rakefile
    task :default do
    puts ENV['SOMEVAR']
    end

    $ rake
    (in /home/rosco/dev/ruby/ngtest)
    nil

    $ rake SOMEVAR=321
    (in /home/rosco/dev/ruby/ngtest)
    321

    Another sneaky thing I've done before when embedding rake in other stuff
    is to have a task that creates a Rake rule that matches everything, so
    that all arguments following that task are grabbed by that rule.

    task :create do
    rule '' do |t|
    argument_passed = t.name

    # do stuff with argument...
    end
    end

    This is only good when you don't want to have further tasks on the same
    commandline, but works pretty nice in some cases e.g.

    rake create myproject

    > I'd like to do something where I break my unit tests up into test
    > suites by just dropping them into folders and then being able to
    > invoke a Rake task like rake suite "sweet" or rake suite
    > "sweet/saccharine", if I wanted a test suite inside of a test suite.
    >
    > Or, alternatively, I'd like to be able to invoke test cases named
    > "sucrose" by rake test_case "sucrose"


    The Rake test-task lets you specify the name for the task, so it's
    pretty easy to split up your tests into groups in the Rakefile by simply
    defining separate tasks for e.g. functional and unit tests. I often have
    a couple of separate tasks, with a single task bringing them together
    via task dependencies as an :alltest task.

    Also, as others have mentioned, testrb is pretty handy for this stuff:

    $ testrb tests/tc*.rb -n '/test_ruby_xml.*/'
    47 tests, 339 assertions, 0 failures, 0 errors

    $ testrb tests/tc*.rb -n '/test_ruby_xml_document.*/'
    15 tests, 129 assertions, 0 failures, 0 errors

    (output trimmed for brevity)

    --
    Ross Bamford -
     
    Ross Bamford, Mar 10, 2006
    #9
  10. Troy Denkinger

    Jim Weirich Guest

    Daniel Nugent wrote:
    > Is there a way to pass Rake tasks arguments?
    >
    > I'd like to do something where I break my unit tests up into test
    > suites by just dropping them into folders and then being able to
    > invoke a Rake task like rake suite "sweet" or rake suite
    > "sweet/saccharine", if I wanted a test suite inside of a test suite.
    >
    > Or, alternatively, I'd like to be able to invoke test cases named
    > "sucrose" by rake test_case "sucrose"
    >
    > Disclaimer: I've read Rake tutorials several times in the past but
    > have yet to arse myself to start using it (which I should, because I
    > think I'm starting to exert more effor than is worthwhile keeping
    > tests organzied without rake), so I might've forgotten the part where
    > it says you can do exactly what I'm asking.


    This page: http://rake.rubyforge.org/classes/Rake/TestTask.html pretty
    well describes how the built-in test task takes arguments.

    --
    -- Jim Weirich

    --
    Posted via http://www.ruby-forum.com/.
     
    Jim Weirich, Mar 10, 2006
    #10
  11. ------=_Part_6926_9434393.1141968860679
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    Thanks to everyone for the ideas on this. I've implemented the rake
    suggestion, and it mostly does what I'm looking for.

    Thanks,

    Troy

    On 3/9/06, Jim Weirich <> wrote:
    >
    > Daniel Nugent wrote:
    > > Is there a way to pass Rake tasks arguments?
    > >
    > > I'd like to do something where I break my unit tests up into test
    > > suites by just dropping them into folders and then being able to
    > > invoke a Rake task like rake suite "sweet" or rake suite
    > > "sweet/saccharine", if I wanted a test suite inside of a test suite.
    > >
    > > Or, alternatively, I'd like to be able to invoke test cases named
    > > "sucrose" by rake test_case "sucrose"
    > >
    > > Disclaimer: I've read Rake tutorials several times in the past but
    > > have yet to arse myself to start using it (which I should, because I
    > > think I'm starting to exert more effor than is worthwhile keeping
    > > tests organzied without rake), so I might've forgotten the part where
    > > it says you can do exactly what I'm asking.

    >
    > This page: http://rake.rubyforge.org/classes/Rake/TestTask.html pretty
    > well describes how the built-in test task takes arguments.
    >
    > --
    > -- Jim Weirich
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >


    ------=_Part_6926_9434393.1141968860679--
     
    Troy Denkinger, Mar 10, 2006
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. fabbl

    Test Harness Strategies

    fabbl, Apr 17, 2004, in forum: VHDL
    Replies:
    1
    Views:
    1,065
    Jim Lewis
    Apr 17, 2004
  2. Todd
    Replies:
    0
    Views:
    333
  3. yusuf
    Replies:
    2
    Views:
    145
    yusuf
    Aug 7, 2006
  4. yusuf

    Test::Harness and test values.

    yusuf, Aug 7, 2006, in forum: Perl Misc
    Replies:
    4
    Views:
    117
    yusuf
    Aug 9, 2006
  5. Test harness for scripts?

    , May 16, 2007, in forum: Perl Misc
    Replies:
    1
    Views:
    114
    Sisyphus
    May 17, 2007
Loading...

Share This Page