views:

53

answers:

2

Is there an easy ruby or rails command to get a list of all the tests in an application? I use Shoulda primarily, but if there's a ruby solution regardless of test suite that would work too.

A: 

I don't think there can be a one-solution-fits-all answer here: how tests are identified is going to be framework-dependent to a great extent. Once you're into frameworks like shoulda, it's near-enough impossible to identify all tests without executing them, consider the case where you have should requirements within a loop, for example.

Listing all tests as they're executed would be a matter of digging into the framework code for the point where the individual tests are identified/generated and extracting the information needed.

I'd be interested to learn if (and how) I'm wrong, though...

Mike Woodhouse
I know this much: you can specify that a specific test be run on the command line with something like "ruby -Ilib:test test/unit/user_test.rb -n test_user_is_created" and it will run the individual test. This is built into test unit, and even works on shoulda tests. It seems reasonable you'd be able to get the reverse. But I can't make that connection :)
Jaime Bellmyer
@Jaime - I can't see how you could produce a list of dynamically-generated tests, without executing the things (or doing a helluva lot of stuff to the test framework!). What problem are you trying to solve? Perhaps there's another way to attack it.
Mike Woodhouse
Hi Mike - I never said the list needed to be generated without running tests. In fact, the easiest option would be a command or plugin that gives you the option to print the tests off as you run them. As for the problem I'm trying to solve, it was literally to get a list of the tests. The solution I found and posted gets me that.
Jaime Bellmyer
+1  A: 

I have found a roundabout solution: the test_benchmarker plugin. You run your tests with an extra environment variable like so:

rake test BENCHMARK_TESTS=true

You're best off piping the output to a file, because it will be long. But it will give you a list of all the test classes and tests themselves, for both unit and functional. Obviously it gives you the benchmarking results too, which is a bonus.

Is there anything better? I guess this is pretty close, and the extra effort is so little, why NOT get the benchmark results too? RSpec has this feature built in, but maybe it'd be fun to write a version for Test::Unit.

Jaime Bellmyer