关于openresty单元测试Test::Nginx一些理解

openresty团队基于perl编写的数据驱动测试框架:https://github.com/openresty/test-nginx

1、安装perl,linux环境默认已经安装perl,window安装参考:https://www.runoob.com/perl/perl-environment.html

2、配置环境变量,使用perl的cpan工具安装Test::Nginx,未安装下载最新版本即可

cpan Test::Nginx

3、下载openresty并安装

4、配置nginx执行文件到环境变量,目的:在任意文件夹下能执行nginx命令

可以加软连接:ln -s /usr/local/openresty/nginx/sbin/nginx /usr/bin/nginx

5、在openresty目录增加:

mkdir -p /usr/local/openresty/t/servroot

6、新建test.t文件

use Test::Nginx::Socket 'no_plan';

run_tests();

__DATA__

=== TEST 1: hello, world

This is just a simple demonstration of the

echo directive provided by ngx_http_echo_module.

--- config

location = /t {

echo "hello, world!";

}

--- request

GET /t

--- response_body

hello, world!

--- error_code: 200

6、使用系统自带perl命令的prove命令执行test.t

/usr/bin/prove test.t

输出:

test.t .. ok

All tests successful.

Files=1, Tests=2, 0 wallclock secs ( 0.01 usr 0.00 sys + 0.12 cusr 0.03 csys = 0.16 CPU)

Result: PASS

 

验证成功

 

个人理解:Test::Nginx是基于perl语言的测试框架Test::Base的一个子分支,专门对于nginx-lua应用进行的测试框架,原理是将测试用例中的代码注入到nginx配置文件生成一个新的nginx启动所需目录以及配置文件,默认使用了1984端口,相当于启动了一个新的nginx服务,因为nginx启动和停止非常轻量级,所以使用这种测试方式非常轻便高效率,具体详细介绍可参考:https://metacpan.org/pod/Test::Nginx::Socket

posted @ 2020-05-22 14:26  blissnmx  阅读(490)  评论(0)    收藏  举报