【问题标题】:Abstracting Automated Testing Code Into Python Objects将自动化测试代码抽象为 Python 对象
【发布时间】:2014-09-30 23:10:43
【问题描述】:

我目前正在通过 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')

我们的 Authentication 类可能如下所示:

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()

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

【问题讨论】:

    标签: python automated-tests appium


    【解决方案1】:

    我是这样构建这种东西的。我还使用了 unittest.TestCase,我强烈推荐它用于任何 python 自动化;它可以让您依靠setUpClasstearDownClass(一次性用于所有衍生测试)和setUptearDown(在每个测试功能之前)进行大量设置和撕裂记下你需要做的事情。

    对于我的 iOS 特定 Appium 测试:

    基础文件:ios_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!
    

    测试文件:test_login.py

    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.
    

    【讨论】:

      猜你喜欢
      • 2020-06-02
      • 2011-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-03
      • 2010-09-16
      • 1970-01-01
      相关资源
      最近更新 更多