python unittest discover in SikuliX

Asked by arwan.khoiruddin

I know that in normal python, to discover python scripts for test

cd python_project
python -m unittest discover
I also know that we can use unittest in SikuliX.

I want to use unittest auto discovery feature in sikulix. I already put this inside my main sikulix script:

def suite():
    return unittest.TestLoader().discover(
        start_dir = ".",
        pattern = 'test_*.py'
    )
Now the problem is, I don't know how to call the auto discovery feature in sikulix. I run it using this

/Applications/Sikulix.app/run -r testdiscovery.sikuli
But it does not discover the files with the pattern defined (test_*.py).

So how should I run sikulix so I can call the unittest auto discovery feature?

Thank you for your kind helps.

Question information

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

no special thing in SikuliX. It is simply Python language.
The Interpreter though is Jython (Java based) and has some restrictions with external C-based modules.
... but the standard should be supported.

I just checked: In SikuliX 1.1.0 discover() is available (Python language version 2.7)

unittest.TestLoader().discover(
        start_dir = ".",
        pattern = 'test_*.py'
    )

just returns a test-suite, that can be used normally.

Revision history for this message
arwan.khoiruddin (arwan-khoiruddin) said :
#2

Thanks for your quick response, RaiMan.

Revision history for this message
tranquillo (rob-tranquillo) said :
#3

I try to use python unittest not to test my own functions, but to organize the sikuli tests as unit test and serve after finish all tests a unittest.xml. To make it most convenient for all test-writers i want to organize all sikuli tests under the subfolder "tests" and the unittest discover()-function should collect all available test into the test-suite.

But, it seems, that sikuli and unittest work not very well together.

This is my directory structure:

unittest_boilerplate.sikuli
|-- unittest_boilerplate.py
|-- tests
| |-- test_progStart
| | |-- __init__.py
| | |-- test_progStart.py

The boilerplate script contains mostly the discover( )-function to get the tests from the subfolder "tests". The "__init__.py" ist just an empty file that is needed for the special kind of the discover() function -> http://stackoverflow.com/questions/12674167/python-unittest-discovery-with-subfolders

Like this structure as above discover() work very well and collect the tests. But for the sikulix-ide its necessary that the subfolder "test_progStart" has the ".sikuli" suffix, wich is a problem for unittest:

progStart.sikuli.test_programStart (unittest.loader.ModuleImportFailure) ... ERROR

======================================================================
ERROR: progStart.sikuli.test_programStart (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError: Failed to import test module: progStart.sikuli.test_programStart
Traceback (most recent call last):
File "C:\sikuli\sikulix.jar\Lib\unittest\loader.py", line 252, in _find_tests
module = self._get_module_from_name(name)
File "C:\sikuli\sikulix.jar\Lib\unittest\loader.py", line 230, in _get_module_from_name
__import__(name)
ImportError: No module named sikuli
----------------------------------------------------------------------

Is there any solution for this, or a workaround to use directorys as modules in unittest together with sikulix?

Thanks
Rob

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

@tranquillo
... yes, to do it this way, is not possible currently with Sikuli(X) IDE, since it is not possible, to edit plain Python files.

The reason behind:
A Sikuli(X) script normally is a package with a .py file and associated images, that are used in the script.

Solution in your case:
- using the SikuliX IDE capture ALL needed images in ALL tests in one images.sikuli script
- have a naming convention for your images and preferably in the images.sikuli use something like
imageFooBar = "imageFooBar.png" (special capture support for that in latest SikuliX)
- in boilerplate take care, that the folder containing the images.sikuli is in sys.path
- in every test have:
from sikuli import *
from images import *
- then use in your test:
click(imageFooBar)

Of course you have to use some Python aware editor, to manage all your test....py (e.g. IntelliJ's PyCharm).