HTML reports shows pass even if the test Fails in Sikuli

Asked by Vishal singh on 2018-01-31

I am new to sikuli and currently automating my Desktop application
I am writing a script to check if the image is present on the application or not and generating the HTML report
But the report always shows the tests pass even if i close the application to check if it shows fail or not

Please tell me how to solve this error
Thank you in advance

Here is the code:
import os
import HTMLTestRunner
import unittest
import sys
import logUtils
logger = logUtils.createLog(r'C:\Tools\td5_gui_automation\Test_logs\toolbar_test.log')

class Util(unittest.TestCase):

    def setUp(self):
        # setup the test environment needed to execute the tests in this function
            TD5 = os.popen(r"C:\Tools\TD5\start_td5_adas_net.lnk")
            if exists("1516275912874.png"):
      "TD5 started")
                logger.error('Unable to launch TD5')
        except Exception, e:

    def tearDown(self):
        # After completion of the tests tear down exits the application
            if exists("1516275912874.png"):
      "Exiting TD5")
                logger.error('Unable to exit TD5')

        except Exception:
            print Exception

    def test_newProjectButton(self):
  "Checking new project button on toolbar")
            if exists("1516275912874.png"):
      "Clicked on new button on toolbar")
                if exists(Pattern("1517330266749.png").similar(0.90)):
          "New project dialogue successfully started")
                    assert True
                    logger.error("Unable to start New project import wizard")
                    assert False
                logger.error("Unable to click on new button in toolbar")

        except Exception, e:

suite = unittest.TestLoader().loadTestsFromTestCase(Util)
outFile = open(r"C:\Tools\td5_gui_automation\Test_reports\toolbar_test.html", "w")
runner = HTMLTestRunner.HTMLTestRunner(stream=outFile, title='test report', description='Testing all toolbar icons on TD5 GUI' )

HTML report:
It shows pass in the report and when i click pass it shows me this

pt1.1: Traceback (most recent call last):
  File "C:\Tools\td5_gui_automation\SikuliX\sikulix.jar\Lib\logging\", line 874, in emit
    stream.write(fs % msg)
  File "C:\Tools\td5_gui_automation\SikuliX\sikulix.jar\Lib\logging\", line 874, in emit
    stream.write(fs % msg)
ValueError: I/O operation on closed file
Logged from file, line 18
.... repeated

Question information

English Edit question
Sikuli Edit question
No assignee Edit question
Solved by:
Vishal singh
Last query:
Last reply:
RaiMan (raimund-hocke) said : #1

apparently you have a problem with the logger feature when running the scripts in the IDE.

Try to run your script from commandline.

BTW: your concept is a little bit weird:
You are using UnitTest/HTMLTestRunner, but there is not one assert, to report what's going wrong in a test, but instead you use the logging feature to report whats going wrong.

In setup you should simply exit, if the tests cannot be run, same in tearDown.

RaiMan (raimund-hocke) said : #2

... and there is some misunderstanding about tests pass/fail:
If in a test a not caught exception happens, the test is ended as error (neither pass nor fail).
If anything happens in the test, that does not break with an exception, the test will pass nevertheless.
Only assert yielding a result False will fail the test - but you do not have any asserts in your test!

Vishal singh (vishalsingh5900) said : #3

Thanks for the reply RaiMan
Can you show me an example like a code snippet what i am doing wrong exactly
I want to create test report as well as test log what i am doing after each step

RaiMan (raimund-hocke) said : #4

Generally you should think about using RobotFrameWork. if it is about testing with SikuliX.

There is a wrapping library:

If you want to stick with your setup:
Use the SikuliX user logging - is much simpler:

If I would do it (no logging):

setUp/tearDown should handle any bad situation and exit if it does not make sense to continue

   def setUp(self):
       TD5 = os.popen(r"C:\Tools\TD5\start_td5_adas_net.lnk")
       if not exists("TD5_ready.png", 30):
            print'Unable to launch TD5'
            exit(1) # makes no sense to continue

    def tearDown(self):
        if exists("TD5_ready.png"):
            if not waitVanish("TD5_ready.png", 30):
                print'Unable to stop TD5'
                exit(1) # makes no sense to continue
                print'TD5 not visible'
                exit(1) # makes no sense to continue

.... only one aspect per test

    def test_ClickNewProjectButton(self):
 #### see below
          if exists("TD5_ready.png"): ### not needed, setup has done it or exited!
                if exists(Pattern("1517330266749.png").similar(0.90), 5):
                    #### click("1517330523887.png") does not make sense here
                    assert True
                    assert False
#### this should never happen, see setup
                logger.error("Unable to click on new button in toolbar")

... so this would be my test:
    def test_ClickNewProjectButton(self):
        if exists(Pattern("TD5_newProjectButton").similar(0.80).targetOffset(-9,1))
            assert exists(Pattern("TD5_newProjectButtonResult").similar(0.90), 5), "TD5_newProjectButtonResult not there within 5 seconds)
           assert False, "TD5_newProjectButton not seen"

Vishal singh (vishalsingh5900) said : #5

Thank you so much for your help RaiMan
I think i got it
How to setup and how to use Unittest module
Thanks once again