How to pass parameters received by my function to a class created inside my function in Python?

I am developing a program with Python 3.7 and Appium 1.15

The program will run parallel test on real Android devices. I need to pass parameters to the function which will run the test on a specific smartphone.

These parameters are the desired capabilities (udid, os version, devicename, etc….).

So my idea was to create a method in which will receive the parameters and pass it to the Class object.
The best way to explain my idea is to share my code:

def Task_auto1(p_name_action,p_udid, p_systemPort, p_deviceName, p_version, p_os,quantity_tasks_per_hit):

   class auto1(unittest.TestCase):
       def setUp(self):
           print(f"--------- Start {p_name_action} for Smartphone N° {p_udid} --------------")

           desired_caps = {}
           desired_caps['automationName'] = 'UiAutomator2'
           desired_caps['platformName'] = p_os
           desired_caps['platformVersion'] = p_version
           desired_caps['deviceName'] = p_deviceName
           desired_caps['udid'] = p_udid
           desired_caps['noReset'] = 'true'
           desired_caps['appWaitActivity'] = 'com.app1.activity.MainTabActivity'
           desired_caps['autoGrantPermissions'] = 'true'
           desired_caps['appPackage'] = ''
           desired_caps['appActivity'] = ''
           desired_caps['systemPort'] = p_systemPort
           desired_caps['chromeDriverPort'] = p_systemPort
           self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
           self.driver.start_activity("", "")

       def tearDown(self):

       def test_auto1(self):
		#do some stuff

    suite = unittest.TestLoader().loadTestsFromTestCase(auto1)

But I have no idea how to pass the parameters received by my function to my test Class and method.

I search on Google and I couldn’t find the answer to my question. I typed “python pass parameters from my function to a class inside my function”, but it doesn’t give me the accurate answer.

A second problem may come, I need this unittest test running properly Appium test.

If some expert could help the newbie I am to find solution to this complex programming issue, please?

Thanks a lot.

Why are you defining the class that creates the webdriver instance inside a function? It’s much better if you define it as a separate class and then instantiate it in the module where your function is defined.

ok. Thank you. great idea! But how can I pass the parameter to my class?

def Task_auto1(p_name_action,p_udid, p_systemPort, p_deviceName, p_version, p_os,quantity_tasks_per_hit):
    suite = unittest.TestLoader().loadTestsFromTestCase(auto1(p_name_action,p_udid, p_systemPort, p_deviceName, p_version, p_os,quantity_tasks_per_hit))

and my class definition like this?
class auto1(unittest.TestCase,p_name_action,p_udid, p_systemPort, p_deviceName, p_version, p_os,quantity_tasks_per_hit):

I tried like this but it doesn’t work. :frowning:

Just pass along the parameters in a dictionary.

What testing framework are you using so I can check how it works?

Thank you so much. I am using unittest. I am not good enough to use other test framework.
When you said

Just pass along the parameters in a dictionary

.You mean I stored my parameters in a dictionary “mydict” and add it in the unittest line of code as parameter, like this?
suite = unittest.TestLoader().loadTestsFromTestCase(auto1(mydict))

If that option is allowed in the unittest framework, then yes. That way, you won’t have to specify every parameter and when you instantiate the driver with an invalid parameter in the dictionary it’ll throw an exception.

ok, thank you. I will try In case it doesn’t work, which framework you recommand? Pytest? other one?

unittest might be good enough, but it depends on how you want to develop your solution. You might want to google it a bit. This is a good starting point:

But more importantly, you need to get more familiar with the Python language so you can write concise clear code that’ll be easy to maintain.

Additionally, you might want to read a bit on solutions other people offer, like:

This guy uses unittest along with Selenium, which provides more flexibility.

Another option is that you develop your solution using Java instead of Pythonñ it has well documented testing frameworks such as JUnit5 and TestNG.

Are you constrained to use only Python as the programming language for your project?

1 Like

Thank you so much for your help.