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