Pass argument to test runner

Asked by Rares Pasca on 2019-01-10

Hello,
 So my test runner file uses HTML test runner. It basically looks like this:

import sys.argv

if __name__== "__main__":

    HagercadUtilities.Utilities.createFolder(HagercadConfig.HAGERCAD_TEST_REPORTS_FOLDER)
    unittest.TextTestRunner(verbosity = 2)
    output = open(HagercadConfig.HAGERCAD_HTML_REPORT_FILE,"w+")
    runner = HTMLTestRunner.HTMLTestRunner(stream=output, title=HagercadConfig.HAGERCAD_HTML_REPORT_TITLE, description=HagercadConfig.HAGERCAD_HTML_REPORT_DESCRIPTION)
    runner.run(sys.argv[1])

I want to run the tests from command line and I do either:
 pathToSikuli\Sikuli\runsikulix -r pathToMyTestRUnnerClass\HagercadTestRunner.sikuli --args HagercadTests.regressionSuite()
or,
java -jar pathToSikuli\Sikuli\sikulix.jar -r pathToMyTestRUnnerClassHagercadTestRunner.sikuli --args HagercadTests.regressionSuite()

So i want to pass via command line an argument directly to runner.run() and of course "HagercadTests.regressionSuite()" is the suite that i want to run wich is located in another file.

I;m getting this error:

    runner.run(sys.argv[1])
  File "C:\Users\rares.pasca\AppData\Roaming\Sikulix\Lib\HTMLTestRunner.py", line 678, in run
    test(result)
TypeError: 'unicode' object is not callable

I dont know exactly how to fix it.
Thanks

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
2019-01-10
Last query:
2019-01-10
Last reply:
2019-01-10
Rares Pasca (asheru93) said : #1

forgot some imports at the begninng but it doesnt matter

RaiMan (raimund-hocke) said : #2

can be reduced to this:

#coming from sys.argv[1]:
"HagercadTests.regressionSuite()"

.... this is a string !!!

but for
runner.run(parameter)

parameter must be a test suite object !!!

Hence you first have to convert sys.argv[1] to a valid test suite.

eval() might be helpful ;-)

--- something like
runner.run(eval("unittest.TestLoader().loadTestsFromTestCase(" + sys.argv[1] ")")

where the parameter in this case must name the Test class.

Best RaiMan (raimund-hocke) said : #3

sorry, a + missing:
runner.run(eval("unittest.TestLoader().loadTestsFromTestCase(" + sys.argv[1] +")")

Rares Pasca (asheru93) said : #5

Thanks RaiMan, that solved my question.

Rares Pasca (asheru93) said : #6

Is it possible to make it run from outside the class?

I'm trying to make a configuration to run the tests like this from command line.

So i have 200 methods let's say that are steps basically,step1(),step2()...step200(). I want to create a configuration in wich if someone wants to run a test, it gives in the command line :java -jar pathToSikuli -r pathToScript --args step1 step5 step127 and from there my script knows to add those steps into a test and run it.

So i was thinking to create a method that collects the arguments from cmd and adds them to a list and returns it. Then the list is passed to another method wich adds the arguments in list to a test suite. Now the method that creates the test suite is passsed to the Runner file and runs it. Is this to complicated? Or is there a simpler way to do it?

That's why I;m asking if I can run something from outisde a class because I want to add at some point this method that creates a suite and it's outside my test class.
Thanks

RaiMan (raimund-hocke) said : #7

yes you can - but this is basic Python knowledge.
Has nothing to do with SikuliX.

Rares Pasca (asheru93) said : #8

I'm halfway trough.

I give the following arguments in cmd:

java -jar pathToSikuli -r pathToTestRunner --args MyClass.startApp() MyMethodLocation.createProject()

The class that contains the tests looks like this:

class TestSuites(unittest.TestCase):

    def firstTest(self,aList):
        #steps returned by argumentsCollector()

def argumentsCollector():
    argumentsList = list()
    for i in sys.argv:
        argumentsList.append(i)
    argumentsList.pop(0)
    HagercadLogger.Logger.Log(HagercadLogger.LEVEL_WARNING, "PRINT: " + ', '.join(argumentsList))
    return argumentsList

def hagerTests():
    suite = unittest.TestSuite()
    suite.addTest(TestSuites('firstTest'))
    return suite

argumentsCollector() return a list that looks like this [MyClass.startApp(),MyMethodLocation.createProject()]
hagerTest() - is a suite that contains one test, the test inside my unittest class.

Is there a restriction to unittest in it;s default implementation, or i can pass arguments to e method inside a unittest class, like in my method firstTest(self,aList) , where aList would be the list of arguments(action/steps) collected from cmd? And then this method knows that the list is actually some steps in a test.

I'm noob in python unfortunately.

RaiMan (raimund-hocke) said : #9

Sorry, but due to private priorities I have no time to help you the next 2 - 3 weeks.

Rares Pasca (asheru93) said : #10

No problem. In the meantime i hope i will find a solution.

Wish you all the best.