# 单元测试与功能测试的区别

单元测试和功能测试之间的界线有时不那么清晰。

不过二者之间有个基本区别：功能测试站在用户角度从外部测试应用，单元测试则站在程序员的角度从内部测试应用。&#x20;

采用的工作流程大致如下：

* 1）先写功能测试，从用户的角度描述应用的新功能&#x20;
* 2）功能测试失败后，想办法编写代码让它通过（或者说至少让当前失败的测试通过）。此时，使用一个或多个单元测试定义希望代码实现的效果，保证为应用中的每一行代码（至少）编写一个单元测试
* 3）单元测试失败后，编写最少量的应用代码，刚好让单元测试通过。有时，要在第2步和第3步之间多次往复，直到我们觉得功能测试有一点进展为止。
* 4）然后，再次运行功能测试，看能否通过，或者有没有进展。这一步可能促使我们编写一些新的单元测试和代码等。

由此可以看出，在整个过程中，功能测试站在高层驱动开发，而单元测试从低层驱动我们做些什么。

> TDD的重要思想是必要时一次只做一件事。即每次只做必要的操作，让功能测试向前迈出一小步即可。

### 参考资料

[Python测试驱动开发(TDD)](https://blog.csdn.net/weixin_41845533/article/details/81232812#%E9%A1%B9%E7%9B%AEgithub%E5%9C%B0%E5%9D%80)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tdd.shujuwajue.com/shi-jian/dan-yuan-ce-shi-yu-gong-neng-ce-shi-de-qu-bie.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
