Forcing some code to run at the end of tests

I’m playing with some test scripts, and I cannot for the life of me
ensure that a block of code is run after every test is run. I can
think of some ugly ways to do it… but is there an elegant way which I
missed?

On 13-May-06, at 5:44 PM, Sy Ali wrote:

I’m playing with some test scripts, and I cannot for the life of me
ensure that a block of code is run after every test is run. I can
think of some ugly ways to do it… but is there an elegant way which I
missed?

Does every test mean every test case? If so you might look at
Test::Unit::TestCase’s teardown.

Mike

Mike S. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.

On May 13, 2006, at 11:44 PM, Sy Ali wrote:

I’m playing with some test scripts, and I cannot for the life of me
ensure that a block of code is run after every test is run. I can
think of some ugly ways to do it… but is there an elegant way which I
missed?

Use the teardown method:

def teardown

end

This is mentioned in the docs: http://www.ruby-doc.org/stdlib/libdoc/
test/unit/rdoc/classes/Test/Unit.html

– Daniel

On 5/13/06, Mike S. [email protected] wrote:

On 13-May-06, at 5:44 PM, Sy Ali wrote:

I’m playing with some test scripts, and I cannot for the life of me
ensure that a block of code is run after every test is run. I can
think of some ugly ways to do it… but is there an elegant way which I
missed?

Does every test mean every test case? If so you might look at
Test::Unit::TestCase’s teardown.

No, not teardown… I want to have some code executed after all testing
has concluded.

You’d think it’d be a matter of running it all at the end of the .rb
file, but this is not true… everything is run and then the test
cases are run.

I suppose I could wrap the entire test in another ruby file… it just
seems strange to need to do this.

On 13-May-06, at 6:27 PM, Sy Ali wrote:

Does every test mean every test case? If so you might look at
seems strange to need to do this.
Have you ever tried code like this:

#!/usr/bin/env ruby

foo = nil
END { puts “end #{foo}” }
puts “regular code”
foo = ‘bar’
BEGIN { puts “begin” }

Maybe an END block can help you (but be careful of bindings and
visibility…)

Mike

Mike S. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.

On May 13, 2006, at 5:44 PM, Sy Ali wrote:

I’m playing with some test scripts, and I cannot for the life of me
ensure that a block of code is run after every test is run. I can
think of some ugly ways to do it… but is there an elegant way which I
missed?

Are you using Test::Unit? Look at the teardown method.

On May 13, 2006, at 6:27 PM, Sy Ali wrote:

Does every test mean every test case? If so you might look at
seems strange to need to do this.
Use an END block.

you can use ‘ensure’ inside begin/def block:

begin ensure end # ----- Original Message ----- From: "Sy Ali" To: "ruby-talk ML" Sent: Saturday, May 13, 2006 6:27 PM Subject: Re: Forcing some code to run at the end of tests

On 5/13/06, Mike S. [email protected] wrote:

On 13-May-06, at 5:44 PM, Sy Ali wrote:

I’m playing with some test scripts, and I cannot for the life of me
ensure that a block of code is run after every test is run. I can
think of some ugly ways to do it… but is there an elegant way which I
missed?

Does every test mean every test case? If so you might look at
Test::Unit::TestCase’s teardown.

No, not teardown… I want to have some code executed after all testing
has concluded.

You’d think it’d be a matter of running it all at the end of the .rb
file, but this is not true… everything is run and then the test
cases are run.

I suppose I could wrap the entire test in another ruby file… it just
seems strange to need to do this.

On May 14, 2006, at 2:09 AM, Daniel H. wrote:

If you are using the rake test task, then you could something like
(I didn’t test this out):

class MyTestTask < Rake::TestTask
def define
super()
# … code you need here
end
end

or this (much simpler):

Rake::TestTask.new(:real_test)
task :test => [:real_test] do

code here

end

– Daniel

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Sy Ali wrote:

I suppose I could wrap the entire test in another ruby file… it just
seems strange to need to do this.

Try Kernel#at_exit
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFEZoigmV9O7RYnKMcRAnGqAKCzdsd8ZRGFFylu8PLWePZ/O7JwlQCfd1it
EZm2Css1MI1cLunv3M3kOCc=
=Mh2m
-----END PGP SIGNATURE-----

Thanks for the ideas everyone…

I did think of using an end block or ensure, but these seemed unclean
to me. I wondered if there was a feature similar to setup/teardown
for doing stuff on exit.

Doing the test stuff manually to force the order tests and other code
is executed in… I tried and failed at… I knew how to run my tests
manually but they were being pulled out from the actual scoring.
Those examples help a lot. I’ll need to keep these in mind if ever I
need to run multiple tests in a specific order (hopefully never!)

I think by far the easiest solution is Kernel#at_exit … it totally
slipped my mind!

On 5/14/06, Sy Ali [email protected] wrote:

I think by far the easiest solution is Kernel#at_exit … it totally
slipped my mind!

Scratch that. at_exit doesn’t work either.

If one of my tests sets a global variable or writes information in a
file… at_exit will still not see the global variable or data…
because the entire tc_name.rb script exits, runs at_exit, and then
the tests are run. Wierd!

I’ll explore forcing that test to be run in a more manual way. Some
examples were given in this thread… I’ll pick them apart and try them
out.

On 5/13/06, Mike S. [email protected] wrote:

Maybe an END block can help you (but be careful of bindings and
visibility…)

Hey that’s neat.

But it works the same way that at_exit does… and so my tests are run
after it.
boggle

Continuing to test. =)

On May 14, 2006, at 12:27 AM, Sy Ali wrote:

No, not teardown… I want to have some code executed after all testing
has concluded.

You’d think it’d be a matter of running it all at the end of the .rb
file, but this is not true… everything is run and then the test
cases are run.

I suppose I could wrap the entire test in another ruby file… it just
seems strange to need to do this.

You can setup your test runner manually:

require ‘test/unit’
require ‘test/unit/ui/console/testrunner’

class TestBlah < Test::Unit::TestCase
def test_asdf
end
end

puts “start”
Test::Unit::UI::Console::TestRunner.run(TestBlah)
puts “finish”

You could do the same with a suite. See the Test::Unit docs on
how to create a test suite manually.

If you are using the rake test task, then you could something like (I
didn’t test this out):

class MyTestTask < Rake::TestTask
def define
super()
# … code you need here
end
end

Why exactly do you need this code to be run at the end of your test?

– Daniel

I played with ‘ensure’, but it wasn’t the answer… =)

On 5/13/06, Daniel H. [email protected] wrote:

You can setup your test runner manually:

You could do the same with a suite. See the [Test::Unit docs][1] on
how to create a test suite manually.

[1]: http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/

I’m looking into this next. I’m not using Rake, so I’ll look at the
docs on test unit to do things more manually.

Why exactly do you need this code to be run at the end of your test?

I’m doing this because I have some tests which will be specific for a
platform, and I want to generate a report to state which tests were
skipped.

Is there an easy way for me to abort a test? (i.e. to exit the test
method cleanly?)

On May 14, 2006, at 7:29 AM, Sy Ali wrote:

Why exactly do you need this code to be run at the end of your test?

I’m doing this because I have some tests which will be specific for a
platform, and I want to generate a report to state which tests were
skipped.

Is there an easy way for me to abort a test? (i.e. to exit the test
method cleanly?)

Aha, now this definitely seems like something that should be there if
its not already.

On 5/14/06, [email protected] [email protected] wrote:

On Sun, 14 May 2006, Sy Ali wrote:

Is there an easy way for me to abort a test? (i.e. to exit the test
method cleanly?)

 harp:~ > cat a.rb
> regards.

Awesome! That’s just what I was looking for (for that particular
problem). It’s strange that the feature isn’t there already.

Now I’m going to play with setting the order of my tests to get a
block of code to execute last.

On Sun, 14 May 2006, Sy Ali wrote:

Is there an easy way for me to abort a test? (i.e. to exit the test
method cleanly?)

 harp:~ > cat a.rb
 require 'test/unit'
 class FooBarTest < Test::Unit::TestCase
   def test_000
     assert true
   end
   def test_001
     assert true
     abort_test!
     assert false
   end


   def abort_test!
     throw 'abort_test!'
   end
   def self.abortable m
     m, m2 = "#{ m }", "__#{ m }__"
     alias_method m2, m
     define_method(m){|*a| catch('abort_test!'){ send(m2,*a) } }
   end
   instance_methods.each{|m| abortable m if m =~ %r/^test/}
 end


 harp:~ > ruby a.rb
 Loaded suite a
 Started
 ..
 Finished in 0.000518 seconds.

 2 tests, 2 assertions, 0 failures, 0 errors

regards.

-a

On 5/13/06, Daniel H. [email protected] wrote:

You can setup your test runner manually:

Thank you. This was just the solution I needed. It was easy to get
working too. =)

On Mon, 15 May 2006, Sy Ali wrote:

problem). It’s strange that the feature isn’t there already.

Now I’m going to play with setting the order of my tests to get a
block of code to execute last.

i’ts simple : they are run in alphabetical order. so, if you name them

test_000
test_001
test_002

they are run in ‘order’.

-a