0

I am using python, unittest and sikuli to create and run some automated tests. So here is my code:

class TestSmoke(unittest.TestCase):

    def createdTestFromArgument(self):
        #step1
        #step2
        #step3
        ...
        #stepx

def argumentsCollector():
   argumentsList = list()
   for i in sys.argv:
        argumentsList.append(i)
   argumentsList.pop(0)
   return argumentsList


def testBuilder():
    #requested help

def smokeTests():
    suite = unittest.TestSuite()
    suite.addTest(TestSmoke('createdTestFromArgument'))
    return suite

I have a test inside TestSmoke class and a suite, smokeTests(). This is like the standard unittest.py approach.

I am running the script like this: ..pathToProject --args step1 step2 step3 argumentsCollector() takes the arguments from command line and adds them to a list then returns the list. So far so good.

Inside the testBuilder(), I want to create a method like the one inside TestSmoke class with the test steps that are given in cmd and collected in my list inside argumentsCollector.

Then if I'm able to do this to call it inside createdTestFromArgument or inside TestSmoke class in order to run it like unittest.py test.

BlueSheepToken
  • 5,751
  • 3
  • 17
  • 42
  • This is a little bit too abstract for me, can u show an example? – BlueSheepToken Jan 15 '19 at 10:52
  • I mean the idea here is that i have the list returned by argumentsCollector() that looks exactly like this: argumentsList = [step1,step2,step3] for example. From that list inside testBuilder i want to build something that returns something similar to what's inside createdTestFromArgument(self) if you remove comments. I need it this way in order for unittest.py to recognize that this is actually a test when I want to run it – Rares Pasca Jan 15 '19 at 10:59
  • You can make a class, and store your list as an attribute? (By the way, sys.argv[1:] does the same thing as your method) – BlueSheepToken Jan 15 '19 at 11:01
  • or another way would be to give this list directly to def createdTestFromArgument(self) I guess... – Rares Pasca Jan 15 '19 at 11:04
  • Basically what you are trying to do is run individual tests for each parameter provided at the command line. You could use unittest's *subTest* to do so. See an example here: https://stackoverflow.com/a/34094/8085234 That question also has multiple implementations so you might want to give all the other answers a look. – BoboDarph Jan 15 '19 at 11:04
  • @BoboDarph something like that, exactly.Run test individually by provided arguments in command line, and the arguments are steps(methods) . I will have a look , thank you. – Rares Pasca Jan 15 '19 at 11:08
  • I've looked into this, maybe I did not understood correctly but they don't answer my needs. The arguments in my case are methods, for example startApp() createProject() and so on. They are not data sets. So what i need is to create a test based on the arguments feeded in command line that are actions(methods) like startApp() createProject() etc... My command will be cmd --args startApp() createProject() for example. And in my script I need to create a single test from the given args wich are actions. – Rares Pasca Jan 15 '19 at 12:45

0 Answers0