请问如何使用OpenResty自带的resty工具来进行mock测试?

189 views
Skip to first unread message

Smart Brave

unread,
Dec 12, 2016, 4:44:27 AM12/12/16
to openresty
最近需要测试一些lua代码,而这些代码需要配合Nginx来跑,并依赖Nginx的运行时环境。因为比较难以模拟,因此想到了用resty来进行mock测试,请问具体的使用方法是怎样的呢?

Ming

unread,
Dec 12, 2016, 5:12:48 AM12/12/16
to open...@googlegroups.com
试试 https://github.com/openresty/test-nginx, 官方的所有模块和 resty 库都是用 Test::Nginx 来驱动的。

在 2016年12月12日 下午5:44,Smart Brave <smartbr...@gmail.com>写道:
最近需要测试一些lua代码,而这些代码需要配合Nginx来跑,并依赖Nginx的运行时环境。因为比较难以模拟,因此想到了用resty来进行mock测试,请问具体的使用方法是怎样的呢?

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty+subscribe@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+unsubscribe@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html

Smart Brave

unread,
Dec 12, 2016, 5:33:47 AM12/12/16
to openresty
Test::Nginx库应该只是用来测试Nginx的C模块的吧?我现在的需求是要测试自己写的lua代码,并屏蔽掉外部接口。比如说有“require('cfg')”,我想让他不去包含真实的cfg模块,该如何做呢?

在 2016年12月12日星期一 UTC+8下午6:12:48,WenMing写道:
试试 https://github.com/openresty/test-nginx, 官方的所有模块和 resty 库都是用 Test::Nginx 来驱动的。
在 2016年12月12日 下午5:44,Smart Brave <smartbr...@gmail.com>写道:
最近需要测试一些lua代码,而这些代码需要配合Nginx来跑,并依赖Nginx的运行时环境。因为比较难以模拟,因此想到了用resty来进行mock测试,请问具体的使用方法是怎样的呢?

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com

Ming

unread,
Dec 12, 2016, 9:15:09 PM12/12/16
to open...@googlegroups.com
Test::Nginx 可以测试 Lua 代码。你这个需求我不知道怎么搞...

订阅: 请发空白邮件到 openresty+subscribe@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+unsubscribe@googlegroups.com

Bingwu Yang

unread,
Dec 14, 2016, 12:58:53 AM12/14/16
to open...@googlegroups.com
Hello.

Test::Nginx 可以实现你的需求,只要你的想象力足够大:), 依赖于 lua vm 的 package.loaded 全局变量,
在 master init 阶段 require 一次模块,然后替换这个模块为你的 mock
的模块即可。下面是从运行在生产的代码的范例(无法运行,删除了隐私部分)。

our $http_config = <<"_EOC_";
lua_package_path '$workdir/?.lua;;';
lua_package_cpath '$workdir/xx/clib/?.so;;';
init_by_lua_block {
-- we mock the socket
require("xx.lib.resty.logger.socket")
package.loaded["xx.lib.resty.logger.socket"] =
require("xx.mock.resty-logger-socket")
local pid = ngx.worker.pid
ngx.worker.pid = function()
return 1993
end
}
_EOC_

repeat_each(1);
no_shuffle();
run_tests();

__DATA__

=== TEST1: test 1
--- http_config eval: $::http_config

--- config
location = /app/active {
content_by_lua_block {
xxxx
}

}

......
Reply all
Reply to author
Forward
0 new messages