使用Python和Nose实现移动应用的自动化测试

发表于:2013-6-03 10:05

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:fbm 译    来源:51Testing软件测试网采编

  采用Appium进行自动化的功能性测试最酷的一点是,你可以使用具有最适合你的测试工具的任何一门语言来写你的测试代码。大家选择最多的一个测试编程语言就是Python。 使用Appium和Python为iOS和Android应用编写测试代码非常容易。

  在这篇博文中我们将详细讲解使用Appium下的Python编写的测试的例子代码对一个iOS的样例应用进行测试所涉及的各个步骤,而对Android应用进行测试所需的步骤与此非常类似。

  开始,先自https://github.com/appium/appiumfork并clone Appium,然后按照安装指南,在你的机器上安装好Appium。

  我还需要安装Appium的所有依赖并对样例apps进行编译。在Appium的工作目录下运行下列命令即可完成此任务:

$ ./reset.sh --ios

  编译完成后,就可以运行下面的命令启动Appium了:

$ grunt appium

  现在,Appium已经运行起来了,然后就切换当前目录到sample-code/examples/python。接着使用pip命令安装所有依赖库(如果不是在虚拟环境virtualenv之下,你就需要使用sudo命令):

$ pip install -r requirements.txt

  接下来运行样例测试:

$ nosetests simple.py

  既然安装完所需软件并运行了测试代码,大致了解了Appium的工作过程,现在让我们进一步详细看看刚才运行的样例测试代码。该测试先是启动了样例应用,然后在几个输入框中填写了一些内容,最后对运行结果和所期望的结果进行了比对。首先,我们创建了测试类及其setUp方法:

classTestSequenceFunctions(unittest.TestCase):
 
    defsetUp(self):
        app=os.path.join(os.path.dirname(__file__),
                           '../../apps/TestApp/build/Release-iphonesimulator',
                           'TestApp.app')
        app=os.path.abspath(app)
        self.driver=webdriver.Remote(
            command_executor='http://127.0.0.1:4723/wd/hub',
            desired_capabilities={
                'browserName':'iOS',
                'platform':'Mac',
                'version':'6.0',
                'app': app
            })
        self._values=[]

  “desired_capabilities”参数用来指定运行平台(iOS 6.0)以及我们想测试的应用。接下来我们还添加了一个tearDown方法,在每个测试完成后发送了退出命令:

deftearDown(self):
    self.driver.quit()

  最后,我们定义了用于填写form的辅助方法和主测试方法:

def_populate(self):
    # populate text fields with two random number
    elems=self.driver.find_elements_by_tag_name('textField')
    foreleminelems:
        rndNum=randint(0,10)
        elem.send_keys(rndNum)
        self._values.append(rndNum)
 
deftest_ui_computation(self):
    # populate text fields with values
    self._populate()
    # trigger computation by using the button
    buttons=self.driver.find_elements_by_tag_name("button")
    buttons[0].click()
    # is sum equal ?
    texts=self.driver.find_elements_by_tag_name("staticText")
    self.assertEqual(int(texts[0].text),self._values[0]+self._values[1])

  就是这样啦!Appium的样例测试代码中还有许多Python的例子。如果你对使用Nose和Python来运行Appium测试有任何问题或看法,烦请告知。

  本文转载自:http://www.oschina.net/translate/automated-mobile-app-testing-with-python-and-nose

《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号