eos测试规格

by Edd Yerburgh

埃德·耶堡(Edd Yerburgh)

希望您的测试更有效? 这样写您的规格。 (Want your tests to be more effective? Write your specifications like this.)

Writing test specifications is tricky. If you get it right, your tests are easy to understand and debug. But get it wrong, and your tests will be confuse people more than they’ll help them.

编写测试规范非常棘手。 如果您做对了,您的测试很容易理解和调试。 但是弄错了,您的测试会使人们更加困惑,而不是帮助他们。

In this article, I’ll show you how to write expressive test specifications.

在本文中,我将向您展示如何编写表达性测试规范。

什么是测试规格? (What are test specifications?)

Test specifications (specs) are the string used to identify tests when they’re run by a test runner.

测试规范(specs)是由测试运行程序运行时用于标识测试的字符串。

Below you can see an example of the output from a failed test. You can see where the specification and assertion error is used to describe how a test failed.

在下面,您可以看到测试失败的输出示例。 您可以看到规范和断言错误用于描述测试失败的位置。

为什么测试规格很重要? (Why are test specifications important?)

When a test fails, the way you identify it is with the test specification.

如果测试失败,则可以通过测试规范来识别它。

If the specification is well-written, you’ll know straight away why the test failed by using the test specification and the test assertion.

如果规范写得好,您将通过使用测试规范和测试断言直接了解为什么测试失败。

calls showModal when button is clickedError: Expected spy to have been called but it was not.

We can guess that the test failed because showModal wasn’t called when the button was clicked. This debugability is what you should aim for in tests.

我们可以猜测测试失败,因为单击按钮时未调用showModal。 这种可调试性是您在测试中应该追求的目标。

Lets look at some rules to help write spectacular test specifications.

让我们看一些规则,以帮助编写出色的测试规范。

金发姑娘规则 (The Goldilocks rule)

You should follow the Goldilocks rule for test specifications—not too general and not too specific.

您应该遵循Goldilocks规则来确定测试规范,不要太笼统,也不要太具体。

For example, does what I expect is too general. You won’t know why the test failed or what the test was checking.

例如, does what I expect是否太笼统了。 您将不知道为什么测试失败或测试正在检查什么。

At the same time, you need to avoid being too specific. Instead of usingadds cache-control none header and vary Lang header, use a less narrow specification, like adds correct headers .

同时,您需要避免过于具体。 与其使用adds cache-control none header and vary Lang header ,不如使用狭窄的规范,例如adds correct headers

A failing test has two parts. The test specification, and the error message.

测试失败包括两个部分。 测试规范和错误消息。

adds correct headers Error: Expected something to equal none

Your test name should tell us what is happening. But it doesn't need to give us every detail. The assertion error should include a value that compliments the test specification.

您的测试名称应告诉我们正在发生的事情。 但这并不需要给我们每一个细节。 断言错误应包含一个符合测试规范的值。

保持简短 (Keep them short)

Specifications should be short.

规格应简短。

My rule of thumb is that they shouldn’t be longer than 150 characters.

我的经验法则是,它们的长度不能超过150个字符。

If your tests are more than 150 characters, there’s a chance that your units are too complex. Either rewrite the spec to be shorter, or break out the functionality of your units into smaller chunks.

如果您的测试超过150个字符,则可能是您的单元太复杂了。 重写规范以使其更短,或者将单元的功能分解为较小的块。

用现在时写 (Write in the present tense)

Your test specs should be in the present tense.

您的测试规格应采用现在时。

For example, calls toggleModal when button is clicked , not will call toggleModal when button is clicked .

例如, calls toggleModal when button is clickedwill call toggleModal when button is clicked

Specifications are statements of how your unit behaves: returns sum of input .

规格说明您的设备如何运行: returns sum of input

Writing in the present tense makes your specifications shorter and easier to read.

以现在时书写使您的规范更短,更容易阅读。

专注于输出和输入 (Focus on output and input)

Tests should trigger an input and expect an output.

测试应触发输入并期望输出。

Your specifications should follow this pattern—output when input. For example calls toggleModal when button is clicked , or returns true when called with string .

您的规范应遵循此模式- 输入时输出 。 例如, calls toggleModal when button is clicked ,或者returns true when called with string calls toggleModal when button is clicked returns true when called with string

Keeping to this standard ensures your tests focus on output and input.

遵守此标准可确保您的测试集中在输出和输入上。

简明扼要 (Be concise)

You don’t have much space in test specs, so stay away from unnecessary words.

您在测试规范中没有太多空间,因此请远离不必要的文字。

For example, don’t add filler words like should, or will.

例如,请勿添加应有或将会的填充词。

should call showmodal when clicked

should call showmodal when clicked showmodal

will call show modal when clicked

will call show modal when clicked call will call show modal when clicked

calls show modal when clicked

calls show modal when clicked

不要使用嵌套的描述块 (Don’t use nested describe blocks)

A lot of Javascript testing libraries include a feature called describe blocks.

许多Javascript测试库都包含一个称为describe块的功能。

describe blocks define sections in your tests.

describe块定义测试中的部分。

A describe block like the one below:

描述块,如下所示:

describe('sum', () => {  test('returns sum of input', () => {    expect(sum(1,2)).toBe(3)  })})

It creates the following console output:

它创建以下控制台输出:

You should use describe blocks to define a test suite in a file.

您应该使用describe块在文件中定义测试套件。

Some developers nest describe blocks inside each other to organize their tests. Never do this.

一些开发人员在彼此内部嵌套描述块以组织测试。 永远不要这样做。

Instead of writing tests like this:

而不是像这样编写测试:

describe('API', () => {  describe('/books', () => {    describe('/id', () => {      describe('not found', () => {        test('returns 404', () => {          expect(4).toBe(4)        })      })    })  })})

Write tests like this:

编写这样的测试:

describe('API', () => {  test('returns 404 when /books/id is not found', () => {    expect(4).toBe(4)  })})

Never nest describe blocks. Maintaining twenty or thirty tests in files with nested describe blocks is really confusing. You waste time deciding what block a new test should go in, and it’s easy to accidentally delete a closing curly brace.

切勿嵌套描述块 。 在带有嵌套描述块的文件中维护二十或三十个测试确实令人困惑。 您浪费时间来决定应加入新测试的块,并且很容易意外删除右花括号。

Nested describe blocks add unnecessary cognitive load.

嵌套的描述块会增加不必要的认知负担。

针对不同类型的测试编写不同的规范 (Write different specs for different types of tests)

There are two types of test specifications you’ll write — high-level specs, and developer-level specs.

您将编写两种类型的测试规范- 高级别规范开发人员级别规范

End to end tests need high-level specs. The actions that end to end tests perform are high-level, and the specification should match that.

端到端测试需要高级规范。 端到端测试执行的操作是高级别的,规范应与此相匹配。

High-level specifications are the kind of specs your manager might give you — the modal opens when the user clicks a button. You could show your manager the specs, and he’d understand what the test is for.

高级规范是您的经理可能会给您的规范- the modal opens when the user clicks a buttonthe modal opens when the user clicks a button 。 您可以向您的经理展示规格,他会了解测试的目的。

On the other hand, unit tests need developer-level specifications.

另一方面,单元测试需要开发人员级别的规范。

Unit tests check how functions in our code work. They’re low level, so the specifications should reflect that.

单元测试检查代码中的功能如何工作。 它们是低级别的,因此规格应该反映出来。

Developer-level specs only make sense to other developers, such asbutton should trigger action with displayModal true when clicked. They can mention concepts that don’t make sense to non-devs. They can use terms like Boolean, and throws error.

开发人员级别的规范仅对其他开发人员有意义,例如, button should trigger action with displayModal true when clickedbutton should trigger action with displayModal true when clicked 。 他们可能会提到对非开发人员没有意义的概念。 他们可以使用Boolean术语,并throws error

Think of unit tests as documentation for future developers, think of end to end tests as documentation for future project managers. And make sure your specifications reflect that.

将单元测试视为未来开发人员的文档,将端到端测试视为未来项目经理的文档。 并确保您的规格反映了这一点。

呼吁采取行动 (Call to action)

Now that you know how to write high quality test specifications, go out and write some tests! If you don’t know how to write tests, the getting started guide from Jest is a great place to start.

现在您知道如何编写高质量的测试规范,请出去编写一些测试! 如果您不知道如何编写测试,那么Jest入门指南就是一个不错的起点。

翻译自: https://www.freecodecamp.org/news/want-your-tests-to-be-more-effective-write-your-specifications-like-this-5d701a961e35/

eos测试规格

eos测试规格_希望您的测试更有效? 这样写您的规格。相关推荐

  1. java如何做测试数据库_如何模拟用于测试的数据库(Java)?

    我在Java编程,我的应用程序正在做很多使用DB.因此,重要的是我能够轻松地测试我的数据库使用. 什么是数据库测试?对我来说,他们应该提供两个简单的要求: >验证SQL语法. >更重要的是 ...

  2. junit 5测试异常处理_使用JUnit 5测试异常

    junit 5测试异常处理 JUnit 5带来了令人敬畏的改进,并且与以前的版本有很大不同. JUnit 5在运行时需要Java 8,因此Lambda表达式可以在测试中使用,尤其是在断言中. 这些断言 ...

  3. 怎样编写测试类测试分支_编写干净的测试–天堂中的麻烦

    怎样编写测试类测试分支 如果我们的代码有明显的错误,我们很有动力对其进行改进. 但是,在某些时候,我们认为我们的代码"足够好"并继续前进. 通常,当我们认为改进现有代码的好处小于所 ...

  4. 怎样编写测试类测试分支_编写干净的测试-被认为有害的新内容

    怎样编写测试类测试分支 很难为干净的代码找到一个好的定义,因为我们每个人都有自己的单词clean的定义. 但是,有一个似乎是通用的定义: 简洁的代码易于阅读. 这可能会让您感到有些惊讶,但我认为该定义 ...

  5. 怎样编写测试类测试分支_编写干净的测试–从配置开始

    怎样编写测试类测试分支 很难为干净的代码找到一个好的定义,因为我们每个人都有自己的单词clean的定义. 但是,有一个似乎是通用的定义: 简洁的代码易于阅读. 这可能会让您感到有些惊讶,但我认为该定义 ...

  6. 网络分析仪测试线损_关于网络分析仪的测试(校准线损问题)

    关于网络分析仪的测试(校准线损问题) ljq200 Post at 2007/7/4 0:48:47 今天有机会用了一下网络分析仪,HP7853E的,感觉还好,想用它来测测天线的匹配,先是用校准器来校 ...

  7. 前端如何实现网络速度测试功能_分析Web前端测试要点,从架构原理上进行分析,希望大家能够掌握...

    基于Web前端分析过程,大概有十几个测试要点,我们今天主要来讲解结合前五个要点进行详细解说.前端测试点主要针对前端展开,什么叫前端分析呢?就是我们所有的分析和测试要点所站的视角都是针对客户端或者浏览器 ...

  8. 3测试原理_智能手表防水测试方案(气密性测试)

    理论依据 •当被检测器件出现泄漏时,密闭腔体内会出现气体摩尔量的损失. •在宏观上则表现为气体压力的降低.故,我司通过精确地测量压力从而达到精密测漏的目的. •直压压损法和定量差压法依据着相同的测试原 ...

  9. pythontdd测试命名_荐Pytest之测试命名规则

    背景: pytest以特定规则搜索测试用例,所以测试用例文件.测试类以及类中的方法.测试函数这些命名都必须符合规则,才能被pytest搜索到并加入测试运行队列中. 默认搜索规则: 如果pytest命令 ...

最新文章

  1. android自带蓝牙例子详解
  2. Intel汇编语言程序设计学习-第五章 过程-上
  3. python中json.load()、json.loads()、json.dump()、json.dumps()的区别
  4. 得到进程id_搞懂进程组、会话、控制终端关系,才能明白守护进程干嘛的?
  5. matlab excel 新建sheet,MATLAB怎么在保存结果的EXCEL里面添加内容?比如把 'sheet1‘ 改成 ‘已知点’ ,在第一行加上 '已知点' ,'x', 'y' 等....
  6. 基于vue与element ui的vue-cron插件的使用及将定时任务cron表达式解析成中文
  7. 测试用例方法-等价类划分
  8. Linux中文显示:解决Windows传到linux文件中文乱码
  9. 好爽 java_JAVA Web学习(27)___第21章清爽夏日九宫格日记网
  10. 用微信公众号控制ESP8266的LED,进一步使用微信当遥控器
  11. 论文解读:MOEA/D-TPN
  12. 安装SQL 2000挂起的解决办法
  13. Ant Design - Authorized
  14. 简论H.266与H.265、AV1、H.264对比
  15. tp5的时间查询,查询时间戳是否在某一天中
  16. python程序设计实用教程清华大学出版社_清华大学出版社-图书详情-《Python程序设计简明教程》...
  17. 观看2022年卡塔尔世界杯的感想
  18. 信息学奥赛一本通题库1005 地球人口承载力估计
  19. 【问题解决】ESP32报错:make: xtensa-esp32-elf-gcc: Command not found
  20. 推荐一款工作学习中十分好用的插件--uTools

热门文章

  1. jdbc操作演示 mysql
  2. 关于.NET框架的主要组件 10:45:02
  3. 集合的体系结构 0119
  4. 控件的布局 1124
  5. python-jieba分词模块
  6. mysql安装过程-zip安装
  7. Laravel中优雅的验证日期需要大于今天
  8. 新手教程——在Linux Mint 16中找到保存的WiFi密码
  9. 时间:2014年3月27日20:08:01网站建表实战与优化意识
  10. 工作与生活 -- 平衡是必须的