unittest and pycharm debugging

Asked by rod

Hi all,

I'm using Sikuli 1.0.1 on Windows7/Java7 and Pycharm as IDE for debugging with :
- jython2.5.3 as Pycharm Jython interpreter
- sikuli-java.jar in a local (pycharm) CLASSPATH env variable
- sikuli-java.jar set into the sys.path
- unittest2 installed (as explained here : https://answers.launchpad.net/sikuli/+question/192876) and set into the sys.path

My project is made up of a main.sikuli\main.py that import unittest2 suites to be run (actually .sikuli script...).

So in my main.py I just :

import unittest2
def suite():
 modules_to_test = ('mySuite',)
 alltests = unittest2.TestSuite()
 for module in map(__import__, modules_to_test):
  alltests.addTest(unittest2.findTestCases(module))
 return alltests

if __name__ == '__main__':
 unittest2.main(defaultTest='suite', exit=False, verbosity=2)

where mySuite.sikuli\mySuite.py is :

from sikuli import *
import unittest2

class mySuite(unittest2.TestCase):

  @classmethod
 def setUpClass(cls):
   cls.app = App(r"C:\Program Files (x86)\myapp")
   cls.app.open()
   cls.mainWindow = wait("window.png", 20)
   cls.mainWindow.highlight(3)

 def setUp(self):

 def test_bool(self):
    assert True == True

 def tearDown(self):

  @classmethod
 def tearDownClass(cls):
  cls.app.close()

Now, when I run the main.py everything works fine (myapp is open and the mainWindow found successfully).
But when I'm in debugging I get the following error :

Traceback (most recent call last):
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 3.4.1\helpers\pydev\pydevd.py", line 1733, in <module>
    debugger.run(setup['file'], None, None)
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 3.4.1\helpers\pydev\pydevd.py", line 1226, in run
    pydev_imports.execfile(file, globals, locals) # execute the script
  File "C:/POC/main.sikuli/main.py", line 30, in <module>
    unittest2.main(defaultTest='suite', exit=False, verbosity=2)
  File "C:\jython2.5.3\Lib\site-packages\unittest2\main.py", line 82, in __init__
    self.module = __import__(module)
ImportError: No module named __main__

I do not understand what this __main__ module is ?

Please note that, in order to make debugging work in Pycharm, I was obliged to comment out the line 10 of the Sikuli.py (#import __main__) as explained in the https://answers.launchpad.net/sikuli/+question/253906.

Is this issue a side effect of this comment ?
Please, have you any idea ?

Thanks a lot,
Rod

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

I have no idea, but you should not name your stuff main.sikuli/main.py, but somehow different.

Is this issue a side effect of this comment ?
I do not think so, but you might do some experiments.

In any case, you should go step by step and check with a basic setup without any specific features (no unites, no Sikuli), wether the debugging works at all.
then you might add additional stuff step by step (1st unit test, 2nd Sikuli and vice versa) to find out, at what stage it crashes.

Can a simple main script having some Sikuli features be debugged?

Revision history for this message
rod (rodhiguain) said :
#2

Hi RaiMan,

I changed my main stuff to another name ('test.py') and tried all steps you suggested me.
So, let me sum up the outcome of this analysis :

1) pycharm debugging works fine with sikuli (no unittest2)
2) pycharm can import unittest2 (extracted as described here : https://answers.launchpad.net/sikuli/+question/253906) without any problems
3) pycharm debugging works fine even when I call the suite mySuite directly (import mySuite ...)

The problem raises (only in debugging !) when I import mySuite with the following :

 unittest2.main(defaultTest='suite', exit=False, verbosity=2)

that calls the method suite() defined in my test.py

Traceback (most recent call last):
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 3.4.1\helpers\pydev\pydevd.py", line 1733, in <module>
    debugger.run(setup['file'], None, None)
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 3.4.1\helpers\pydev\pydevd.py", line 1226, in run
    pydev_imports.execfile(file, globals, locals) # execute the script
  File "C:/POC/test.sikuli/test.py", line 37, in <module>
    unittest2.main(defaultTest='suite', exit=False, verbosity=2)
  File "C:\POC\unittest2\main.py", line 82, in __init__
    self.module = __import__(module)
ImportError: No module named __main__

Revision history for this message
RaiMan (raimund-hocke) said :
#3

So it seems a problem with unittest2.main()

You might try with Jython 2.7beta, which says to be at Python language level 2.7.
Hence you would not need unittest2 (which is a back port of 2.7 unittest for usage with Python language level < 2.7), but simply use the built-in unittest.

Revision history for this message
rod (rodhiguain) said :
#4

I installed Jython 2.7 beta3 and used unittest (built-in) but I got the same error message :

Traceback (most recent call last):
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 3.4.1\helpers\pydev\pydevd.py", line 1733, in <module>
    debugger.run(setup['file'], None, None)
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 3.4.1\helpers\pydev\pydevd.py", line 1226, in run
    pydev_imports.execfile(file, globals, locals) # execute the script
  File "C:/POC/test.sikuli/test.py", line 37, in <module>
    unittest.main(defaultTest='suite', exit=False, verbosity=2)
  File "C:\jython2.7b3\Lib\unittest\main.py", line 77, in __init__
    self.module = __import__(module)
ImportError: No module named __main__

Revision history for this message
RaiMan (raimund-hocke) said :
#5

Ok, then this is a general issue in the combination of unittest.main() and PyCharm debugging.

To get on the road, you should not use unit test.main().
There are other available methods to setup and run suites using unittest.

I will stop at this point, since this problem is too specific and too far away from my Sikuli perspective.
Hope you understand.

Revision history for this message
rod (rodhiguain) said :
#6

ok thanks any way

Can you help with this problem?

Provide an answer of your own, or ask rod for more information if necessary.

To post a message you must log in.