How to pass a dictionary as an argument inside setup function of unittest - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to pass a dictionary as an argument inside setup function of unittest (/thread-18021.html) |
How to pass a dictionary as an argument inside setup function of unittest - nilaybnrj - May-03-2019 Basically I am writing an appium code to automate one native app. Now what I want is that instead of hard coding the capabilities I want to use an external dictionary which will provide data to my test case regarding all capabilities.How can I achieve that here is my main programm from appium import webdriver from time import sleep from pathlib2 import Path import os from appium.webdriver.common.touch_action import TouchAction import unittest class report(unittest.TestCase): def setUp(self): """"Launch the settings""" capabilities = {} capabilities["platformName"] = "Android" capabilities["platformVersion"] = "8.0.0" capabilities["deviceName"] = "Nil_Emulator" capabilities["app"] = os.path.abspath(str(Path(__file__).parents[1]) + "/apk/medical-wisdom-1.0.8.apk") self.driver = webdriver.Remote('http://localhost:4723/wd/hub', capabilities) self.driver.implicitly_wait(30) def tearDown(self): self.driver.quit() def test_report(self): element_best_practice = self.driver.find_elements_by_xpath("//android.widget.RelativeLayout[@index=0]") action = TouchAction(self.driver) action.tap(element_best_practice[4]).perform() sleep(3) element_report_record = self.driver.find_element_by_xpath("//android.widget.ImageView[@index=1]") action.tap(element_report_record).perform() sleep(3) if __name__ == '__main__': suite = unittest.TestLoader().loadTestsFromTestCase(report) unittest.TextTestRunner(verbosity=2).run(suite)Also the code which is going to return a dictionary is from devices import DeviceSelection import subprocess class getOS(DeviceSelection): def __init__(self): super(getOS,self).__init__() self.dict = {} def getdata(self): for device in self.list: output = subprocess.check_output("adb -s " + device+" shell getprop ro.build.version.release ",shell=True) self.dict[device]= output return self.dict RE: How to pass a dictionary as an argument inside setup function of unittest - keames - May-11-2019 Passing lists and dictionaries to functions is done using the asterisk and double asterisk syntax respectively. Here is a simple demo program to show you how it works: def aFunc (param1, param2, param3): print("This is param1: {}".format(param1)) print("This is param2: {}".format(param2)) print("This is param3: {}".format(param3)) paramDict = {"param1": "I am param1!", "param2": "I am param2!", "param3": "I am param3!"} aFunc(**paramDict) print("\n") paramList = ["I am param1!", "I am param2!", "I am param3!"] aFunc(*paramList) Using the single asterisk syntax on dictionaries passes the keys to the function. Single asterisk also works with tuples.I hope this helps. I'm going by the title of the thread. I know what unittest is, though I've never used it, I don't know what Appium code is and I am unfamiliar with about half the libraries you're using. If all you needed was to know how to pass dictionaries to functions, here you go. |