plan tests => repeat_each() * (blocks() * 6 + 4);
一个测试case就是一个测试block, 一个block由多个section组成,
repeat_each()返回之前设置的重复次数, 一个测试文件有2个测试case, 每个重复2次, 那就是总共测试4次,
那么后面block()函数返回的是什么值? plan tests 到底要做什么?
Test::Nginx中的block()函数
Q:block()函数返回的是什么值?
A: blocks()
函数返回的是该 .t
文件中「测试块」的数量,所谓「测试块」就是 __DATA__
中对应的 === TEST 1: xxx
/ === TEST 2: xxx
这样的数据块,详细的说明可见 :https://metacpan.org/pod/distribution/Test-Base/lib/Test/Base.pod#blocks(-[data-section-name]-)
Q:plan tests 到底要做什么?
A:plan tests 用来指定期望测试项目的数量,防止测试框架中有以 exit code 0
退出(不会触发报错)的测试用例,导致剩余用例未运行却未被发现
xiaocang 谢谢大佬回复, 但是nginx-lua-moudle的test中很多都是
repeat_each(2);
plan tests => repeat_each() * (blocks() * 2);
(https://github.com/openresty/lua-nginx-module/blob/master/t/007-md5.t)
这种还是看不懂,md5.t中总共7个block, 每个重复2次,那么总共测试14次才对,但是 plan test 中 block() 除了 repeat 又额外乘了2是为什么,
还有很多测试文件是这种的
repeat_each(2);
plan tests => repeat_each() * (blocks() * 3 + 4);
(https://github.com/openresty/lua-nginx-module/blob/master/t/009-log.t)
后面还加个数,就更不明白是怎么回事了
LubinLew blocks() * 3 + 4) 表示每个block中一般有3个assert,后面的+4表示个别block
block中的assert数量不为3,可能多了或者少了,在这里做加减法匹配。
hackzhuyan ok, 明白了,谢谢大佬
hackzhuyan 大佬那个assert没搞懂, 什么算assert ?
LubinLew assert
这个概念可以见 https://metacpan.org/pod/Test::More
Before anything else, you need a testing plan. This basically declares how many tests your script is going to run to protect against premature failure.
顺便说一下,Test::Nginx 是在 Test::Base 和 Test::More 基础上封装了一层,很多基础的概念都是从这两个框架借来的