I’m new to unit testing, and test-first development. Sometimes I
would like to run tests from within other tests rather than recoding
and maintaining a test in two places. Sometimes it helps to pass in
an argument as shown below. In Test::Unit 0.1.4, the first example
test_do_some_work(w=@worker) worked fine, but the second
test_do_more_work(w=@worker) failed to run. Now, in 0.1.8, both fail
to run. I’m using ruby 1.6.8 on Windows. Questions: Is this
behavior by design? Why? Is it a bad idea to do what I am trying?
Thanks,
require 'test/unit’
require ‘work.rb’
class TC_Work < Test::Unit::TestCase
def setup
@worker = Worker.new
end
def test_do_some_work(w=@worker)
s = 'a’
i = 25
assert_equal(‘abcdefghijklmnopqrstuvwxyz’, w.do_some_work(s, i))
end
def test_do_more_work(w=@worker)
s = 'abc’
i = 4
assert_equal(‘zbczbczbczbc’, w.do_more_work(s, i))
end
end
Jim Davis
I’m fairly new to unit testing too, but this could solve the problem:
def test_foo(w = nil)
w ||= @worker
w.bar(baz, quz, fubar)
end
I don’t feel OK when I see an instance var. as a default value, not sure
why.
···
On Sat, Mar 01, 2003 at 06:05:43AM +0900, James Davis wrote:
I’m new to unit testing, and test-first development. Sometimes I
would like to run tests from within other tests rather than recoding
and maintaining a test in two places. Sometimes it helps to pass in
an argument as shown below. In Test::Unit 0.1.4, the first example
test_do_some_work(w=@worker) worked fine, but the second
test_do_more_work(w=@worker) failed to run. Now, in 0.1.8, both fail
to run. I’m using ruby 1.6.8 on Windows. Questions: Is this
behavior by design? Why? Is it a bad idea to do what I am trying?
–
_ _
__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_
_ \ / ` | ’ \
) | (| | |__ \ | | | | | (| | | | |
.__/ _,|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com
Actually, typing random strings in the Finder does the equivalent of
filename completion.
– Discussion on file completion vs. the Mac Finder
Help is just a simple refactoring away. Since the test_xxx methods are
called by the framework, changing their signatures is a questionable
tactic (although one /would/ think that the default argument would
work).
Basically you just need to break the code into separate functions and
then call those functions from the test methods. For example …
class TC_Work < Test::Unit::TestCase
def setup
@worker = Worker.new
end
def test_do_some_work
check_some_work(@worker)
end
def check_some_work(w)
s = ‘a’
i = 25
assert_equal(‘abcdefghijklmnopqrstuvwxyz’, w.do_some_work(s, i))
end
def test_do_more_work
check_more_work(@worker)
end
def check_more_work(w)
s = ‘abc’
i = 4
assert_equal(‘zbczbczbczbc’, w.do_more_work(s, i))
end
end
···
On Fri, 2003-02-28 at 16:05, James Davis wrote:
I’m new to unit testing, and test-first development. Sometimes I
would like to run tests from within other tests rather than recoding
and maintaining a test in two places. Sometimes it helps to pass in
an argument as shown below. In Test::Unit 0.1.4, the first example
test_do_some_work(w=@worker) worked fine, but the second
test_do_more_work(w=@worker) failed to run. Now, in 0.1.8, both fail
to run. I’m using ruby 1.6.8 on Windows. Questions: Is this
behavior by design? Why? Is it a bad idea to do what I am trying?
Thanks,
require ‘test/unit’
require ‘work.rb’
class TC_Work < Test::Unit::TestCase
def setup
@worker = Worker.new
end
def test_do_some_work(w=@worker)
s = ‘a’
i = 25
assert_equal(‘abcdefghijklmnopqrstuvwxyz’, w.do_some_work(s, i))
end
def test_do_more_work(w=@worker)
s = ‘abc’
i = 4
assert_equal(‘zbczbczbczbc’, w.do_more_work(s, i))
end
end
“Beware of bugs in the above code; I have only proved it correct,
not tried it.” – Donald Knuth (in a memo to Peter van Emde Boas)
Help is just a simple refactoring away. Since the test_xxx methods are
called by the framework, changing their signatures is a questionable
tactic (although one /would/ think that the default argument would
work).
Basically you just need to break the code into separate functions and
then call those functions from the test methods. For example …
class TC_Work < Test::Unit::TestCase
def setup
@worker = Worker.new
end
def test_do_some_work
check_some_work(@worker)
end
def check_some_work(w)
s = ‘a’
i = 25
assert_equal(‘abcdefghijklmnopqrstuvwxyz’, w.do_some_work(s, i))
end
end
Yes. Two important realizations here:
- Assertions need not be placed in ‘test_xxx’ methods.
- Refactor mercilessly!
Thanks,