Use Ruby's Unit-testing functionality in Sketchup
-
I've been playing around with ruby scripts in Sketchup for some time now but want to start test harness my code. Ruby has the build in unit testing framework, so I was wondering whether anyone has hands on experience with applying this within the boundaries of Sketchup. the presentation http://www.playuptools.com/csroby/media/GDC_Sketchup_09.pdf mentions it should work (slide 31 & 32), but so far, I've been unsuccessful to get it up and running.
-
I've not used TestUnits with SU Ruby. But that's a nice presentation you linked to.
-
Does the framework rely on compiled libraries? If so, you may end up having to compile it just for use in SketchUp since SU is still using version 1.8.0. Still, several plug-ins are using the Ruby win32API.so and win32ole.so libraries copied from the Ruby 1.8.6 distribution, but other libraries don't play as nicely for whatever reason.
If the framework is pure Ruby, you should be able to use it as-is. What do you mean by unsuccessful? What have you done so far?
-
Another thought....
Ruby ver 1.9.1 comes with a MiniTest library. Maybe it may work better than the fullblown edition.
I don't see it with ver 1.8.6, don't knowif it's in 1.8.7 __ anyone?
EDIT: (Just unzipped v1.8.7-p72, and it's not there. Looks like it's a v1.9.x addition. So the question is will it work in the v1.8.x branch?) -
Guys,
I got in contact with the author of the slide deck I mentioned. He told me that the unit testing functionality of Ruby is not part of the SketchUp installation (on PC, it is on Mac), so that I need to copy the unit testing folders from ruby into the plugin folder of sketchup.
this is also mentioned on the FAQ pages of SketchUp (http://code.google.com/apis/sketchup/docs/faq.html)
What parts of Ruby comes with SketchUp?
Only the minimal "core" Ruby modules come with SketchUp on the PC. On the Mac, the full standard Ruby install is included with the OS.I need a Ruby module (such as CGI) that doesn't come with SketchUp. What do I do?
You could either require that your plugin users install the latest version of Ruby on their computer (it comes standard on the Mac, by the way) or you could distribute the needed .rb files in a subdirectory that you add to the /Plugins directory when they install your script.I will try this out later on today and keep you tuned on how it went.
Lieven
-
So I copied the test and underlying folders from my Ruby 1.8 installation to the Plugins folder of Sketchup.
I bumped into another load error, so I also copied the optparse.rb file to the Plugins folder. Now my code snippet is loading fine. I'll come back with more news, once I've done some real testing.L.
-
@lbaeyens said:
You could either require that your plugin users install the latest version of Ruby on their computer
hmm.. I don't think that sounds like a good solution. As a user I'd rather not have to install a full separate Ruby installation.
-
I don't know why on Mac SU comes with the full Ruby set while on PC only the "core" Ruby components are deployed.
-
Yea, there are many questionmarks regarding the SU Ruby implementation...
-
@lbaeyens said:
I don't know why on Mac SU comes with the full Ruby set while on PC only the "core" Ruby components are deployed.
because OSX, not SU, has Ruby preinstalled by default (among other nice things).
-
I'm interested in learning how unit testing can improve the development process for plugins. I've never felt I needed it, but I can't really justify that feeling because I really don't know what unit testing is all about.
My plugins are simple and straight-forward. I am making an assumption that testing is designed more for software that is too large to understand as a whole, and so it is divided into smaller pieces (units.) Then the tests make sure you're smaller units don't break as you work on the other parts.
-
I got it working! thanks to Scott (the author of the slide deck remember)
the issue was that the stdout which is the standard output stream used by the TestRunner is not supported in SU, so you need to implement a SU specific output stream SketchupConsoleOutput in this case
a second issue that had to be resolved was the reference to Test::Unit::UI::NORMAL.
but now it's up and running. copy the code into a .rb file and give it a try.Test driven SU, here we come!
require 'test/unit/ui/console/testrunner'
require 'test/unit'
require 'sketchup'
Sketchup.send_action "showRubyPanel:"module Sketchup
class Model
def dumpr
return true
end
end
endclass SketchupConsoleOutput
def puts s
print s.to_s + "\n"
end
def write s
print s
end
def flush
#nop
# The testrunner expects to be able to call this method on the supplied io object.
end
endclass ModelDumpTest < Test::Unit::TestCase
def test_dumpexistingmodel
assert_equal true, Sketchup.active_model.dumpr
end
enddef runTests
runner = Test::Unit::UI::Console::TestRunner.new(ModelDumpTest, Test::Unit::UI::NORMAL, SketchupConsoleOutput.new)
runner.start
endUI.menu("PlugIns").add_item("TestRunner") { runTests }
-
test driven development for sketchup plugins
well, I come out of the classic software development space and apply TDD for the last 10 years. when I started experimenting with SU plugin development, I started without TDD, but after a while I felt like developing "naked". once you're used to the TDD way, you won't go back to the non TDD way. and Ruby has embedded TDD capability, so no excuse!
I'm glad I got it up and running, I'll keep you tuned on my TDD adventures in SU Ruby -
You can also start sketchup from the command line as follows:
$ sketchup.exe > log.txt
And then output goes to the file as long as you do not open the Ruby Console. The Ruby Console slows SU down and is less convenient when there is a lot of output.
So is the example code you posted an actual test you would write, or is it for testing the setup?
-
the code snippet is not an actual test, but just to prove it works. now the real work starts. thanks for the hint on directing the SU output.
Advertisement