将自动测试代码抽象为Python对象

将自动测试代码抽象为Python对象,python,automated-tests,appium,Python,Automated Tests,Appium,我目前正在通过Appium和python进行一些自动化测试,以测试Android应用程序。我想把一些细节抽象出来,以便测试更容易阅读 现在我有一整班人在做测试。例如,我想将此转换为: # authentication self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) username = self.driver.find_element_by_name('username') passwor

我目前正在通过Appium和python进行一些自动化测试,以测试Android应用程序。我想把一些细节抽象出来,以便测试更容易阅读

现在我有一整班人在做测试。例如,我想将此转换为:

# authentication

self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
username = self.driver.find_element_by_name('username')
password = self.driver.find_element_by_name('pw')
username.send_keys('some username')
password.send_keys('some password')
login_button = self.driver.find_element_by_name('Login')
login_button.click()
变成这样:

Authentication.login(self.driver, 'largedata_lgabriel@hmbnet.com', 'R3DruM24')
class Authentication:

    def login(driver, username, password):
        input_username = driver.find_element_by_name('username')
        input_password = driver.find_element_by_name('pw')
        input_username.send_keys(username)
        input_password.send_keys(password)
        login_button = driver.find_element_by_name('Login')
        login_button.click()
其中,我们的身份验证类可能如下所示:

Authentication.login(self.driver, 'largedata_lgabriel@hmbnet.com', 'R3DruM24')
class Authentication:

    def login(driver, username, password):
        input_username = driver.find_element_by_name('username')
        input_password = driver.find_element_by_name('pw')
        input_username.send_keys(username)
        input_password.send_keys(password)
        login_button = driver.find_element_by_name('Login')
        login_button.click()

这将需要创建一个“身份验证”类,但我不确定如何将这些方法导入到我的主测试类中,以及如何共享web驱动程序对象。

以下是我如何构造此类对象的。我还使用unittest.TestCase,我强烈推荐它用于任何python自动化;它将允许您依靠
setUpClass
tearDownClass
(所有衍生测试都是一次性的),以及
setUp
tearDown
(在每个测试函数之前)来完成大量需要完成的设置和拆卸工作

对于我的iOS特定应用程序测试:

基本文件:
ios\u Base.py

class iOSBase(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        # Set up logging if you want it
        # Set up the XCode and iOS Simulator details you need if you're doing iOS
        # Set up Appium

    def setUp(self):
        # Per test, I like to log the start of each test
        self.log.info("--- {0} ---".format(self._testMethodName))

    # etc. on setUp / tearDown stuff, whatever you need

    # Helpful function like you have in mind
    def login(self, username, password):
        # You'll get self.driver for free by using setUpClass, yea!
from ios_base import iOSBase

class iOSLoginTests(iOSBase):
    # Add any additional login-specific setUp you might want here, see also super()

    def test_valid_login(self):
        self.login(good_username, good_password)

    def test_login_invalid_username(self):
        self.login(bad_username, good_password)

    # etc.
测试文件:
Test\u login.py

class iOSBase(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        # Set up logging if you want it
        # Set up the XCode and iOS Simulator details you need if you're doing iOS
        # Set up Appium

    def setUp(self):
        # Per test, I like to log the start of each test
        self.log.info("--- {0} ---".format(self._testMethodName))

    # etc. on setUp / tearDown stuff, whatever you need

    # Helpful function like you have in mind
    def login(self, username, password):
        # You'll get self.driver for free by using setUpClass, yea!
from ios_base import iOSBase

class iOSLoginTests(iOSBase):
    # Add any additional login-specific setUp you might want here, see also super()

    def test_valid_login(self):
        self.login(good_username, good_password)

    def test_login_invalid_username(self):
        self.login(bad_username, good_password)

    # etc.