getBundlePath returns None when running unit tests from the command line

Asked by Matthew Balvanz

I have been working with the Sikuli unit test framework successfully using by adding specific paths (e.g. "c:\scripts") to sys.path for module imports but using the built in function getBundlePath() would be far more convenient. However when I use getBundlePath() in a .sikuli and launch it from the command line it returns None instead of the package directory.

Example:

path = getBundlePath()

def setUp(self):
 print "setup run"

def tearDown(self):
 print "tearDown run"

def testSomething(self):
 print self.path

Prints the path to the .sikuli when I use the run button in the Sikuli IDE but using SIkuli-IDE.bat from the command line I get the following result:

[info] Sikuli vision engine loaded.
[info] Windows utilities loaded.
[info] VDictProxy loaded.
.setup run
None
tearDown run

Time: 0.003

OK (1 test)

Am I missing a step to use this function in this case?

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

try

self.path = getBundlePath()

since the IDE testrun feature wraps a class around your code, that is instantiated at runtime and available in self.

Revision history for this message
Matthew Balvanz (mkbalvanz) said :
#2

Using the self.path option seems to cause an error that prevents the script from running:

[info] Sikuli vision engine loaded.
[info] Windows utilities loaded.
[info] VDictProxy loaded.
[error] null

The IDE shows this error:

Exception in thread "MainThread" Traceback (most recent call last):
File " ", line 6, in
File " ", line 14, in unitTests
NameError: name 'self' is not defined

Not declaring self before you start declaring functions for unit tests seems to work fine. For instance:

mystring = "My String"
def testIt(self):
 print self.mystring

Prints the value of mystring in the IDE and command line.

Something I noticed today is that I get the same behavior in the IDE if I have not previously run any standard scripts using the Run button or the Run drop down. So it appears that it may be related to running the script as unit tests instead of running as a standard sikuli script.

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

Outside the def()'s self. cannot be used in unittest, since it is not defined and makes IDE to hang. A unittest script should only contain def()'s. Running a unittest script in a normal IDE run should do nothing except reporting syntax errors.

If you need additional variables in your subsequent tests, you may define them in setUp() using self.variable=value and reference them using self.variable later on.

Be aware, that e.g. the normal import does not work as expected on the script level, you have to repeat every needed import in every def(), were the respective module features are needed.

So putting all together, you can do the following:

def setUp(self):
 self.x = "xxx"
 import os
 self.path = os.path.join(getBundlePath(), "script_subfolder")
 self.myJoin=os.path.join # stores a reference to a module function
 print "setup run"

def tearDown(self):
 print self.x
 print "tearDown run"

def testSomething(self):
 print "in test:", self.myJoin(self.path,"some_script.sikuli")

Can you help with this problem?

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

To post a message you must log in.