openKylin论坛

 找回密码

Getting started with Autopilot - 开始使用Autopilot [复制链接]

本帖最后由 Jiaowen520Li 于 2013-3-13 13:42 编辑

*温馨提示:本人英文能力有限,翻译不当之处还望大家跟帖指正,共同学习、进步!*

http://www.theorangenotebook.com ... with-autopilot.html
Getting started with Autopilot
开始使用Autopilot

If you caught the last post, you'll have some background on autopilot and what it can do. Start there if you haven't already read the post.
如果你赶上了最后一篇文章,你会了解到一些关于Autopilot的背景知识和Autopilot能做什么。如果你还没有读过这篇文章请点这里。

So, now that we've seen what autopilot can do, let's dig in to making this work for our testing efforts. A fair warning, there is some python code ahead, but I would encourage even the non-programmers among you to have a glance at what is below. It's not exotic programming (after all, I did it!). Before we start, let's make sure you have autopilot itself installed. Note, you'll need to get the version from this ppa in order for things to work properly:
所以,现在我们已经看到Autopilot能做什么,让我们为能让其效力于我们的测试工作开始认真工作吧。一个公正的警告,Autopilot前面会有一些python代码,但我鼓励甚至是非程序员看看后面的内容。这不是特殊的编程(毕竟,我做到了!)。在我们开始之前,确保Autopilot是已经安装的。请注意,为了能够让其正常工作,你将需要从这个ppa获取版本:
sudo add-apt-repository ppa:autopilot/ppa
sudo apt-get update && sudo apt-get install python-autopilot

Ok, so first things first. Let's create a basic shell that we can use for any testcase that we want to write. To make things a bit easier, there's a lovely bazaar branch you can pull from that has everything you need to follow along.
好的,先做重要的事情。让我们创建一个基本的shell脚本,可用于任何我们想编写的测试用例。为了让事情变得更简单,有一个好的bazaar分支你可以带走,那里有一切你想要遵从的东西。
bzr branch lp:~nskaggs/+junk/autopilot-walkthrough
cd autopilot-walkthrough

You'll find two folders. Let's start with the helloworld folder. We're going to verify autopilot can see the testcases, and then run and look at the 'helloworld' tests first. (Note, in order for autopilot to see the testcases, you need to be in the root directory, not inside the helloworld directory)
你将发现两个文件夹。让我们先从helloworld文件夹开始。我们将要验证Autopilot可以读取这个测试用例,然后运行、考虑“helloworld”测试先。(请注意,为了使Autopilot读取到这个测试用例,你需要在根目录而不是在helloworld目录里面)

$ autopilot list helloworld
Loading tests from: /home/nskaggs/projects/

    helloworld.test_example.ExampleFunctions.test_keyboard
    helloworld.test_example.ExampleFunctions.test_mouse
    helloworld.test_hello.HelloWorld.test_type_hello_world

3 total tests.  

Go ahead and execute the first helloworld test.
继续执行第一个helloworld测试。

autopilot run helloworld.test_hello.HelloWorld.test_type_hello_world

A gedit window will spawn, and type hello world to you ;-) Go ahead and close the window afterwards. So, let's take a look at this basic testcase and talk about how it works.
一个文本编辑窗口将会产生,并打印“hello world”给你;-)然后关闭窗口。所以,让我们看看这个基础的测试用例并谈论下它是如何工作的。

from autopilot.testcase import AutopilotTestCase

class HelloWorld(AutopilotTestCase):

    def setUp(self):
        super(HelloWorld, self).setUp()
        self.app = self.start_app("Text Editor")

    def test_type_hello_world(self):
        self.keyboard.type("Hello World")


If you've used other testing frameworks that follow in the line of xUnit, you will notice the similarities. We implement an AutopilotTestCase object (class HelloWorld(AutopilotTestCase)), and define a new method for each test (ie, test_type_hello_world). You will also notice the setUp method. This is called before each test is run by the testrunner. In this case, we're launching the "Text Editor" application before we run each test (self.start_app("Text Editor")). Finally our test (test_type_hello_world) is simply sending keystrokes to type out "Hello World".
如果你使用其它测试框架,遵循xUnit路线,你将注意到它们的相似之处。我们实现一个Autopilot测试用例对象(class HelloWorld(AutopilotTestCase)),并为每个测试定义一个新方法(例如:test_type_hello_world)。你还将注意到设置方法。这就是所谓的在每个测试之前由testrunner执行。在这种情况下,我们运行每个测试在开始的“Text Editor”应用前(self.start_app("Text Editor"))。最后,我们的测试只是简单地发送按键打印出“Hello World”。

From this basic shell we can add more testcases to the helloworld testsuite easily by adding a new method. Let's add some simple ones now to show off some other capabilities of autopilot to control the mouse and keyboard. If you branched the bzr branch, there is a few more tests in the test_example.py file. These demonstrate some of the utility methods AutopilotTestCase makes available to us. Try running them now. The comments inside the file also explain briefly what each method does.
从这个基本的shell脚本我们可以借助添加一个新方法更容易地添加更多的测试用例到helloworld测试集。让我们添加一些简单的来展示一些Autopilot控制鼠标和键盘的新功能。如果你分支的bzr分支,在test_example.py文件中有更多的一些测试。这些展示一些我们适用的Autopilot测试用例实用方法。现在尝试运行他们吧。文件里的注释也将简略的说明每个方法做了什么。

autopilot run helloworld.test_example.ExampleFunctions.test_keyboard
autopilot run helloworld.test_example.ExampleFunctions.test_mouse

Now there is more that autopilot can do, but armed with this basic knowledge we can put the final piece of the puzzle together. Let's create some assertions, or things that must be true in order for the test to pass. Here's a testcase showing some basic assertions.
现在有更多的Autopilot我们可以做,但拥有这个基础知识我们可以把最后一块拼图找到并拼好。让我们创建一些论证,或为了测试能通过一定是真的事情。这是一个展示一些基础论证的测试用例。

autopilot run helloworld.test_example.ExampleFunctions.test_assert

Finally, there's some standards that are important to know when using autopilot. You'll notice a few things about each testsuite.  
最后,有一些标准在使用Autopilot时我们必须知道。你会注意到一些关于每个测试用例的事情。
· We have a folder named testsuite.
· Inside the folder, we have a file named test_testsuite.py
· Inside the file, we have TestSuite class, with test_testcase_name
· Finally, in order for autopilot to see our testsuite we need to let python know there is a submodule in the directory. Ignoring the geekspeak, we need an __init__.py file (this can be blank if not otherwise needed)
· 我们有一个名为testsuite的文件夹
· 在这个文件夹里,我们有一个名为test_testsuite.py的文件
· 在这个文件里,我们有TestSuite类,和test_testcase_name一起
· 最后,为了Autopilot读取测试用例集,我们需要让python知道目录中有一个子模块。忽略奇客语言,我们需要一个__init__.py文件(如果没有其他需要,这个文件的内容可以为空)。

Given the knowledge we've just acquired, we can tackle our first testcase conversion! For those of you who like to work ahead, you can already see the conversion inside the "firefox" folder. But the details, my dear Watson, will be revealed in due time. Until the next post, cheerio!
鉴于我们刚刚获取的知识,我们可以处理我们的第一个测试用例转换!对于你们中那些想提前工作的,已经可以在“firefox”文件夹中看到转换。而细节,我亲爱的Watson,将在适当的时间透露。

发表于 2013-3-11 09:46:59
回复

使用道具 举报

Getting started with Autopilot - 开始使用Autopilot [复制链接]

抢沙发
发表于 2013-3-11 10:43:16
回复

使用道具 举报

Getting started with Autopilot - 开始使用Autopilot [复制链接]

本帖最后由 Jiaowen520Li 于 2013-3-13 14:31 编辑

"autopilot run helloworld.test_hello.HelloWorld.test_type_hello_world"命令执行的自动化测试过程展示如下:
autopilot-helloworld-example.png
autopilot-helloworld.png
 楼主| 发表于 2013-3-13 14:28:06
回复

使用道具 举报

Getting started with Autopilot - 开始使用Autopilot [复制链接]

Jiaowen520Li 发表于 2013-3-13 14:28
"autopilot run helloworld.test_hello.HelloWorld.test_type_hello_world"命令执行的自动化测试过程展示如 ...

我们可以看出,在执行此命令时,系统自动弹出了一个gedit文本文档,并在打印了"Hello World"后自行关闭。
当执行过程顺利完成后,终端会返回执行此测试所用时间及测试结果(OK or FAILED)。
 楼主| 发表于 2013-3-13 14:37:49
回复

使用道具 举报

openKylin

GMT+8, 2024-3-29 19:20 , Processed in 0.040820 second(s), 21 queries , Gzip On.

Copyright ©2022 openKylin. All Rights Reserved .

ICP No. 15002470-12 Tianjin

快速回复 返回顶部 返回列表