Putting unit test cases in the code - RDoc & UnitTest

Discussion in 'Ruby' started by listrecv@gmail.com, Nov 14, 2005.

  1. Guest

    Hi. I find that the best place to put unit test cases is in the file
    for the class being tested itself. This puts everything in the same
    place, and also makes examples readily available for someone trying to
    understand the code.

    Question 1: Can I do this without having Test::Unit automatically run
    the tests, every time the file is require 'd?

    Question 2: How can I do this so that RDoc shows the examples in the
    generated documentation?
    , Nov 14, 2005
    #1
    1. Advertising

  2. How about just something like that at the end of your file;

    if ENV["RUN_TESTS"]
    require 'test/unit'
    class MyTest < Test::Unit::TestCase
    #your tests
    end
    end


    Cheers,
    Dave

    On 11/15/05, <> wrote:
    > Hi. I find that the best place to put unit test cases is in the file
    > for the class being tested itself. This puts everything in the same
    > place, and also makes examples readily available for someone trying to
    > understand the code.
    >
    > Question 1: Can I do this without having Test::Unit automatically run
    > the tests, every time the file is require 'd?
    >
    > Question 2: How can I do this so that RDoc shows the examples in the
    > generated documentation?
    >
    >
    >
    David Balmain, Nov 14, 2005
    #2
    1. Advertising

  3. Hi --

    On Tue, 15 Nov 2005, wrote:

    > Hi. I find that the best place to put unit test cases is in the file
    > for the class being tested itself. This puts everything in the same
    > place, and also makes examples readily available for someone trying to
    > understand the code.
    >
    > Question 1: Can I do this without having Test::Unit automatically run
    > the tests, every time the file is require 'd?


    You can put this at the end of the file:

    if $0 == __FILE__
    # test code here
    end

    and then that code will only run if that exact file has been invoked.


    David

    --
    David A. Black
    David A. Black, Nov 14, 2005
    #3
  4. Guest

    David,

    That's a great idea. The only problem is that, let's say FileA and
    FileB both require FileC. We'd like to run the unit tests for each
    file separately, and certainly not more than once. With your code,
    they'll get run recursively, each time they're required.

    But maybe I could add
    if ENV["RUN_TESTS"} && $0 == __FILE__ ?

    Would that do it? And I guess then RDoc would see the class and doc it
    (it'll ignore the if statement wrapping it, I assume).
    , Nov 14, 2005
    #4
  5. Hi --

    On Tue, 15 Nov 2005, wrote:

    > David,
    >
    > That's a great idea. The only problem is that, let's say FileA and
    > FileB both require FileC. We'd like to run the unit tests for each
    > file separately, and certainly not more than once. With your code,
    > they'll get run recursively, each time they're required.


    They shouldn't run at all from any require'd file, because the $0 ==
    __FILE__ test won't pass in that file.

    > But maybe I could add
    > if ENV["RUN_TESTS"} && $0 == __FILE__ ?
    >
    > Would that do it? And I guess then RDoc would see the class and doc it
    > (it'll ignore the if statement wrapping it, I assume).


    If it gets that complex you might want to use Rake and set up a test
    target. That might be more scaleable.


    David

    --
    David A. Black
    David A. Black, Nov 14, 2005
    #5
  6. Trans Guest

    With Facets I use a remark:

    =begin test
    require 'text/unit'
    class ATest << Test::Unit::TestCase
    ...
    end
    =end

    when I want to run the test I remark the remarks:

    #=begin test
    ...
    #=end

    That works good for dev testing.

    Later when I want to run the enite testsuite I use a Reap task (see
    Rubyforge) that extracts and copies the remarked tests to the test
    directory.

    T.
    Trans, Nov 14, 2005
    #6
  7. Eric Hodel Guest

    On Nov 14, 2005, at 7:32 AM, wrote:

    > Hi. I find that the best place to put unit test cases is in the file
    > for the class being tested itself. This puts everything in the same
    > place, and also makes examples readily available for someone trying to
    > understand the code.



    So does creating a file per class and placing tests into identically =20
    named files with test_ on the front.

    This also makes it easy for testrb to pick up your tests and make =20
    things Just Work=99

    > Question 1: Can I do this without having Test::Unit automatically run
    > the tests, every time the file is require 'd?


    You need to not require test/unit. This is best done by placing =20
    tests in their own files.

    > Question 2: How can I do this so that RDoc shows the examples in the
    > generated documentation?


    Put the tests in their own file and point RDoc at your test files.

    Typically I've found examples are best placed in the class and method =20=

    documentation because tests can provide an overwhelming amount of =20
    information.

    --=20
    Eric Hodel - - http://segment7.net
    FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04
    Eric Hodel, Nov 14, 2005
    #7
  8. Eric Hodel Guest

    On Nov 14, 2005, at 9:32 AM, Trans wrote:

    > With Facets I use a remark:
    >
    > =begin test
    > require 'text/unit'
    > class ATest << Test::Unit::TestCase
    > ...
    > end
    > =end
    >
    > when I want to run the test I remark the remarks:
    >
    > #=begin test
    > ...
    > #=end
    >
    > That works good for dev testing.
    >
    > Later when I want to run the enite testsuite I use a Reap task (see
    > Rubyforge) that extracts and copies the remarked tests to the test
    > directory.


    I like to run my tests all the time, and I like source control to run
    tests for me when I check in to make sure I didn't forget. Having to
    edit a file to run them is unnecessary extra work.

    This extra work does not reinforce good testing discipline.

    --
    Eric Hodel - - http://segment7.net
    FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04
    Eric Hodel, Nov 14, 2005
    #8
  9. Trans Guest

    Eric, you missed the last point. I have a script that exracts all tests
    to test_xxx files in test directory, so I have best of both worlds.

    T.
    Trans, Nov 15, 2005
    #9
  10. Eric Hodel Guest

    On Nov 14, 2005, at 7:02 PM, Trans wrote:

    > Eric, you missed the last point. I have a script that exracts all
    > tests
    > to test_xxx files in test directory, so I have best of both worlds.


    But... I don't need that script, so I will never have to maintain it.

    --
    Eric Hodel - - http://segment7.net
    FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04
    Eric Hodel, Nov 15, 2005
    #10
    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. Saurabh
    Replies:
    6
    Views:
    4,498
    Chris Smith
    May 30, 2004
  2. Scott David Daniels

    Failing unittest Test cases

    Scott David Daniels, Jan 9, 2006, in forum: Python
    Replies:
    18
    Views:
    500
    Scott David Daniels
    Jan 13, 2006
  3. Terry

    ignored test cases in unittest

    Terry, Aug 16, 2009, in forum: Python
    Replies:
    3
    Views:
    326
    David House
    Aug 17, 2009
  4. David Mitchell
    Replies:
    10
    Views:
    244
  5. jitendra gupta
    Replies:
    0
    Views:
    86
    jitendra gupta
    Mar 8, 2013
Loading...

Share This Page