|
| 1 | +== Test::Nginx |
| 2 | + |
| 3 | +link:https://metacpan.org/pod/Test::Nginx[Test::Nginx] 是一个测试驱动框架,可以驱动运行在 NGINX 上的任何代码,当然也可以是 NGINX 的内核代码。它使用 Perl 语言编写的,数年时间的积累,使得它丰富的测试设备,以及完备的周边工具链。更进一步,使用者都不需要知道这些测试用例架构是用 Perl 书写的,`Test::Nginx` 提供了非常简单符号来表达当前测试用例,并把它们用一种规范格式组织起来。 |
| 4 | + |
| 5 | +link:https://metacpan.org/pod/Test::Nginx[Test::Nginx] is a test framework |
| 6 | +that drives test cases written for any |
| 7 | +code running atop NGINX, and also, naturally, the NGINX core itself. It |
| 8 | +is written in Perl because of the rich testing facilities and toolchain |
| 9 | +already accumulated in the Perl world for years. Fortunately, the user |
| 10 | +does not really need to know Perl for writing test cases atop this scaffold |
| 11 | +since `Test::Nginx` provides a very simple notation to present the test |
| 12 | +cases in a specification-like format. |
| 13 | + |
| 14 | +这种简单测试规范格式或称语言,让 `Test::Nginx` 的存在更像一种方言,尤其是它与 Perl 世界里提供更普遍的 link:https://metacpan.org/pod/distribution/Test-Base/lib/Test/Base.pod[Test::Base] 测试语言。 |
| 15 | + |
| 16 | +The simple test specification format, or language, used in `Test::Nginx` |
| 17 | +is just a dialect of the more general testing language provided by the |
| 18 | +link:https://metacpan.org/pod/distribution/Test-Base/lib/Test/Base.pod[Test::Base] |
| 19 | +testing module in the Perl world. |
| 20 | + |
| 21 | +事实上,用面向对象的角度看,`Test::Nginx` 仅仅是 `Test::Base` 子类。 |
| 22 | +In fact, `Test::Nginx` is just a subclass |
| 23 | +of `Test::Base` in the sense of object-oriented programming. |
| 24 | + |
| 25 | +这就意味着 `Test::Base` 提供的所有特性在 `Test::Nginx` 都是可用的,`Test::Nginx` 仅仅提供更易用的原语和符号,用来简化 NGINX 和 OpenResty 环境的测试。 |
| 26 | +This means |
| 27 | +that all the features offered by `Test::Base` is available in `Test::Nginx` |
| 28 | +and `Test::Nginx` just provides handy primitives and notations that simplify |
| 29 | +testing in the NGINX and OpenResty context. |
| 30 | + |
| 31 | +`Test::Base` 的核心理念,是基于 `Test::Base` 的测试系统可以被不同项目广泛、有效的使用,甚至包括 Haskell 编程和 Linux 内核模块。`Test::Nginx` 只是我们为了 NGINX 和 OpenResty 世界创造的例子。有关 `Test::Base` 框架自身的详细讨论,已经超过了这本书的范畴,但在后续章节中,我们还将介绍 `Test::Nginx` 继承下来有关 `Test::Base` 的重要特性。 |
| 32 | +The core idea of `Test::Base` |
| 33 | +is so useful that we have been using testing scaffolds based on `Test::Base` |
| 34 | +in many different projects even including Haskell programs and Linux kernel |
| 35 | +modules. `Test::Nginx` is such an example we created for the NGINX and |
| 36 | +OpenResty world. Detailed discussion of the `Test::Base` framework itself |
| 37 | +is beyond the scope of this book, but we will introduce the important features |
| 38 | +of `Test::Base` that are inherited by `Test::Nginx` in the later sections. |
| 39 | + |
| 40 | +`Test::Nginx` 通过 link:http://www.cpan.org/[CPAN] 发行,CPAN(Comprehensive |
| 41 | +Perl Archive Network)。如同其他大多数 Perl 的库,如果在你的系统中已经安装了 `perl` (大多数 Linux 版本已经默认包含了 `perl`),这时你可以使用下面简单命令完成安装: |
| 42 | +`Test::Nginx` is distributed via link:http://www.cpan.org/[CPAN], the Comprehensive |
| 43 | +Perl Archive Network, just like most of the other Perl libraries. If you |
| 44 | +already have `perl` installed in your system (many Linux distributions |
| 45 | +ship with `perl` by default), then you can install `Test::Nginx` with the |
| 46 | +following simple command: |
| 47 | + |
| 48 | +[source,bash] |
| 49 | +---- |
| 50 | +cpan Test::Nginx |
| 51 | +---- |
| 52 | + |
| 53 | +对于第一次运行 `cpan` 工具,可能它将提示你配置 `cpan` 工具,以此来适配你的环境。如果你不确定这些选项,只选择默认配置选项(如果可用)或者接受所有默认选项。 |
| 54 | +For the first time that the `cpan` utility is run, you may be prompted |
| 55 | +to configure the `cpan` utility to fit your requirements. If you are unsure |
| 56 | +about those options, just choose the automatic configuration option (if |
| 57 | +available) or just accept all the default settings. |
| 58 | + |
| 59 | +`Test::Nginx` 针对不同用户环境,提供了几个不同的测试类。最经常被使用的一个是 `Test::Nginx::Socket` 。本章的剩余部分焦点将集中在测试类以及它的子类。 |
| 60 | +`Test::Nginx` provides several different testing classes for different |
| 61 | +user requirements. The most frequently used one is `Test::Nginx::Socket`. |
| 62 | +The rest of this chapter will focus on this testing class and its subclasses. |
| 63 | +We will use the names `Test::Nginx` and `Test::Nginx::Socket` interchangeably |
| 64 | +from now on to mean the `Test::Nginx::Socket` test module and its subclasses, |
| 65 | +unless otherwise specified. |
| 66 | + |
| 67 | +// Alas. GitBook does not support sidebar blocks in its AsciiDoc render. |
| 68 | +// .Another Test::Nginx |
| 69 | + |
| 70 | +NOTE: There is actually another different testing scaffold called `Test::Nginx`, |
| 71 | +created by Maxim Dounin and maintained by the official NGINX team. That |
| 72 | +testing module is shipped with the link:http://hg.nginx.org/nginx-tests/file/tip[official |
| 73 | +NGINX test suite] and has no |
| 74 | +relationship with our `Test::Nginx` except that both of these are meant |
| 75 | +to test NGINX related code. The NGINX team's `Test::Nginx` requires the |
| 76 | +user to directly code in Perl to convey all the test cases, which means |
| 77 | +that tests written for their `Test::Nginx` are not data driven and requires |
| 78 | +decent knowledge about Perl programming. |
0 commit comments