# List tests with test/unit

**URL:** <https://www.ruby-forum.com/t/list-tests-with-test-unit/110160>\
**Category:** Ruby\
**Created:** [July 23, 2007, 9:33pm UTC](https://www.ruby-forum.com/t/list-tests-with-test-unit/110160 "2007-07-23T21:33:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [July 23, 2007, 9:33pm UTC](https://www.ruby-forum.com/t/list-tests-with-test-unit/110160/1 "2007-07-23T21:33:02Z")

</div>

Test::Unit is quite useful. I have a test class TC\_BasicAcceptanceTest  
with the following tests:

test\_001\_firstTest  
test\_002\_secondTest  
test\_003\_thirdTest

…

You get the picture.

Now, the ruby bat.rb -n is really useful. It allows the  
execution of a single test within the test case. For running tests (and  
debugging) on the command line, it would be useful to list all the tests  
in the suite. Then, one could use the -n flag to run the one of  
interest.

For example:

% ruby bat\bat.rb -l

TC\_BasicAcceptanceTest  
test\_001\_firstTest  
test\_002\_secondTest  
test\_003\_thirdTest

% ruby bat\bat.rb -n test\_002\_secondTest

Now the question…  
If one wanted to modify the autorunner.rb, how would one iterate through  
the tests in the test case? This code only lists the TC name. How does  
one list the individual test cases?

o.on(’-l’, ‘–list’, ‘List the tests to run.’){  
@collector[self].tests.each { |tc| puts “#{tc.name}” }  
exit  
}

---

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [July 23, 2007, 9:54pm UTC](https://www.ruby-forum.com/t/list-tests-with-test-unit/110160/2 "2007-07-23T21:54:03Z")

</div>

I’m not sure what @collector has exactly, but this is what you can do  
(adapt  
to fit with collector as needed)

test\_methods = MyTestClass.instance\_methods.grep(/^test\_/)

if “tc” is an instance of the test class, then simply do:

tc.methods.grep(/^test/)

Jason
