[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

passing data to tests with test/unit

Matt Berney

3/28/2007 9:23:00 PM

I have a test case, based on the Test::Unit::TestCase. How does one pass
data to the test in such a way that the test can be run multiple times
with different input.

For example:

class TC_CreateOrder < Test::Unit::TestCase
def test_NewOrder
assert "order successful"
end
end

Now, I want to be able to generate a new order for various order types,
(TYPE1, TYPE2, TYPE3, etc.)

class TS_OrderTests
def self.suite
suite = Test::Unit::TestSuite.new
suite << TC_CreateOrder.suite # TYPE1 goes here
suite << TC_CreateOrder.suite # TYPE2 goes here
suite << TC_CreateOrder.suite # TYPE3 goes here
return suite
end
end

Test::Unit::UI::Console::TestRunner.run(TS_OrderTests)

Thanks in advance.

--
Posted via http://www.ruby-....

13 Answers

Brian Candler

3/29/2007 5:13:00 AM

0

On Thu, Mar 29, 2007 at 06:23:14AM +0900, Matt Berney wrote:
> I have a test case, based on the Test::Unit::TestCase. How does one pass
> data to the test in such a way that the test can be run multiple times
> with different input.
>
> For example:
>
> class TC_CreateOrder < Test::Unit::TestCase
> def test_NewOrder
> assert "order successful"
> end
> end
>
> Now, I want to be able to generate a new order for various order types,
> (TYPE1, TYPE2, TYPE3, etc.)
>
> class TS_OrderTests
> def self.suite
> suite = Test::Unit::TestSuite.new
> suite << TC_CreateOrder.suite # TYPE1 goes here
> suite << TC_CreateOrder.suite # TYPE2 goes here
> suite << TC_CreateOrder.suite # TYPE3 goes here
> return suite
> end
> end
>
> Test::Unit::UI::Console::TestRunner.run(TS_OrderTests)

If it's only one test method, you can factor it out in the class:

class TC_CreateOrder < Test::Unit::TestCase
def do_test(t)
.. process t
end

def test_type1
do_test(TYPE1)
end

def test_type2
do_test(TYPE2)
end

def test_type3
do_test(TYPE3)
end
end

Otherwise, how about:

class TC_CreateOrder1 < TC_CreateOrder
FIXTURE = TYPE1
end

class TC_CreateOrder2 < TC_CreateOrder
FIXTURE = TYPE2
end

class TC_CreateOrder3 < TC_CreateOrder
FIXTURE = TYPE3
end

(I'm not sure how you'd prevent the base test TC_CreateOrder being run)

Regards,

Brian.

Matt Berney

3/29/2007 3:42:00 PM

0

Thanks Brian,

This would work ok if there was only one test method and only 3 types.
Now let's say that I want to use this test technique multiple times,
with multiple test methods. And, the data input is over 100 items.
Duplicating this structure 100 times doesn't seem tenable.

What I am really looking for is a data-driven test method, such that I
can use the same test case and pass it different data. However, for
test execution and measurement purposes, it is preferable to record
these as separate tests.


Brian Candler wrote:
> On Thu, Mar 29, 2007 at 06:23:14AM +0900, Matt Berney wrote:
>> end
>> return suite
>> end
>> end
>>
>> Test::Unit::UI::Console::TestRunner.run(TS_OrderTests)
>
> If it's only one test method, you can factor it out in the class:
>
> class TC_CreateOrder < Test::Unit::TestCase
> def do_test(t)
> .. process t
> end
>
> def test_type1
> do_test(TYPE1)
> end
>
> def test_type2
> do_test(TYPE2)
> end
>
> def test_type3
> do_test(TYPE3)
> end
> end
>
> Otherwise, how about:
>
> class TC_CreateOrder1 < TC_CreateOrder
> FIXTURE = TYPE1
> end
>
> class TC_CreateOrder2 < TC_CreateOrder
> FIXTURE = TYPE2
> end
>
> class TC_CreateOrder3 < TC_CreateOrder
> FIXTURE = TYPE3
> end
>
> (I'm not sure how you'd prevent the base test TC_CreateOrder being run)
>
> Regards,
>
> Brian.


--
Posted via http://www.ruby-....

Ara.T.Howard

3/29/2007 4:51:00 PM

0

Brian Candler

3/29/2007 7:09:00 PM

0

On Fri, Mar 30, 2007 at 12:41:54AM +0900, Matt Berney wrote:
> This would work ok if there was only one test method and only 3 types.
> Now let's say that I want to use this test technique multiple times,
> with multiple test methods. And, the data input is over 100 items.
> Duplicating this structure 100 times doesn't seem tenable.
...
> > class TC_CreateOrder1 < TC_CreateOrder
> > FIXTURE = TYPE1
> > end
> >
> > class TC_CreateOrder2 < TC_CreateOrder
> > FIXTURE = TYPE2
> > end
> >
> > class TC_CreateOrder3 < TC_CreateOrder
> > FIXTURE = TYPE3
> > end

But this was just an example; if this works then you can do it dynamically.

How about something like:

$test_classes = []

fixtures = [TYPE1, TYPE2, TYPE3]
fixtures.each do |f|
klass = Class.new(TC_CreateOrder)
klass.const_set(:FIXTURE, f)
$test_classes << klass # to prevent garbage collection
end

Matt Berney

3/30/2007 3:44:00 PM

0

Thanks to everyone who posted responses. Through a combination of your
feedback and experimentation, here is the solution I came up with:

class TC_CreateOrder < Test::Unit::TestCase
def test_NewOrder
orderTypeList = File.open('orders.yaml') { |yf| YAML::load(yf) }
orderTypeList.each { |type|
# perform the test
assert "order successful"
@_result.add_run
end
end


The trick was the last line "@_result.add_run". By adding this line,
each time through the loop increments the test count such that each
order is a separate test case.


Nasir Khan wrote:
> You could write a private method in your test class which is called from
> a
> public test method which in turn gets the data from a YAML file or some
> other data source.
>
> e.g
>
> ------------------------------------------------------
> require 'test/unit'
>
> class MyTest < Test::Unit::TestCase
>
> def test_public
> 3.times { |i| _test_private(i) } # get your input data here before
> calling private method
> end
>
> def _test_private(arg)
> puts "Now testing with #{arg}"
> assert(true)
> end
>
> private :_test_private
> end
>
> -------------------------------
> Loaded suite test
> Started
> Now testing with 0
> Now testing with 1
> Now testing with 2
>
> - nasir


--
Posted via http://www.ruby-....

Phlip

4/1/2011 4:51:00 PM

0

On Apr 1, 6:31 am, PaxPerPoten <P...@USA.com> wrote:

> But it is still a 10 to 1 majority backing the Libyan leader. Now that
> is true Democracy.
> Oboma can murder all he wants to by bombing, but the odds are in favor
> of a popular
> leader in Libya. Can any one say:* War Crimes?*

And you support the terrorist who ordered the bombing of Pan Am 103?

Typical of you. How about you go support Osama bin Laden, too?

mobius_bb_pretzel

4/1/2011 4:55:00 PM

0

On Apr 1, 12:50 pm, Phlip <phlip2...@gmail.com> wrote:
> On Apr 1, 6:31 am, PaxPerPoten <P...@USA.com> wrote:
>
> > But it is still a 10 to 1 majority backing the Libyan leader. Now that
> > is true Democracy.
> > Oboma can murder all he wants to by bombing, but the odds are in favor
> > of a popular
> > leader in Libya. Can any one say:* War Crimes?*
>
> And you support the terrorist who ordered the bombing of Pan Am 103?
>
> Typical of you. How about you go support Osama bin Laden, too?


You mean the Pan Am 103 orchestrated by the CIA as a false flag to set-
up Kadafy?

And do you also refer to the Osama bin Laden who was financed by the
CIA?


You probably support terrorists like the CIA.

Phlip

4/1/2011 11:16:00 PM

0

On Apr 1, 9:55 am, Möebius Pretzel <mobius_bb_pret...@yahoo.com>
wrote:

> > And you support the terrorist who ordered the bombing of Pan Am 103?
>
> You mean the Pan Am 103 orchestrated by the CIA as a false flag to set-
> up Kadafy?

Jesus Christ you Truthers have, like, ALWAYS been with us.

Next we'll hear the village of Lockerbie was wired for controlled
demolition...

(And no I do NOT support the Cocaine Industry of America - I happen to
know first-hand THAT conspiracy theory is true!)

PaxPerPoten

4/2/2011 8:55:00 AM

0

On 4/1/2011 11:50 AM, Phlip wrote:
> On Apr 1, 6:31 am, PaxPerPoten<P...@USA.com> wrote:
>
>> But it is still a 10 to 1 majority backing the Libyan leader. Now that
>> is true Democracy.
>> Oboma can murder all he wants to by bombing, but the odds are in favor
>> of a popular
>> leader in Libya. Can any one say:* War Crimes?*
> And you support the terrorist who ordered the bombing of Pan Am 103?
>
> Typical of you. How about you go support Osama bin Laden, too?


That was never proven! In fact reparations were blackmailed out of
Libya with an
economic embargo that was killing its citizens and children. That is why
the Bushies
thought it would also work in Iraq. In Iraq it killed thousands of
children, elderly and
more by cutting needed medical supplies, food and other necessities of
life. There
are now people in our own government that now believe that More Saudi
Citizens
were the true killers. Just like you to get millions killed by jumping
to ignorant conclusions.
Even if the Libyan leader did order that..What is your excuse to murder
thousands of
innocent citizens? Using your reasoning we should vaporize Israel for
the slaughter
of the USS Liberty. Which really makes more sense then what we are
doing now.



--
X-No-Archive: Yes

PaxPerPoten

4/2/2011 8:56:00 AM

0

On 4/1/2011 11:50 AM, Phlip wrote:
> On Apr 1, 6:31 am, PaxPerPoten<P...@USA.com> wrote:
>
>> But it is still a 10 to 1 majority backing the Libyan leader. Now that
>> is true Democracy.
>> Oboma can murder all he wants to by bombing, but the odds are in favor
>> of a popular
>> leader in Libya. Can any one say:* War Crimes?*
> And you support the terrorist who ordered the bombing of Pan Am 103?
>
> Typical of you. How about you go support Osama bin Laden, too?

Osama Bin Laden has been dead since 2002.


--
X-No-Archive: Yes