Can I use Rhino Inside to unit test Rhino Python code?

Hi all, I’d like to fire off unit tests fairly frequently while developing rhino python code in VS code.

This involves two challenges

  1. What the best practice for sending python code into rhino?
  2. How should I best mock up user input?

It looks like Rhino inside could be an answer for both items - it handles reloading libraries, can build a headless rhino environment, and - as far as I understand it - can let me simulate user input (in the worst case I could mock Rhino.Input). It might even be possible to put everything in a nice clean docker imagine.

Questions include:

  • can we run rhino python scripts outside of rhino?
  • has anyone done this for the purpose of testing user facing rhino scripts?
  • if so, could they share the recipe or a repo that has appropriate text fixtures in place?

Thanks!

4 Likes

Hey Max,

I do face some similar issues with mocking user interaction in Rhino.
Did anyone find an answer for this in the last 4 years?

With kind regards,

Hajo

I run unittest within GhPython itself:

discovered_suite = unittest.TestLoader().discover(
                               start_dir = start_dir
                              ,pattern = '*test*.py'
                              )

unittest.TextTestRunner(o, verbosity=2).run(discovered_suite)

I haven’t tested it, but I can’t think why that wouldn’t work with RhinoPython.

In Grasshopper, the new CPython components could run Pytest and other tool too (just via the entry point in code, not the normal CLI). Combining either with importing the majority of the code as an compiled pure Python package (which can be developed directly in VS Code etc., or installed from an install script), and have a “reload module” component that deletes it from sys.modules. Using these together, a plug-in can support hot reloads.

It’s a bit involved, but I’ve also figured out how to create a workflow from the command line, including testing interactions between real Grasshopper components in code, sending the output back to the command line, and ending the process with an error on test failure.