Manual Test::Unit and popen's require space

(7rans) #1

Hi, I'm trying to manully run unit test using Test::Unit because I need
to isolate certain requires from others in the
course of the tests. Certain files I'm testing can potentially cause
conflits with others, so I need to run them in isolation. But I still
want to tally the tests/assertions/errors, etc. So I'm wondering if I'm
doing this right. Specifically, is popen("-", "w+") giving me a whole
seperate "require space" to utilize?

Here's my code so far:

    def fork_test( testfile )
      result = nil
      IO.popen("-","w+") do |pipe|
        result = pipe.instance_eval do
          require 'test/unit'
          require 'test/unit/collector'
          require 'test/unit/collector/objectspace'
          require 'test/unit/ui/testrunnermediator'
          load( testfile )
          tests = Test::Unit::Collector::ObjectSpace.new.collect
          runner = Test::Unit::UI::TestRunnerMediator.new( tests )
          runner.run_suite
        end
      end
      p result
      result
    end

Thanks,
T.

(Ara.T.Howard) #2

i think you'll want to open up a pipe to the ruby interpreter and send a
program to it - forking will not give you a clean require space:

   jib:~ > cat a.rb
   require 'yaml'

   src = <<-src
     puts( require('yaml') ? 'required yaml' : 'already had yaml' )
   src

···

On Thu, 11 Aug 2005, Trans wrote:

Hi, I'm trying to manully run unit test using Test::Unit because I need
to isolate certain requires from others in the
course of the tests. Certain files I'm testing can potentially cause
conflits with others, so I need to run them in isolation. But I still
want to tally the tests/assertions/errors, etc. So I'm wondering if I'm
doing this right. Specifically, is popen("-", "w+") giving me a whole
seperate "require space" to utilize?

Here's my code so far:

   def fork_test( testfile )
     result = nil
     IO.popen("-","w+") do |pipe|
       result = pipe.instance_eval do
         require 'test/unit'
         require 'test/unit/collector'
         require 'test/unit/collector/objectspace'
         require 'test/unit/ui/testrunnermediator'
         load( testfile )
         tests = Test::Unit::Collector::ObjectSpace.new.collect
         runner = Test::Unit::UI::TestRunnerMediator.new( tests )
         runner.run_suite
       end
     end
     p result
     result
   end

Thanks,
T.

   #
   # via child
   #
   stdout =
     IO::popen('-', 'r+') do |child|
       if child
         child.puts src
         child.close_write
         child.read
       else
         eval STDIN.read
       end
     end
   puts "child - stdout <#{ stdout.strip }>"

   #
   # via ruby
   #
   stdout =
     IO::popen('ruby', 'r+') do |ruby|
       ruby.puts src
       ruby.close_write
       ruby.read
     end
   puts "ruby - stdout <#{ stdout.strip }>"

   jib:~ > ruby a.rb
   child - stdout <already had yaml>
   ruby - stdout <required yaml>

hth.

-a
--

email :: ara [dot] t [dot] howard [at] noaa [dot] gov
phone :: 303.497.6469
Your life dwells amoung the causes of death Like a lamp standing in a strong breeze. --Nagarjuna

===============================================================================

(7rans) #3

Thanks Ara! That's just what I needed!

(7rans) #4

Almost have it, but can anyone tell me how to stop Test::Unit from
running the automatic test runner?

Thanks,
T.

(Ryan Leavengood) #5

Trans said:

Almost have it, but can anyone tell me how to stop Test::Unit from
running the automatic test runner?

Put this somewhere in your script:

Test::Unit.run = true

At first this may not look right, but what it is doing is telling
Test::Unit that the tests have already been run, not that it should run
them.

The at_exit block in unit.rb checks this flag before running the test runner.

Ryan

(7rans) #6

Ryan, Thanks.