Using images from dictionaries in unit tests

Asked by Calle Rundgren

Hi,

Today I thought I should learn how to make unit tests. At current state I have written two different tests. One which contains the images direcly in the testfile and one which refers to the images as keys in a dictionary which is in another file.

Both tests are identical to the point where the images are stored. And none of them returns any Errors when doing a normal Run.

The problem is that only the test where the images is directly stored in the test are working.
How would I do in order to make the other test run aswell?
The script which got the images in a dictionary in another file is identical apart from where images are called. Like in the wait() below

In the dictionary test it looks like this: wait(imgSet.getImage("imageAsString")) Everything works when not in unit test mode so it could not be that the dict is imported wrong (imported as you swhown me yesterday. )

Here is the code:

def setUp(self):
    App.open(r"c:\Program\Firefox.exe")
    wait("image.png")

def tearDown(self):
    App.close(r"Firefox")
    wait("element on desktop")

def test_findGuiElementButton(self):
    if not exists("toolBar.png"): #just checks if firefox is in focus
        print("Not focus") #if not print "Not focus"
    if exists("theButton.png"): #check if a specific image, in this case button is visible
        return True
    elif not exists("theButton.png"):
        return False
        print("Does not exists") #if not visible print "does not exist"

Feels like I have been trying everything. The only positive thing this day is the sunny weather. :-)

Hope the sun shine above your heads aswell!

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
Calle Rundgren (c-rundgren) said :
#1

Forgot, additional information, When a test for the dictinary test is started the unit test just freezes. When aborting the test (ctrl + shift + C) this Error Message is displayed in teh sikuli IDE:

Exception in thread "MainThread" TypeError: hashCode(): self arg can't be coerced to java.lang.Enum

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

Using the test feature in the IDE is a bit tricky, when trying to use import, user defined functions to be used in the test cases or global variables.

On top, if any errors happen that are not assert exceptions, the test might hang (as in your case, see bug 803795).

So in the moment it is recommended to use Pythons unittest module yourself and run your tests as a normal Sikuli script, if you want to do more complex things. So you have full control on everything.

The problem in your case is the placement of the import statement: in the IDE tests, this cannot be used as usual:

The general workaround is:

- there is a utestsub.sikuli
- containing a subtest() function

def setUp(self):
    dir = "path-to-your-module-directory"
    if not dir in sys.path: sys.path.append(dir)

def test1(self):
    import utestsub
    utestsub.subtest()
    assert(True)

the import statement has to be placed in every testXXX, where functions/variables from the respective module are needed.
the managment of sys.path is implemented in setUp(), which is run before every test case, so you do not need it in every testXXX.

Revision history for this message
Calle Rundgren (c-rundgren) said :
#3

Thanks RaiMan. I will check this workaround out. I have already been checking pythons unittest out, but not in this perticular case so I would probably try it.

Have a nice day!

Revision history for this message
Calle Rundgren (c-rundgren) said :
#4

Thanks RaiMan, that solved my question.