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
···
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?
--
Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/
The "`Stok' disclaimers" apply.
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 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?
Are you using Test::Unit? Look at the teardown method.
···
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?
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 5/13/06, Mike Stok <mike@stok.ca> 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.
Use an END block.
···
On May 13, 2006, at 6:27 PM, Sy Ali wrote:
On 5/13/06, Mike Stok <mike@stok.ca> 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.
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
···
On 13-May-06, at 6:27 PM, Sy Ali wrote:
On 5/13/06, Mike Stok <mike@stok.ca> 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.
--
Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/
The "`Stok' disclaimers" apply.
you can use 'ensure' inside begin/def block:
···
#
<test preparation code>
begin
<execute tests>
ensure
<code executed after all testing>
end
#
----- Original Message ----- From: "Sy Ali" <sy1234@gmail.com>
To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
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 Stok <mike@stok.ca> 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.
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][1] 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?
[1]: http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/
-- Daniel
···
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.
Sy Ali wrote:
···
On 5/13/06, Mike Stok <mike@stok.ca> wrote:
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.
Try Kernel#at_exit
or this (much simpler):
Rake::TestTask.new(:real_test)
task :test => [:real_test] do
# code here
end
-- Daniel
···
On May 14, 2006, at 2:09 AM, Daniel Harple 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
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!
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 5/13/06, Mike Stok <mike@stok.ca> wrote:
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...)
I played with 'ensure', but it wasn't the answer.. =)
You can setup your test runner manually:
<snip>
You could do the same with a suite. See the [Test::Unit docs][1] on
how to create a test suite manually.
<snip>
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 5/13/06, Daniel Harple <dharple@generalconsumption.org> wrote:
<example snipped>
Thank you. This was just the solution I needed. It was easy to get
working too. =)
···
On 5/13/06, Daniel Harple <dharple@generalconsumption.org> wrote:
You can setup your test runner manually:
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/14/06, Sy Ali <sy1234@gmail.com> wrote:
I think by far the easiest solution is Kernel#at_exit .. it totally
slipped my mind!
Aha, now this definitely seems like something that should be there if its not already.
···
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?)
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 Sun, 14 May 2006, Sy Ali wrote:
I played with 'ensure', but it wasn't the answer.. =)
On 5/13/06, Daniel Harple <dharple@generalconsumption.org> wrote:
You can setup your test runner manually:
<snip>
You could do the same with a suite. See the [Test::Unit docs][1] on
how to create a test suite manually.<snip>
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?)
--
be kind whenever possible... it is always possible.
- h.h. the 14th dali lama
Sy Ali wrote:
You can setup your test runner manually:
<example snipped>
Thank you. This was just the solution I needed. It was easy to get
working too. =)
I have a similar situation: I have to setup and teardown various bits in
a database before and after (respectively) [the combined whole of] ALL
tests. Furthermore, the teardown must occur regardless of whether any
tests pass or fail. I've posted to ruby-talk about this a while ago.
My final solution was to just use a wrapper script to call rake, and
then call the teardown code.
Would the above TestRunner solution work for my case, too?
Pistos
···
On 5/13/06, Daniel Harple <dharple@generalconsumption.org> wrote:
--
Posted via http://www.ruby-forum.com/\.
> Is there an easy way for me to abort a test? (i.e. to exit the test
> method cleanly?)harp:~ > cat a.rb
<snip>
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 5/14/06, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:
On Sun, 14 May 2006, Sy Ali wrote: