#Chapter 1

>第一章

##A Brief Tour

>快速预览

###About This Chapter

>关于本章

There are a lot of principles, patterns, and smells in this book—and even more patterns that couldn’t fit into the book. Do you need to learn them all? Do you need to use them all? Probably not! This chapter provides an abbreviated introduction to the bulk of the material in the entire book. You can use it as a quick tour of the material before diving into particular patterns or smells of interest. You can also use it as a warm-up before exploring the more detailed narrative chapters.

>本书中有许多原则、模式、代码异味—甚至还有更多的模式不适合出现在本书中。

我想你并不需要全部的学习和使用他们。本章节为整本书的大量内容做了一个精简的介绍。

你可以在深入到特定的模式或代码异味之前,将它作为一个快速预览。你同样可以将它作为探索更详细的叙述章节之前的热身。

###The Simplest Test Automation Strategy That Could Possibly Work

>能使用的最简单的自动化测试策略

There is a simple test automation strategy that will work for many, many projects. This section describes this minimal test strategy. The principles, patterns, and smells referenced here are the core patterns that will serve us well in the long run. If we learn to apply them effectively, we will probably be successful in our test automation endeavors. If we find that we really cannot make the minimal test strategy work on our project by using these patterns, we can fall back to the alternative patterns listed in the full descriptions of these patterns and in the other narratives.

>这是一种可以在许多项目中使用的,简单的自动化测试策略。本节将描述这个最小的测试策略。

这里提及的这些原则、模式和代码异味将是长期为我们服务的核心模式。

如果我们能够学会高效的应用他们,那么我们将在自动化测试的尝试中取得成功。

如果我们发现无法使用这个最小策略将这些模式用于我们的项目,

我们还可以选择一些非传统的模式,他们将在这些模式的完整描述和其他叙述中列出。

I have laid out this simple strategy in five parts:

>我将这个简单策略分为五个部分

* Development Process: How the process we use to develop the code affects our tests.

>开发过程:用于开发影响到测试的代码的过程。

* Customer Tests: The first tests we should write as the ultimate definition of “what done looks like.”

>用户测试:我们应该写的第一个测试,用于最终定义“操作完成后应该是什么情况”

* Unit Tests: The tests that help our design emerge incrementally and

ensure that all our code is tested.

>单元测试:单元测试用于帮助我们逐渐展现出设计意图,并且保证代码是经过测试的。

* Design for Testability: The patterns that make our design easier to test,

thereby reducing the cost of test automation.

>设计代码的可测试性:用于确保代码更容易进行测试的模式,因此也可以降低自动化测试的开销。

* Test Organization: How we can organize our Test Methods (page 348)

and Testcase Classes (page 373).

>测试组织:我们应当如何组织测试方法(348页)和测试类(373页)

###Development Process

>开发过程

First things first: When do we write our tests? Writing tests before we write our software has several benefits. In particular, it gives us an agreed-upon definition of what success looks like.

>重中之重:我们什么时候编写测试?在开始编写软件之前编写测试有几个好处。

特别是他给了我们软件正确的表现的约定。

When doing new software development, we strive to do storytest-driven development by first automating a suite of customer tests that verify the functionality provided by the application. To ensure that all of our software is tested, we augment these tests with a suite of unit tests that verify all code paths or, at a minimum, all the code paths that are not covered by the customer tests. We can use code coverage tools to discover which code is not being exercised and then retrofit unit tests to accommodate the untested code.

>在开发新的软件时,我们应该争取使用一组自动化的用户测试来确保程序所提供的功能正确。

要想保证所有的软件都经过测试,我们还需要增加一组单元测试用于确保所有的代码路径,或者

至少是所有没有被用户测试覆盖的代码路径正确。我们可以使用代码覆盖检测工具来发现

哪些代码没有被测试,然后添加单元测试来包括这些未测试的代码。

By organizing the unit tests and customer tests into separate test suites, we ensure that we can run just the unit tests or just the customer tests if necessary. The unit tests should always pass before we check them in; this is what we mean by the phrase “keep the bar green.” To ensure that the unit tests are run frequently, we can include them in the Smoke Tests [SCM] that are run as part of the Integration Build [SCM]. Although many of the customer tests will fail until the corresponding functionality is built, it is nevertheless useful to run all the passing customer tests as part of the integration build phase—but only if this step does not slow the build down too much. In that case, we can leave them out of the check-in build and simply run them every night.

>我们需要把单元测试和用户测试组织到相互独立的测试组件中,来确保我们可以在有需要的情况下单独运行他们其中的一个。

在我们签入代码之前,必须保证单元测试是通过的;这就是我们平常所说的

“保持这个条是绿色的”。为了确保单元测试被频繁执行,我们可以把它包含到

Smoke Tests[SCM]作为整体构建的一部分运行。许多用户测试都将会失败,直到相关的功能被构建。

尽管如此,把通过用户测试作为成体构建的一部分还是相当有用的,只要他没有使得构建过程变慢太多。

如果测试过程太慢,我们可以将它排除在签入构建之外,仅仅只是每天晚上执行他们。

We can ensure that our software is testable by doing test-driven development (TDD). That is, we write the unit tests before we write the code, and we use the tests to help us define the software’s design. This strategy helps concentrate all the business logic that needs verification in well-defined objects that can be tested independently of the database. Although we should also have unit tests for the data access layer and the database, we try to keep the dependency on the

database to a minimum in the unit tests for the business logic.

>我们可以通过测试驱动开发(TDD)来确保我们的软件是可测试的。测试驱动开发就是在我们编写代码之前先编写单元测试,

并且通过这些测试来帮助我们定义软件的设计。这个策略帮助我们专注于那些需要被验证的业务逻辑,是否被良好定义在可以独立于数据库

进行测试的对象中。尽管我们也需要对数据访问层和数据库进行单元测试,我们还是尽量尝试保持单元测试中得业务逻辑对数据库的依赖最小化。

###Customer Tests

>用户测试

The customer tests should capture the essence of what the customer wants the system to do. Enumerating the tests before we begin their development is an important step whether or not we actually automate the tests, because it helps the development team understand what the customer really wants; these tests define what success looks like. We can automate the tests using Scripted Tests (page 285) or Data-Driven Tests (page 288) depending on who is pre- paring the tests; customers can take part in test automation if we use Data- Driven Tests. On rare occasions, we might even use Recorded Tests (page 278) for regression testing an existing application while we refactor the application to improve its testability. Of course, we usually discard these tests once we have developed other tests that cover the functionality, because Recorded Tests tend to be Fragile Tests (page 239).

>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,204评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,091评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,548评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,657评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,689评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,554评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,302评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,216评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,661评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,851评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,977评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,697评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,306评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,898评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,019评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,138评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,927评论 2 355

推荐阅读更多精彩内容

  • 最近有很多朋友说想来见见我,说请我吃饭,我说参加我的线上培训即可。如果不想参加我的线上培训,接待工作,直接由我们工...
    自寅日记阅读 765评论 0 0
  • 现在我躺在床上,想找一个途径来发泄自我,我一直对自己说,从来没有想过自己会做现在的一份工作,也从来没想过自己会过上...
    粉_刷浆阅读 196评论 0 2
  • 昨天刷一个大V微博,偶然间在评论里有个人插入自己的广告,说自己的父辈是养蜂人,跟随自己的好奇心,我点入他的头像进入...
    白驹过溪阅读 300评论 1 1
  • 燕鸣春日暮,枝间碎羽,似来相诉。 雾蔽横苔,黯然片红飞度。 往岁前番还掷,绪犹缓,行归别路。 提寸句。聊言苦乐,去...
    月中闲笔阅读 194评论 0 0