Python Forum
How to pass a dictionary as an argument inside setup function of unittest
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to pass a dictionary as an argument inside setup function of unittest
#1
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
Reply
#2
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)
Output:
This is param1: I am param1 This is param2: I am param2 This is param3: I am param3 This is param1: I am param1 This is param2: I am param2 This is param3: I am param3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in using unittest akbarza 2 257 Feb-25-2024, 12:51 PM
Last Post: deanhystad
  mutable argument in function definition akbarza 1 423 Dec-15-2023, 02:00 PM
Last Post: deanhystad
  How to pass encrypted pass to pyodbc script tester_V 0 800 Jul-27-2023, 12:40 AM
Last Post: tester_V
  with open context inside of a recursive function billykid999 1 549 May-23-2023, 02:37 AM
Last Post: deanhystad
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 752 May-02-2023, 08:40 AM
Last Post: Gribouillis
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,026 Dec-25-2022, 03:00 PM
Last Post: askfriends
  passing dictionary to the function mark588 2 932 Dec-19-2022, 07:28 PM
Last Post: deanhystad
Question Unwanted execution of unittest ThomasFab 9 1,944 Nov-15-2022, 05:33 PM
Last Post: snippsat
  i want to use type= as a function/method keyword argument Skaperen 9 1,771 Nov-06-2022, 04:28 AM
Last Post: Skaperen
  unittest.mock for an api key silver 3 1,335 Aug-29-2022, 03:52 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020