Test::Unit - Ruby Unit Testing Framework Questions

Recently, we collected several (29) Selenium Ruby scripts and put them
into a unit test that runs whenever there’s a build of our product.
That’s been reasonably successful, but our developers would like more.
Specifically, they would like us to include asserts inside of our
scripts.

I see two challenges to this. First, I need guidance on when to code an
assertion. I have some ideas, but like all the open source
documentation I see so far, the emphasis seems to be on syntax rather
than semantics.

Second, I’m a little concerned about inconvenience when I’m debugging an
individual test in the unit test package. The following are real code
examples from my environment.

Run the campaign canvas script under IE:

Today:
driver $ ca
(11 characters)

With Test::Unit:
selenium_tests.rb --name test_create_canvas
(45 characters, and I cannot pick the browser)

Driver.rb invokes the browser of choice ($ in this case, the default is
Firefox if it cannot make out what you asked for), then runs the chosen
script (ca, chosen out of an if/elsif stack with case insensitivity and
matching to the string supplied, even if it’s abbreviated).

With Test::Unit: I lose the ability to pick browser and have to do a
lot more typing to pick a test, and that typing has to be spot on. I
can help the latter problem with some careful re-naming.

I had thought to turn driver into a unit test by surrounding it with the
basic code, but I cannot figure out how to get to the ARGV array once I
do that, so I cannot read the command line.


Bill M. | Test Automation | office: (703) 584-2768
| fax: (703) 584-2751
[email protected] | Eloqua www.eloqua.com | 1921 Gallows Road,
Suite 250, Vienna, VA 22182

Hello Bill,

On Thu, Oct 22, 2009 at 9:02 AM, Bill M. [email protected]
wrote:

With Test::Unit: Â I lose the ability to pick browser and have to do a
lot more typing to pick a test, and that typing has to be spot on. Â I
can help the latter problem with some careful re-naming.

I had thought to turn driver into a unit test by surrounding it with the
basic code, but I cannot figure out how to get to the ARGV array once I
do that, so I cannot read the command line.

I am also trying to get more familiar with Test::Unit. In my case I
have some scripts that operate on a sqlite3 database. Since a sqlite3
database is just a file with some arbitrary name, I needed to be able
to pass in the name of the file to the testing script. The solution I
used was this construct:

require 'test/unit' require 'sqlite3' require 'lib/sample.rb'

$dbfile = ARGV[0] || “sample.db”
exit unless File.exist?($dbfile)

class TestSample < Test::Unit::TestCase

In this way I was able to test my sample.rb code using the foo.db like
this:

$ ./tests/test_sample.rb foo.db

and if I wanted to test it with the bar.db, I could do this:

$ ./tests/test_sample.rb bar.db

And if I don’t specify a file, it defaults to “sample.db”.

I don’t know if that is the “right way” to do it, but it’s working for
me thus far.

Let us know what you’ve tried and what works (and what doesn’t) for you.

Regards,

  • Robert

Bill M. wrote:

Recently, we collected several (29) Selenium Ruby scripts and put them
into a unit test that runs whenever there’s a build of our product.
That’s been reasonably successful, but our developers would like more.
Specifically, they would like us to include asserts inside of our
scripts.
[…]

You probably want to use Cucumber and Webrat. I would also recommend
considering RSpec instead of Test::Unit.

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]