-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
53 additions
and
1 deletion.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
### 前端的TDD开发探索 | ||
|
||
> 前言:`TDD` 测试驱动开发。 | ||
## 目录 | ||
|
||
* [What? 测试/TDD](#What? 测试/TDD) | ||
* [Why? 测试/TDD](#Why? 测试/TDD) | ||
* [单元测试](#单元测试) | ||
|
||
|
||
|
||
## What? 测试/TDD | ||
|
||
什么是测试?我们开发者描述的开发过程中测试,一般来说: | ||
是编写一段检测你应用代码是否按预期执行的代码并执行的过程。 | ||
|
||
什么是TDD?是测试驱动开发的英文简写。一般来说: | ||
是在编写生产代码之前,先用一段测试代码来描述应用的执行流程,然后再让生产代码按测试代码的意图来走通逻辑流程。 | ||
|
||
## Why? 测试/TDD | ||
|
||
因为你可以通过编写测试来使你的应用更可靠,更容易重构。 | ||
|
||
## 单元测试 | ||
|
||
单元测试就是以代码模块为单元进行测试。给单元测试一个输入,让它自动执行,将输出结果和预期结果做对比看其是否正确(输入可以是一个函数参数,输出就是函数的返回值)。 | ||
|
||
### 使用测试框架 | ||
|
||
我们在编写测试代码时候,需要使用测试框架,目前比较流行的框架是Mocha,另外还有一些像 Jest/Jasmine/Tape 等框架也可以使用。 | ||
|
||
### 使用 Mocha 进行单元测试 | ||
|
||
我们一般需要在命令行运行我们的测试框架启动,为了节省时间,我们把启动代码保存到 npm script 中,也就是`package.json`。 | ||
|
||
``` | ||
"scripts": { | ||
... | ||
"test": "mocha 'test/**/test-*.js' && eslint test lib", | ||
... | ||
}, | ||
``` | ||
|
||
上面的命令行 会 运行 test文件夹中所有以 `test-` ,把所有测试都放在测试目录是一个不错的做法。 | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters