# Create dynamic tests for Test::Unit

**URL:** <https://www.ruby-forum.com/t/create-dynamic-tests-for-test-unit/48431>\
**Category:** Ruby\
**Created:** [November 20, 2005, 3:36pm UTC](https://www.ruby-forum.com/t/create-dynamic-tests-for-test-unit/48431 "2005-11-20T15:36:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![achim.domma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3d9bf3/32.png) [@achim.domma](https://www.ruby-forum.com/u/achim.domma)\
**Post date:** [November 20, 2005, 3:36pm UTC](https://www.ruby-forum.com/t/create-dynamic-tests-for-test-unit/48431/1 "2005-11-20T15:36:00Z")

</div>

Hi,

I have some experiences in generic programming in python and .Net but  
I’m quite new to ruby. I want to test a web app using watir. There are  
many forms with similar structure which have to be filled with data. So  
my idea was to create a YAML file which holds the location of a form,  
the type of the form and the data to be filled in.

My problem is, that I don’t see on how to create dynamicaly a single  
test for each entry in the yaml file!? Am I not yet thinking in ruby?  
Could somebody guide me to the right direction on how to solve this  
task?

regards,  
Achim

---

<div class="post-metadata">

**Author:** ![achim.domma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3d9bf3/32.png) [@achim.domma](https://www.ruby-forum.com/u/achim.domma)\
**Post date:** [November 20, 2005, 5:25pm UTC](https://www.ruby-forum.com/t/create-dynamic-tests-for-test-unit/48431/2 "2005-11-20T17:25:40Z")

</div>

Achim D. (SyynX Solutions GmbH) schrieb:

> I have some experiences in generic programming in python and .Net but  
> I’m quite new to ruby. I want to test a web app using watir. There are  
> many forms with similar structure which have to be filled with data. So  
> my idea was to create a YAML file which holds the location of a form,  
> the type of the form and the data to be filled in.
> 
> My problem is, that I don’t see on how to create dynamicaly a single  
> test for each entry in the yaml file!? Am I not yet thinking in ruby?  
> Could somebody guide me to the right direction on how to solve this task?

Hallo Achim,

maybe this example gets you going:

require “test/unit”

DynamicTest = Struct.new :name, :expected, :actual

DYNAMIC\_TESTS = [  
DynamicTest.new( “test\_one”, 123, 123 ),  
DynamicTest.new( “test\_two”, 125, 123 ),  
DynamicTest.new( “test\_three”, 127, 127 ),  
]

Class.new Test::Unit::TestCase do  
DYNAMIC\_TESTS.each do |t|  
define\_method t.name do  
assert\_equal t.expected, t.actual  
end  
end  
end

Running this, I get

Loaded suite C:/tmp/r  
Started  
…F  
Finished in 0.047 seconds.

```
 1) Failure:

```

test\_two()  
[C:/tmp/r.rb:15:in `test_two' C:/tmp/r.rb:14:in`test\_two’]:  
\<125\> expected but was  
\<123\>.

3 tests, 3 assertions, 1 failures, 0 errors

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![achim.domma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3d9bf3/32.png) [@achim.domma](https://www.ruby-forum.com/u/achim.domma)\
**Post date:** [November 21, 2005, 12:01pm UTC](https://www.ruby-forum.com/t/create-dynamic-tests-for-test-unit/48431/3 "2005-11-21T12:01:47Z")

</div>

Pit C. wrote:

> maybe this example gets you going:

Thanks, that’s what I was looking for. Seems like I have to learn much  
more ruby! 😉

regards,  
Achim
