作者都是各自领域经过审查的专家,并撰写他们有经验的主题. 我们所有的内容都经过同行评审,并由同一领域的Toptal专家验证.
亚历山大Gedevanishvili
验证专家 在工程
18 的经验

Alexander是一位多才多艺的全栈开发人员,拥有超过十年的经验. He is passionate about mobile development.

分享

Testing is an essential part of any mobile app development process. Whether you are automating such tests or not, 任何理智的开发者都不会认为自己的工作已经完成,除非他们已经测试了自己的应用.

一个经过良好测试的应用通常要经过多个测试步骤:单元测试、集成测试、 验收测试等等. 随着应用程序的发展 测试的重要性 increases and automation in testing becomes a necessity.

葫芦 验收测试 for 安卓 and iOS

而其他平台, 比如网络, have advanced significantly in terms of testing mechanisms and 框架s, the mobile realm is not lagging behind. 在本文中, 您将学习如何使用葫芦自动化UI为您的安卓和iOS应用程序使用简单的英语说明,并使验收测试他们尽可能无痛.

什么是UI测试?

If you have been testing your apps manually, 你可能会浪费大量的时间一遍又一遍地执行同样的任务. 您对代码进行了一些更改, 构建应用程序, 在设备或模拟器中运行它, and fiddle with the app to figure out if it works as expected.

通过自动化UI测试,您可以自动执行相同的手动步骤. 如果你的应用有一个合适的大小, 这可以节省你大量的时间,也可以避免你的应用充斥着令人尴尬的漏洞, 尤其是那些回归的.

“这听起来很棒,”你会说,但你该如何在安卓或iOS应用中做到这一点呢?

UI Testing Frameworks for 安卓 and iOS

If you read the official documentation for 安卓 and iOS, they suggest you write and run UI tests in their official IDEs. For 安卓, it’s 安卓 Studio, and for iOS, it’s Xcode.

官方文档甚至推荐了用于测试的特定框架. The official 安卓 documentation covers some topics about 浓缩咖啡, the 安卓 UI testing 框架. Similarly, Apple suggests using the XCTest 框架.

And if you are going to work seriously on UI tests, you may be following these suggestions, 这是有道理的,因为浓缩咖啡是由谷歌维护的,是安卓支持库的一部分. 浓缩咖啡很有可能会支持Google未来为安卓推出的所有新功能. You could say the same about XCTest 框架 for iOS.

然而, 值得记住的是,尽管自动化测试有很多好处, many developers simply don’t write them at all.

每个深入了解测试自动化的开发人员都知道这是一个好主意. 但, when it comes to sitting down and writing these tests, many developers start questioning if it is worth their time, 因为手动“触摸按钮”比编写自动“触摸按钮”的代码要快得多. 有时,急切地等着试用这款应用的客户和经理也没有提供帮助.

许多开发人员, 在这一点上, 决定继续开发应用程序的新功能,而不是为现有的功能编写自动化的UI测试.

当应用程序增长时, 每次更新应用程序时,手动“触摸这些按钮”变得越来越耗时.

但 what if t在这里 was a 框架 that made UI testing easier, and didn’t give you any excuse to not write UI tests for your apps?

满足 葫芦.

葫芦: Automated Acceptance Test for 手机应用程序s

大约一年前, 我开始寻找一个对非软件开发人员来说容易使用的测试框架. 就在那时,我发现了葫芦.

This open source testing 框架, developed and maintained by the Xamarin的 team, works for both 安卓 and iOS. 它允许您编写和执行移动应用程序的自动化验收测试.

验收测试通常是在系统测试之后进行的,用于确定应用程序是否满足业务需求. Given that it operates on the UI level, this works well as our choice of UI testing automation 框架.

葫芦 can interact with your app like 浓缩咖啡 or XCTest does. 然而,让葫芦成为一个很好的选择的是它对黄瓜的支持.

Cucumber是一个可以运行用简单英语编写的自动化测试的工具(如果您愿意,您可以将其调整为使用任何其他简单语言)。. So to write automated tests on Cucumber, the tester doesn’t need to know Java, objective - c, 或者其他编程语言.

什么让葫芦滴答作响?

葫芦框架由可以与安卓和iOS应用程序交互的库组成. 它可以在真实的设备上运行. So it can do things that the tester is doing manually.

T在这里 are two different projects on GitHub that make 葫芦 possible:

葫芦 can work with any Ruby-based test 框架. 在本文中, 我们将介绍Cucumber——为葫芦编写测试的最流行和最方便的方法.

在继续之前, if you want to try 葫芦 as you follow the rest of the article, be sure you have Ruby installed on your machine. You can find detailed instructions of installation 在这里.

接下来,按照上面的GitHub链接为你喜欢的平台安装葫芦.

Writing Your First Test on 葫芦

Writing tests on 葫芦 is quite easy. Let’s see how a simple test for an iOS app looks:

功能:用户登录

    场景:用户登录失败
        鉴于这款应用已经发布
        Then I wait for the "Login" button to appear
        When I enter "tstuser" into the "Username" field
        And I enter "qwerty" into the "Password" field
        点击“登录”
        Then I should see "Username you entered is incorrect"

    场景:用户登录成功
        鉴于这款应用已经发布
        Then I wait for the "Login" button to appear
        When I enter "testeruser" into the "Username" field
        And I enter "qwerty" into the "Password" field
        点击“登录”
        然后我应该看到“嘿,测试用户!"

在这里, an app is being tested with incorrect username and password, and then is being tested with correct username and password. 测试期望应用程序在第一种情况下登录失败,但在第二种情况下登录成功.

You can create as many scenarios as need, 你所需要做的就是把这些步骤/说明分解成简单的英语句子. 就像你写故事一样!

有谁知道 行为驱动开发 (BDD) will already find themselves familiar with this.

葫芦是如何工作的?

To see what goes on behind the steps that tester is using, you can open the project on GitHub and check the following 文件:

calabash-cucumber/features/step_definitions/calabash_steps.rb

Let’s see a definition of the following step:

When I enter "testeruser" into the "Username" field
然后/ ^我进入  "([^\"]*)" 到  "([^\"]*)" 场/美元做| text_to_type, field_name |
  touch("textField marked: '#{field_name}'")
  wait_for_keyboard
  keyboard_enter_text text_to_type
  睡眠(STEP_PAUSE)
结束

This small snippet of Ruby code looks for a specific field, 触摸它, 等待键盘出现, 中键入文本 text_to_type variable, and waits for a bit before switching to the next step.

The first word of the step can be “Given,” “When,” “Then,” “And,” or “但.” It does not matter what keyword you will use. You can use any of them to make the story clearer.

如何添加自定义步骤

如果您需要的步骤尚未在葫芦中实现,您可以自己编写它. The syntax is exactly the same as it is in already predefined steps.

例如,如果测试人员需要通过占位符访问输入字段,而不是字段名称:

然后/ ^我进入  "([^\"]*)" 到田野占位符  "([^\"]*)"$/ do |text_to_type, 占位符|
	touch("textField placeholder:'#{placeholder}'")
	wait_for_keyboard ()
	keyboard_enter_text text_to_type
	睡眠(STEP_PAUSE)
结束

This step definition is much the same as it was the previous one, but you’re accessing the field by placeholder instead of the field name. Given how your app looks, this may make things even easier for the tester.

And it’s easy for the developer, too. 开发人员只执行一次步骤,然后测试人员在需要的时候使用它. 此外,您不需要了解很多Ruby知识就可以实现您自己的定制步骤.

You can find the Ruby functions you can use, 在这里:

http://www.rubydoc.info/gems/calabash-cucumber/Calabash/Cucumber

Xamarin的测试云

T在这里 is one more challenge when testing mobile applications. You should test them on as many devices as possible, because t在这里 are so many devices and so many OS versions.

这就是 Xamarin的测试云 很有帮助. 云中有大约2000个真实的设备,好消息是它支持葫芦测试.

通过避免重复工作而帮助您节省时间的葫芦测试现在可以用于在许多实际设备上测试您的应用程序.

开始编写UI测试

Whether 葫芦 is the testing solution your app needs, 它带来的好处, 在为你的移动应用编写自动化UI测试时,没有任何借口. 如果你的应用严重依赖于某些设备功能,那么葫芦可能会出现问题.g.(相机),但它仍然使得为大多数应用程序编写测试变得容易得多.

了解基本知识

  • 什么是验收测试?

    验收测试确定应用程序是否满足业务需求, usually from the point of view of its users.

  • 黄瓜是什么??

    Cucumber is a tool that can run automated tests written in plain English. 它允许人们在不需要编程背景的情况下编写自动化测试.

聘请Toptal这方面的专家.
现在雇佣
亚历山大Gedevanishvili

亚历山大Gedevanishvili

验证专家 在工程
18 的经验

第比利斯,格鲁吉亚

2016年1月20日成为会员

作者简介

Alexander是一位多才多艺的全栈开发人员,拥有超过十年的经验. He is passionate about mobile development.

作者都是各自领域经过审查的专家,并撰写他们有经验的主题. 我们所有的内容都经过同行评审,并由同一领域的Toptal专家验证.

World-class articles, delivered weekly.

By entering your email, you are agreeing to our 隐私政策.

World-class articles, delivered weekly.

By entering your email, you are agreeing to our 隐私政策.

Toptal开发者

加入总冠军® 社区.