
cover20220907.png
相關(guān)網(wǎng)站
安裝方法
sudo cpan Test::Nginx
我在掛了梯子之后才安裝成功
目錄結(jié)構(gòu)
在項目根目錄下創(chuàng)建一個文件夾"t"
在這個文件夾下創(chuàng)建若干后綴為".t"的文件即可
如:
└── 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
測試文件結(jié)構(gòu)
測試文件的內(nèi)容由2部分構(gòu)成,上面是Perl代碼,下面是數(shù)據(jù),中間用"__DATA__"分割。
如:
use Test::Nginx::Socket 'no_plan';
run_tests();
# 上面是Perl代碼
__DATA__
# 下面是數(shù)據(jù)
# 每一個測試塊以"==="開頭,后面是測試塊的名稱
=== TEST 1: hello, world
這是一個簡單的測試 # 這行寫描述信息
# 這是一個section,section以"---開頭", 后面接section的名字,不同的section有不同的作用
# 這個section的作用是把下面的配置寫進配置文件的server{}下
--- config
location = /t {
echo "hello, world!";
}
# 這個section發(fā)起了一個請求
--- request
GET /t
# 檢查響應(yīng)內(nèi)容是不是"hello, world!"
--- response_body
hello, world!
# 檢查錯誤碼是不是200
--- error_code: 200
運行測試
配置環(huán)境變量:export PATH=/usr/local/openresty/nginx/sbin:$PATH
使用prove命令來運行,如:prove t/foo.t
如果使用vim, 在vim中輸入命令:!prove %可以運行當前文件