Using Unit Test with SketchUp for Debugging & Testing
-
A friend of mine, Steve Baumgartner, showed me how to hook up Unit Testing to the SketchUp Ruby API and Ruby Console. See http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing for brief introduction and http://ruby-doc.org/stdlib-1.9.3/libdoc/test/unit/rdoc/index.html for Ruby version 1.9.3 classes and methods. The instruction I will provide here work for Ruby version 1.8.7 and hence for SketchUp 8.
The following are the instructions Steve originally sent me for purposes of explanation. However, the attached zip has already incorporated the changes Steve instructed me to make so you don't have to perform them. Simply place the Unit_test folder in the zip file in your Plugins directory and you are good to go.
Joe,
I think I have cracked the nut on getting the Ruby Unit Test library to run under SketchUp!!! It takes a bit of work to set things up the first time, but after that it is easy to add more tests. Here's what I did:
-
Create a subdirectory called Unit_test under your SketchUp Plugins directory.
-
From your Ruby installation subdirectory lib\ruby\1.8 (I would stick to a 1.8 version, not 1.9, but have had no issues with 1.8.7), copy the "test" subdirectory and all its contents, the "optparse.rb"
file, and the "optparse" subdirectory and all its contents into the Unit_test directory. Although it is possible to set up the load path to access the originals in the Ruby installation, I think it is wiser to make these copies so that files can be tweaked for SketchUp without risking damage to the main installation. -
Replace the file "testrunner.rb" in Unit_test\test\unit\ui\console with the attached version. This version fixes issues with output to the SKETCHUP_CONSOLE. If you want, you can delete the subdirectories other than console in Unit_test\test\unit\ui. They are for other forms of display, and I haven't been able to get any except the console to work within SketchUp (yet).
-
Read the file Unit_test\test\unit.rb to understand the general implementation of the unit test harness. Also read Unit_test\test\unit\assertions.rb to learn all of the variations of test assertions you can use.
-
When you code a unit test file, use the attached as an example of a few bits of fluff that you need to add to get the tests to run correctly inside SketchUp. Of course, each test method will need to have specific setup calls to get your test code into the desired test condition, and these will depend on what you are testing. Also, unless you get awfully complex, it may be necessary for a human to look at the SketchUp display to verify that something occurred correctly - the test harness can only verify that some code condition was satisfied, not that the drawing on the screen is right!
-
The Ruby toolbar works great for running your tests and rerunning when you fix a bug in a class you are testing. However, I have had issues with the test classes themselves seeming "sticky", requiring you to kill and restart SketchUp before modifications to your test suite are noticed.
-
The output appears on the SketchUp Ruby Console, but the test harness is pretty frugal with output, normally showing details only for tests that fail, so you don't have major issues with Console fill-up unless you have a lot of errors .
Try it and let me know how it works out for you!
Steve
--
S L Baumgartner
Fine Furniture
-
-
Thankyou! I'll go through this and set myself up for unit testing - it's what's been missing on my development workflow.
-
Joe,
Thanks for posting this. I was going to eventually, but got tangled up in other things and didn't get around to it/ I hope it is useful to others.
Steve
Advertisement