《Test::Nginx::Socket 测试框架》
Test::Nginx::Socket 是 OpenResty 生态中最常用的单元测试框架,用于测试 nginx 配置与 Lua 代码。
1. 参考文档
- 用户指南:https://metacpan.org/pod/Test::Nginx::Socket
- OpenResty 编程(testing 部分):https://openresty.gitbooks.io/programming-openresty/content/testing/test-nginx.html
Test::Nginx::Socket历史版本:https://metacpan.org/pod/release/AGENT/Test-Nginx-0.24/lib/Test/Nginx/Socket.pm
2. 常用 filter
chomp:去掉末尾换行符
--- error_code: 200
与
--- error_code chomp
200
是等价的。
eval:将 section 的 value 作为 Perl 脚本执行并返回结果
--- response_body eval
"a" x 10
返回长度为 10、内容为 aaaaaaaaaa 的字符串(把 a 复制 10 次)。
添加注释
在 test case 标题下方即可添加注释:
=== TEST 1 hello world
this is the place where make comments
3. no plan
.t 文件开头声明预期数据 section 输出的数量不确定:
use Test::Nginx::Socket 'no_plan';
4. no_shuffle()
让测试用例按其在 .t 文件中出现的顺序依次执行:
no_shuffle();
5. no_diff()
当期望与实际结果不一致时,不以 diff 格式展示,而是把实际与期望全部输出:
no_diff();
diff 格式示例
# Failed test 'TEST 1: - response_body - response is expected (req 0)'
# at .../test-nginx/lib/Test/Nginx/Socket.pm line 1277.
# @@ -1,3 +1,3 @@
# Life is short.
# -Moon is deem.
# +Moon is bright.
# Sun is shining.
# Looks like you failed 1 test of 2.
非 diff 格式示例
# Failed test 'TEST 1: - response_body - response is expected (req 0)'
# at .../test-nginx/lib/Test/Nginx/Socket.pm line 1277.
# got: 'Life is short.
# Moon is bright.
# Sun is shining.
# '
# expected: 'Life is short.
# Moon is deem.
# Sun is shining.
# '
# Looks like you failed 1 test of 2.
6. 压测 Benchmark Mode
安装 weighttp
apt-get install libev-dev
git clone https://github.com/lighttpd/weighttp
cd weighttp
./autogen.sh
./configure
make -j`nproc`
make -j`nproc` install
开启压测
apt-get install expect apache2-utils
# 通过 TEST_NGINX_BENCHMARK 环境变量开启压测模式
export TEST_NGINX_BENCHMARK='2000 2'
prove t/foo.t
阅读 —
·
全站 —