首页 > 解决方案 > 如何在unittest的设置函数中将字典作为参数传递

问题描述

基本上我正在编写一个 appium 代码来自动化一个本机应用程序。现在我想要的是,我不想对功能进行硬编码,而是使用一个外部字典,它将为我的测试用例提供有关所有功能的数据。我怎样才能实现这一点

这是我的主要程序

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)

还有要返回字典的代码是

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

标签: pythonappium

解决方案


推荐阅读