如何保持方法分离在所有python测试套件中都很常见

时间:2016-03-30 05:28:50

标签: python automation appium

假设这是python文件, androidChess.py

import os
import unittest
from appium import webdriver
from time import sleep

class ChessAndroidTests(unittest.TestCase):
    "Class to run tests against the Chess Free app"
    def setUp(self):
        "Setup for the test"
        desired_caps = {}
        desired_caps['platformName'] = 'Android'
        desired_caps['platformVersion'] = '4.2'
        desired_caps['deviceName'] = 'Android Emulator'
        # Returns abs path relative to this file and not cwd
        desired_caps['app'] = os.path.abspath(os.path.join(os.path.dirname(__file__),'apps/Chess Free.apk'))
        desired_caps['appPackage'] = 'uk.co.aifactory.chessfree'
        desired_caps['appActivity'] = '.ChessFreeActivity'
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

    def tearDown(self):
        "Tear down the test"
        self.driver.quit()

    def test_single_player_mode(self):
        "Test the Single Player mode launches correctly"
        element = self.driver.find_element_by_name("PLAY!")
        element.click()
        self.driver.find_element_by_name("Single Player").click()
        textfields = self.driver.find_elements_by_class_name("android.widget.TextView")
        self.assertEqual('MATCH SETTINGS', textfields[0].text)

#---START OF SCRIPT
if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(ChessAndroidTests)
    unittest.TextTestRunner(verbosity=2).run(suite)

我有 setup()方法,在大多数文件中都很常见。不是在所有文件中编写相同的代码,我如何将其分开?并从别处打电话给它?

 def setUp(self):
            "Setup for the test"
            desired_caps = {}
            desired_caps['platformName'] = 'Android'
            desired_caps['platformVersion'] = '4.2'
            desired_caps['deviceName'] = 'Android Emulator'
            # Returns abs path relative to this file and not cwd
            desired_caps['app'] = os.path.abspath(os.path.join(os.path.dirname(__file__),'apps/Chess Free.apk'))
            desired_caps['appPackage'] = 'uk.co.aifactory.chessfree'
            desired_caps['appActivity'] = '.ChessFreeActivity'
            self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

我希望此函数是独立的,稍后将从该位置调用。 这该怎么做 ? 请帮助..

0 个答案:

没有答案