Using HTMLTestRunner: NameError name 'ClassName' is not defined

Asked by DH

Hi,
I've a problem. I run tests in Sikuli 2.0.4, this is my use case:

File located under: C:\Sikuli\Scripts\Mytests.sikuli\ClassName.py
In the same folder I've put these files for HTMLTestrunner
HTMLTestRunner$py.class
HTMLTestRunner.py

filename = ClassName.py

from sikuli import *
import unittest
import HTMLTestRunner

class ClassName(unittest.TestCase):

    fileCreate = str(time.strftime(("%Y:%m:%d_%H:%M:%S")))
    fileName = "C:\\Sikuli\\Reports\\{}_Testreport_MyReport.html".format(fileCreate.replace(':', '_'))

    def test_StartPage(self):
        type( "\\Images\\windows_searchbar.png", 'Edge')
        click("\\Images\\windows_edge.png")
        assert False

    suite = unittest.TestLoader().loadTestsFromTestCase(ClassName)
    outfile = open(fileName, "w")
    runner = HTMLTestRunner.HTMLTestRunner(stream=outfile, title=' My report Title', description='Some desc..' )
    runner.run(suite)

When I run the test I get:

[error] script [ ClassName ] stopped with error in line 5
[error] NameError ( name 'ClassName' is not defined )
[error] --- Traceback --- error source first
line: module ( function ) statement
60: main ( ClassName ) suite = unittest.TestLoader().loadTestsFromTestCase(ClassName)
5: main ( <module> ) class ClassName(unittest.TestCase):
[error] --- Traceback --- end --------------

What am I doing wrong here?

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
RaiMan (raimund-hocke) said :
#1

not correct Python:

suite = unittest.TestLoader().loadTestsFromTestCase(ClassName)
outfile = open(fileName, "w")
runner = HTMLTestRunner.HTMLTestRunner(stream=outfile, title=' My report Title', description='Some desc..' )
runner.run(suite)

these lines must be outside the class context (no indent).

HTMLTestRunner need not be in your script folder since it is bundled with SikuliX.

Revision history for this message
DH (dh72) said :
#2

Hi RaiMan, thanks for ypur answer :-)
I did lika you told me:

The two files

* HTMLTestRunner.py
* HTMLTestRunner$py.class

are located in the same folder as sikulixide-2.0.4.jar is and I even put the files where I've my test script "ClassName.py"

C:\Sikuli
- HTMLTestRunner.py
- HTMLTestRunner$py.class

-- Scripts\ClassName
    --- HTMLTestRunner.py
    ---HTMLTestRunner$py.class

My test class as follows:

from sikuli import *
import unittest
import HTMLTestRunner

fileCreate = str(time.strftime(("%Y:%m:%d_%H:%M:%S")))
fileName = "C:\\Sikuli\\Reports\\{}_Testreport_MyReport.html".format(fileCreate.replace(':', '_'))

class ClassName(unittest.TestCase):

    def test_StartPage(self):
        type( "\\Images\\windows_searchbar.png", 'Edge')
        click("\\Images\\windows_edge.png")
        assert False

    def isAborted(self):
        return False

suite = unittest.TestLoader().loadTestsFromTestCase(ClassName)
outfile = open(fileName, "w")
runner = HTMLTestRunner.HTMLTestRunner(stream=outfile, title=' My report Title', description='Some desc..' )
runner.run(suite)

Now gives the error:

[error] script [ ClassName ] stopped with error in line 22
[error] AttributeError ( 'HTMLTestRunner' object has no attribute 'isAborted' )
[error] --- Traceback --- error source first
line: module ( function ) statement
22: main ( <module> ) runner.run(suite)
[error] --- Traceback --- end --------------

What I can see I've added isAborted to my class? Or what?

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

apparently not does not work with your

    def isAborted(self):
        return False

so leave it out and try to get the basics running without using your own HTMLTestRunner.py.

Then you might start to do additional experiments.

Please read:
https://sikulix-2014.readthedocs.io/en/latest/scenarios.html#access-python-packages-from-sikulix-scripts-run-by-sikulix-gui-or-commandline

Revision history for this message
DH (dh72) said :
#4

Thanks for your answers, it is really appreciated.
I did this: Created a brand new testfile.py

from sikuli import *
import unittest
import HTMLTestRunner

filSkapades = str(time.strftime(("%Y:%m:%d_%H:%M:%S")))
print filSkapades
filnamn = "C:\\Sikuli\\Reports\\{}_Testraport_Filurmotionsmall.html".format(filSkapades.replace(':', '_'))

class MySimpleTest(unittest.TestCase):

    def test_StartPage(self):
        type( "\\Images\\windows_searchbar.png", 'Edge')
        click("\\Images\\windows_edge.png")
        type("\\Images\\edge_url_window.png", 'https://test.test.com/' + Key.ENTER)

        if exists("\\Images\\someImage.png"):
            assert True
        else:
            assert False

        m = find("\\Images\\edge_close.png")
        m.highlight(1)
        click(m.getTopRight())

      #I know this is wrong but when I put the instanciation of class inside the class, it just works
       suite = unittest.TestLoader().loadTestsFromTestCase(FilurTester)
      outfile = open(filnamn, "w")
      runner = HTMLTestRunner.HTMLTestRunner(stream=outfile, title=' Some testreport', description='More testing.' )
      runner.run(suite)

When I run the script from Sikuli IDE, the testreport is now created.
But, when I run from command line I get the error:
NameError ( name 'MySimpleTest' is not defined )

And yes, I've put the
HTMLTestRunner$py.class and HTMLTestRunner.py in the same folder as the sikuli script MySimpleTest.py. And also I put the files in
C:\Users\MyUserProfile\AppData\Roaming\Sikulix\Lib
HTMLTestRunner$py.class and HTMLTestRunner.py

Some debug:
[DEBUG]startUp: IDE: Running: C:\Sikuli\sikulixide-2.0.4.jar
[DEBUG]startUp: IDE: AppData: C:\Users\MyUserProfile\AppData\Roaming\Sikulix
[DEBUG]startUp: IDE: starting with classpath: C:\Sikuli\sikulixide-2.0.4.jar ...
[DEBUG]startUp: IDE: no extensions.txt nor valid content
[DEBUG]startUp: IDE: adding extension file: C:\Users\MyUserProfile\AppData\Roaming\Sikulix\Extensions\jruby-complete-9.2.0.0.jar
[DEBUG]startUp: IDE: adding extension file: C:\Users\MyUserProfile\AppData\Roaming\Sikulix\Extensions\jython-standalone-2.7.1.jar
[414 debug] RunTime:temp folder ok: C:\Users\MyUserProfile\AppData\Local\Temp\Sikulix_1235572012
Options: *** options dump
Options: testing =
Options: OptionsFile = C:\Users\MyUserProfile\AppData\Roaming\Sikulix\SikulixStore\SikulixOptions.txt
Options: *** options dump end
***** show environment for 2.0.4-2020-03-14_08:01 API
user.home: C:\Users\MyUserProfile
user.dir (work dir): C:\
user.name: MyUserProfile
java.io.tmpdir: C:\Users\MyUserProfile\AppData\Local\Temp
running 64Bit(amd64) on w (10.0) from a jar
java 11 version 11 vm 11.0.8+10 class 55.0 arch 64
app data folder: C:\Users\MyUserProfile\AppData\Roaming\Sikulix
executing jar: C:\Sikuli\sikulixide-2.0.4.jar
*** classpath dump sikulix
  0: C:\Sikuli\sikulixide-2.0.4.jar
*** classpath dump end
***** show environment end
[DEBUG]startUp: IDE: *********************** leaving start
[84 debug] Sikulix: starting IDE
[142 debug] RunTime:temp folder ok: C:\Users\MyUserProfile\AppData\Local\Temp\Sikulix_736297173
Options: *** options dump
Options: testing =
Options: OptionsFile = C:\Users\MyUserProfile\AppData\Roaming\Sikulix\SikulixStore\SikulixOptions.txt
Options: *** options dump end
***** show environment for 2.0.4-2020-03-14_08:01 API
user.home: C:\Users\MyUserProfile
user.dir (work dir): C:\
user.name: MyUserProfile
java.io.tmpdir: C:\Users\MyUserProfile\AppData\Local\Temp
running 64Bit(amd64) on w (10.0) from a jar
java 11 version 11 vm 11.0.8+10 class 55.0 arch 64
app data folder: C:\Users\MyUserProfile\AppData\Roaming\Sikulix
executing jar: C:\Sikuli\sikulixide-2.0.4.jar
*** classpath dump sikulix
  0: C:\Sikuli\sikulixide-2.0.4.jar
  1: C:\Users\MyUserProfile\AppData\Roaming\Sikulix\Extensions\jruby-complete-9.2.0.0.jar
  2: C:\Users\MyUserProfile\AppData\Roaming\Sikulix\Extensions\jython-standalone-2.7.1.jar
*** classpath dump end
***** show environment end
[1561 debug] Runner: added: PackedSikulix [skl] text/skl
[1567 debug] Runner: added: NetworkRunner [] NET
[1568 debug] Runner: init: class org.sikuli.script.runners.AbstractLocalFileScriptRunner: warning: not possible
[1570 debug] Runner: added: SikulixJar [jar] text/jar
[1587 debug] Runner: added: Jython [py] text/jython
[1589 debug] Runner: added: PowerShell [ps1] text/powershell
[1590 debug] Runner: added: Text [txt] text/text
[1612 debug] Runner: added: JRuby [rb] text/ruby
[1614 debug] Runner: added: Sikulix [] directory/sikulix
[1615 debug] Runner: added: PackedSikulix [zip] application/zip
[2288 debug] Runner: added: JavaScript [js] text/javascript
[2310 debug] HotkeyManager: add Abort Hotkey: ALT+SKIFT C (67, 9)
[2631 debug] Runner: runscript: running script: C:\Sikuli\Scripts\MySimpleTest\MySimpleTest.py
[2638 debug] JythonRunner: starting initialization
[2643 debug] RunTime:resourceLocation: (class org.sikuli.script.support.RunTime) /Lib/sikuli
[2651 debug] RunTime:resourceList: having jar: jar:file:/C:/Sikuli/sikulixide-2.0.4.jar!/Lib/sikuli
[4591 debug] RunTime:files exported: 7 from: Lib/sikuli to:
 C:\Users\MyUserProfile\AppData\Roaming\Sikulix\Lib\sikuli
[9513 debug] Jython: added as Jython::sys.path[0]:
C:\Users\MyUserProfile\AppData\Roaming\Sikulix\Lib\site-packages
[9521 debug] Jython: ***** sys.path
 0: C:\Users\MyUserProfile\AppData\Roaming\Sikulix\Lib\site-packages
 1: C:\Users\MyUserProfile\AppData\Roaming\Sikulix\Lib
 2: C:\Users\MyUserProfile\AppData\Roaming\Sikulix\Extensions\Lib
 3: C:\Users\MyUserProfile\AppData\Roaming\Sikulix\Extensions\jython-standalone-2.7.1.jar\Lib
[9544 debug] JythonRunner: ready: version 2.7.1
[debug] Jython: SikulixForJython: init: starting
[debug] Jython: added as Jython::sys.path[0]:
C:\Users\MyUserProfile\AppData\Roaming\Sikulix\Lib\site-packages
[debug] Jython: SikulixForJython: init: success
[debug] Jython: sikuli: Sikuli: starting init
[debug] Screen: initScreens: starting
[debug] Screen: Accessing: GraphicsEnvironment.getLocalGraphicsEnvironment()
[debug] Screen: Accessing: GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()
[debug] Screen: ScreenDevice 0 has (0,0) --- will be primary Screen(0)
[debug] Screen: Monitor 0: (0, 0) 1920 x 1200
[debug] Mouse: init start
[debug] Mouse: init end
[debug] Screen: initScreens: ending
[debug] Jython: use as default region: R[0,0 1920x1200]@S(0)
[debug] Image: BufferedImage: (1920, 1200)
[debug] Jython: sikuli: Sikuli: ending init
[debug] Jython: use as default region: R[0,0 1920x1200]@S(0)
[debug] Image: BufferedImage: (1920, 1200)
[debug] ImagePath: new BundlePath: C:\Sikuli\Scripts\MySimpleTest
2020:11:06_15:26:22
[error] script [ MySimpleTest ] stopped with error in line 6
[error] NameError ( name 'MySimpleTest' is not defined )
[error] --- Traceback --- error source first
line: module ( function ) statement
27: main ( MySimpleTest ) suite = unittest.TestLoader().loadTestsFromTestCase(MySimpleTest)
6: main ( <module> ) class MySimpleTest(unittest.TestCase):
[error] --- Traceback --- end --------------
[debug] HotkeyManager: reset: removed all SikuliX hotkeys.
[debug] HotkeyController: stopping hotkey provider
[debug] RunTime:***** final cleanup at System.exit() *****
[debug] FileManager: deleteFileOrFolder:
C:\Users\MyUserProfile\AppData\Local\Temp\Sikulix_736297173
[20378 debug] RunTime:***** final cleanup at System.exit() *****
[20447 debug] FileManager: deleteFileOrFolder:
C:\Users\MyUserProfile\AppData\Local\Temp\Sikulix_1235572012

Arrrggggghhhh, :-)

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

This works for me:

--- new script in the IDE:
import unittest
import HTMLTestRunner

outDir = getBundlePath()
outHTML = os.path.join(outDir, "testOUT.html")

class myTest(unittest.TestCase):
    def test1(self):
        pass

suite = unittest.TestLoader().loadTestsFromTestCase(myTest)
outfile = open(outHTML, "w")
testRunner = HTMLTestRunner.HTMLTestRunner(stream=outfile)
testRunner.run(suite)
outfile.close()

--- stored this as folder <somewhere>/testHTR
which gives
... <somewhere>/testHTR
... ... testHTR.py

--- running in the IDE creates
<somewhere>/testHTR/testOUT.html

--- running this from commandline:
java -jar sikulixide...jar -r <somewhere>/testHTR

works as expected.

I found, that one should not use "runner" as variable name (therefor I used testRunner), since this leads to a problem (... isAborted() ...),. Probably the name is already internally defined globally for something else (I have to sort out).

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

Be aware: no need to copy/have HTMLTestRunner.py/....$py.class anywhere, since it is bundled with SikuliX

Revision history for this message
DH (dh72) said :
#7

Thanks RaiMan, that solved my question.

Revision history for this message
DH (dh72) said :
#8

Thanks for your quick response :-)