使用 `Test::Nginx` 驱动我们的测试套件,通常使用一个公共的目录结构以及规范的测试文件名样式,用来组织我们的测试集合。

== 测试套件设计

// 翻译中。。。。(yuansheng)

使用 Test::Nginx 驱动我们的测试套件,通常使用一个公共的目录结构以及规范的测试文件名样式,用来组织我们的测试集合。 这些会让用户更容易明白,这些测试用例在项目源码中的目录关系,以及测试用例的使用情况。 它不是必须的,然而,按照这种常见约定组织,还是非常推荐的。

Projects using Test::Nginx to drive their test suites usually have a common directory layout and common test file name patterns to organize their tests. This makes the user easy to reason about the location of the test suite in a project source tree and the usage of the tests. It is not really required, however, to use this common convention; it is just highly recommended.

按照约定,这些项目在当前源码的根目录下,有个 t/ 目录用来存放测试文件。 每个测试文件包含一些具有关联度的测试用例,保存在扩展名为 .t 的文件中,可以很容易表明这是 “测试文件”。 下面的目录树结构,是真实项目 link:https://github.com/openresty/headers-more-nginx-module[headers-more-nginx-module] 的测试套件:

By convention, such projects have a t/ directory at the root of their source tree where test files reside in. Each test file contains test cases that are closely related in some way and has the file extension .t to easily identify themselves as “test files”. Below is the directory tree structure of a real-world test suite inside the link:https://github.com/openresty/headers-more-nginx-module[headers-more-nginx-module] project:

…. └── t    ├── bug.t    ├── builtin.t    ├── eval.t    ├── input-conn.t    ├── input-cookie.t    ├── input-ua.t    ├── input.t    ├── phase.t    ├── sanity.t    ├── subrequest.t    ├── unused.t    └── vars.t ….

当你有很多测试文件,你可以对 t/ 使用子目录拆分完成更深的分组归类。例如,link:https://github.com/openresty/lua-nginx-module[lua-nginx-module] 项目,我们在 t/ 目录下就有 023-rewrite/024-access/ 两个子目录。

When you have many test files, you can also group them further with sub-directories under t/. For example, in the link:https://github.com/openresty/lua-nginx-module[lua-nginx-module] project, we have sub-directores like 023-rewrite/ and 024-access/ under its t/ directory.

本质上,每一个 .t 文件是一个 Perl 脚本文件,它的执行由 perl 或者 Perl 世界比较普遍的工具名叫 link:http://perldoc.perl.org/prove.html[prove] 完成的。 我们通常使用 prove 命令行工具来执行这些 .t 文件来获取测试结果。 尽管这些 .t 文件每一个都是 Perl 脚本,但他们通常都不包含任何 Perl 脚本。 取而代之的,这里声明的所有测试用例,仅仅是放在 .t 文件中统一格式的基本 “数据”。

In essence, each .t file is a Perl script file runnable by either perl or Perl’s universal test harness tool named link:http://perldoc.perl.org/prove.html[prove]. We usually use the prove command-line utility to run such .t files to obtain test results. Although .t files are Perl scripts per se, they usually do not have much Perl code at all. Instead, all of the test cases are declared as cleanly formatted “data” in these .t files.

注意:我们这里使用的测试套件设计约定,在 Perl 社区已经延用多年。 因为 Test::Nginx 使用 Perl 编写的,并且在 Perl 的测试工具链中被重用,我们只需要让 NGINX 和 OpenResty 世界简单的遵循这些约定就好。

NOTE: The test suite layout convention we use here are also used by the Perl community for many years. Because Test::Nginx is written in Perl and reuses Perl’s testing toolchain, it makes sense for us to simply follow that convention in the NGINX and OpenResty world as well.