lua-resty-test

基于 OpenResty 的 Nginx_lua 测试框架

$ opm get jie123108/lua-resty-test

名称

lua-resty-test 是基于 Openresty 的 Nginx_lua 测试框架

描述

此 Lua 库是一个测试框架,用于测试您的 ngx_lua 源代码或其他服务器(tcp 或 udp)

http://wiki.nginx.org/HttpLuaModule

概述

    -- test.lua
    local iresty_test    = require "resty.iresty_test"
    local tb = iresty_test.new({unit_name="example"})
    
    function tb:init(  )
        self:log("init complete")
    end
    
    function tb:test_00001(  )
        error("invalid input")
    end
    
    function tb:atest_00002()
        self:log("never be called")
    end
    
    function tb:test_00003(  )
        self:log("ok")
    end
    
    -- units test
    tb:run()

运行测试用例

<img src="./images/run-test-result.png" width="50%" height="50%">

模拟示例

lua-resty-test 为用户测试提供了一个 mock_run 方法。 我们总是需要测试当函数返回错误时,接口是否可以被正确处理。 mock_run 方法可以帮助我们解决这个问题。

示例

    local iresty_test    = require "resty.iresty_test"
    local tb = iresty_test.new({unit_name="example_mock"})
    
    local M = {}
    function M.test()
        return "hello world"
    end
    
    
    function tb:test_00001()
        local function mock_test()
            return "mock test func error"
        end
    
        local mock_rules = {
            { M, "test", mock_test}
        }
    
        local function mock_test_run()
            self:log(M.test())
        end
    
        self:mock_run(mock_rules, mock_test_run)
    end
    
    -- units test
    tb:run()
    

运行模拟测试用例

    resty -Ilib example_mock.lua

<img src="./images/run-mock-test-result.png" width="50%" height="50%">

作者

Yuansheng Wang "membphis" (王院生) membphis@gmail.com, 360 Inc.

版权和许可

此模块采用 BSD 许可证授权。

版权所有 (C) 2016-2019,由 "membphis" (王院生) membphis@gmail.com。

保留所有权利。

在满足以下条件的情况下,允许以源代码和二进制形式重新分发和使用此软件,无论是否修改。

源代码的再分发必须保留上述版权声明、此条件列表和以下免责声明。

二进制形式的再分发必须在随分发提供的文档和/或其他材料中复制上述版权声明、此条件列表和以下免责声明。

本软件由版权持有人和贡献者“按现状”提供,并且任何明示或暗示的担保,包括但不限于对适销性和特定用途适用性的暗示担保,均被放弃。 在任何情况下,版权持有人或贡献者均不对任何直接、间接、偶然、特殊、示例性或后果性损害(包括但不限于获取替代商品或服务的费用;使用、数据或利润损失;或业务中断)负责,无论是在合同、严格责任或侵权(包括疏忽或其他原因)的情况下,即使被告知可能发生此类损害,也与使用本软件有关。

另请参阅

  • ngx_lua 模块:http://wiki.nginx.org/HttpLuaModule

作者

membphis

许可证

2bsd

版本