wip-3.0
zeroChapter 3 The anatomy of a unit test
第 3 章 单元测试的结构
This chapter covers:
本章内容包括:
- The structure of a unit test
单元测试的结构。 - Unit test naming best practices
单元测试命名最佳实践。 - Working with parameterized tests
使用参数化测试。 - Working with fluent assertions
使用流式断言。
In this remaining chapter of part 1, I’ll give you a refresher on some basic topics. I’ll go over the structure of a typical unit test, which is usually represented by the arrange, act, and assert (AAA) pattern. I’ll also show the unit testing framework of my choice—xUnit—and explain why I’m using it and not one of its competitors.
在第 1 部分剩下的这一章中,我会帮你回顾一些基础主题。我会讲解典型单元测试的结构,它通常由准备、执行、断言(AAA)模式表示。我还会展示我选择的单元测试框架 xUnit,并解释为什么我使用它,而不是其他竞争框架。
Along the way, we’ll talk about naming unit tests. There are quite a few competing pieces of advice on this topic, and unfortunately, most of them don’t do a good enough job improving your unit tests. In this chapter, I describe those less-useful naming practices and show why they usually aren’t the best choice. Instead of those practices, I give you an alternative—a simple, easy-to-follow guideline for naming tests in a way that makes them readable not only to the programmer who wrote them, but also to any other person familiar with the problem domain.
在这个过程中,我们还会讨论单元测试命名。关于这个主题有不少彼此竞争的建议,但遗憾的是,大多数建议并不能很好地改进你的单元测试。本章会描述那些不太有用的命名实践,并说明为什么它们通常不是最佳选择。作为替代,我会给出一条简单、容易遵循的命名指南,让测试名称不仅对编写它们的程序员可读,也对任何熟悉问题领域的人可读。
Finally, I’ll talk about some features of the framework that help streamline the process of unit testing. Don’t worry about this information being too specific to C# and .NET; most unit testing frameworks exhibit similar functionality, regardless of the programming language. If you learn one of them, you won’t have problems working with another.
最后,我会讨论该框架中一些有助于简化单元测试过程的功能。不要担心这些信息过于特定于 C# 和 .NET;无论使用哪种编程语言,大多数单元测试框架都具有类似功能。如果你学会了其中一个,使用另一个也不会有问题。