unit test

B

Brian Buckley

------=_Part_269_8476087.1115384678989
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Hello all,=20

In the code below the first test by itself passes. However, when I add a=20
second test called "test_bad_data", the first test fails. The method count=
=20
on BigDecimal apppears to change from 82 to 86.

I've noticed that if I give "test_bad_data" a different test name (but stil=
l=20
starting with "test_") all works as expected.

What's happening?

Brian

-----------------------------------------
require 'test/unit'
require 'bigdecimal'

class TestBigDecimal < Test::Unit::TestCase
def test_method_count
assert_equal 82, BigDecimal.methods.size
end

def test_bad_data
assert_equal "", "0.0" #causes first test to fail
end

end

------=_Part_269_8476087.1115384678989--
 
N

Nathaniel Talbott

Brian said:
In the code below the first test by itself passes. However, when I add a
second test called "test_bad_data", the first test fails. The method count
on BigDecimal apppears to change from 82 to 86.

I've noticed that if I give "test_bad_data" a different test name (but still
starting with "test_") all works as expected.

What's happening?

I'm not sure, since I can't reproduce the problem:

t.rb:
require 'test/unit'
require 'bigdecimal'

$include_bad_data = ARGV[0]

class TestBigDecimal < Test::Unit::TestCase
def test_method_count
assert_equal 83, BigDecimal.methods.size
end

if($include_bad_data)
def test_bad_data
assert_equal "", "0.0" #causes first test to fail
end
end
end

Output:

C:\TEMP>ruby -v t.rb
ruby 1.8.2 (2005-04-21) [i386-mswin32]
Loaded suite t
Started
 
B

Brian Buckley

------=_Part_389_10275367.1115390141311
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Nathaniel,

I'm 1.8.2 (2005-04-21) [i386-mswin32] also. Windows XP.

I'm not sufficiently familiar with ruby -- might there be custom settings=
=20
that I am setting on my box which aren't on yours? (ruby's equivalent to=20
java's classpath? loadpath?)

And one more piece of diagnosis info: the code below shows the four methods=
=20
that are being added (It still doesn't explain why this only happens to me=
=20
when using the name "test_bad_data" or why our method counts are off - 83 v=
s=20
82)

Any suggestions?

Brian

---------------------------------

class TestBigDecimal < Test::Unit::TestCase

def test_method_count

b =3D BigDecimal.methods
assert_equal 82, b.size

assert_raise(Test::Unit::AssertionFailedError) do
assert_equal "0.0", "0.x"=20
end

a =3D BigDecimal.methods
assert_equal 86, a.size

assert_equal a - b,=20
["pretty_print_cycle",=20
"pretty_print_inspect",=20
"pretty_print",=20
"pretty_print_instance_variables"]
end

end

------=_Part_389_10275367.1115390141311--
 
B

Brian Buckley

------=_Part_397_14464118.1115390812703
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Oops - hang on - I'm on 1.8.2 (2004-12-25) [i386-mswin32]. I'm guessing tha=
t=20
explains the one-method-off part. -Brian

------=_Part_397_14464118.1115390812703--
 
C

Caleb Tennis

And one more piece of diagnosis info: the code below shows the four
methods
that are being added (It still doesn't explain why this only happens to me
when using the name "test_bad_data" or why our method counts are off - 83
vs
82)

One thing of note: the test cases are (at least here) executed in
alphabetical order, not in the order they are defined in. Your
"test_bad_data" is probably getting run first, and modifying something
that is changing how one of your later test cases is running.
 
N

Nathaniel Talbott

Brian said:
I'm not sufficiently familiar with ruby -- might there be custom settings
that I am setting on my box which aren't on yours? (ruby's equivalent to
java's classpath? loadpath?)

And one more piece of diagnosis info: the code below shows the four methods
that are being added (It still doesn't explain why this only happens to me
when using the name "test_bad_data" or why our method counts are off - 83 vs
82)

Any suggestions?

["pretty_print_cycle",
"pretty_print_inspect",
"pretty_print",
"pretty_print_instance_variables"]

OK, I know what's going on now. 'pp' (PrettyPrint) adds those four
methods to Object (or Kernel, I can't remember) when it's required, and
test/unit requires pp when it builds the message for the failed
assertion. The reason I wasn't seeing my methods go up is because when I
wrote my little test program, I was requiring pp at the top, thus the
methods were already there. The funny thing is I deleted that line
before posting, thinking, "Awwww, it won't matter".

You have three options: explicitly require pp, disable the use of pp by
test/unit (Test::Unit::Assertions.use_pp = false), or change your code
so that it doesn't care. I'd favor the last option, since Ruby's dynamic
nature makes a method count extremely brittle. Perhaps you could
explicitly check for methods you expect to be there, or better yet,
whether the object you're testing #responds_to? the methods of interest?

HTH,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,253
Latest member
BlytheFant

Latest Threads

Top