Using unittest in Rhino python not possible?

Dear all,

I encountered the same problem as Katsu, but is there no answer since August?

I noticed that there is a unittest folder in C:\Program Files (x86)\Rhinoceros 5\Plug-ins\IronPython\Lib

But somehow, when I call my standard line

if __name__ == '__main__':

I also get the error

Message: No module named __main__

Anybody with an idea?

Kind regards

Filip Rooms

Continuing the discussion from Error when using unittest module in PythonScript:

 import unittest
    
    class MyClassTestCase(unittest.TestCase):
        def setUp(self):
            pass
        def tearDown(self):
            pass
    
        def testFooMethod(self):
            pass
    
    if __name__ == "__main__":
        unittest.main()

---
    Message: No module named __main__
    
    Traceback:
      line 75, in __init__, "C:\Program Files\Rhinoceros 5.0 (64-bit)\Plug-ins\IronPython\Lib\unittest\main.py"
1 Like

Hi,
How about this?

suite = unittest.TestLoader().loadTestsFromTestCase(MyClassTestCase)
unittest.TextTestRunner(verbosity=2).run(suite)
1 Like

Thanks, that seems to work!!

Could you explain me what was wrong with my approach and why this longer function call to the test suite was needed then?

1 Like

suite = unittest.TestLoader().loadTestsFromTestCase(MyClassTestCase)
unittest.TextTestRunner(verbosity=2).run(suite)

I believe this solution doesn’t completely solve the problem raised by filip_rooms.
This will run the unittest properly for the first time it executes. However, after the second time the test time becomes 0.00s and will return the same result as the first run. This is true even when you modify your test file. My guess is that the cache doesn’t get fully refreshed until you close the Rhinoceros software.
My temporary solution at the moment is to reopen my software everytime I want to execute the test program. However this is not efficient and not time productive.

Does anyone having the same problem?
I understand that this is an old topic, however unittest is such a crucial tool for any programmer.
Any help or advice is highly appreciated,

Tadashi

I have been fiddling more on this topic and it seems to be that this error related to cache occurs when I have unit test as a separate file (I believe this is the right way of creating test file). When I have test file embeded within the same file then unit test functions fine. Maybe the problem has to do with the import and not unittest.
Again any help is appreciated.

Hi,
Does reloading the module work for you?
https://docs.python.org/2.7/library/functions.html#reload

1 Like

Hi Alain,
Thank you. It works.
I reloaded the test file and it works like a charm.