tests

package
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 28, 2026 License: BSD-3-Clause Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Package tests

Support for writing lua tests like go tests; any function starting with Test will be run.

Example go code

func TestApi(t *testing.T) {
preload := tests.SeveralPreloadFuncs(
inspect.Preload,
strings.Preload,
)
assert.NotZero(t, tests.RunLuaTestFile(t, preload, "./test/test_api.lua"))
}

Example lua code

function TestFoo(t)
    t:Log("foo bar baz")
    assert(someVariable, tostring(someVariable))
    expected = 2
    assert(somethingElse == expected, string.format("%d ~= %d", somethingElse, expected))
end

function TestMaybe(t)
    if os.getenv('SKIP_IT') then
        t:Skip("Skipped because SKIP_IT is defined")
    end
    assert(theActualTest, tostring(theActualTest))
end

Example use of suite

To simulate testify.suite hooks, a simple suite implementation is preloaded as well

local suite = require 'suite'

local MySuite = suite.Suite:new {
    setupCount = 0,
    setupSuiteCount = 0,
    tearDownCount = 0,
    tearDownSuiteCount = 0,
}

function MySuite:SetupSuite()
    self.setupSuiteCount = self.setupSuiteCount + 1
end

function MySuite:TearDownSuite()
    self.tearDownSuiteCount = self.tearDownSuiteCount + 1
end

function MySuite:SetupTest()
    self.setupCount = self.setupCount + 1
end

function MySuite:TearDownTest()
    self.tearDownCount = self.tearDownCount + 1
end

function MySuite:TestFoobar()
    -- T is available from superclass for suites; not passed in as arg
    self:T():Log('TestFoobar')
end

function MySuite:TestBaz()
    self:T():Log('TestBaz')

    self:Run('sub1', function()
        self:T():Log('sub1')
    end)

    self:Run('sub2', function()
        self:T():Log('sub2')
    end)
end

function TestSuite(t)
    -- Same mechanism for test discovery is used, but then the suite is run as sub tests via suite.Run
    assert(suite.Run(t, MySuite), "No tests were run by this Suite")

    -- Called for every test: two tests so should be 2
    assert(MySuite.setupCount == 2, tostring(MySuite.setupCount))
    assert(MySuite.tearDownCount == 2, tostring(MySuite.tearDownCount))

    -- Called only once for the suite so should be 1
    assert(MySuite.setupSuiteCount == 1, tostring(MySuite.setupSuiteCount))
    assert(MySuite.tearDownSuiteCount == 1, tostring(MySuite.tearDownSuiteCount))
end

Example use of assert and require

Similar to testify assert and require, Lua's assert and require can be enhanced to add structured assertions.

local require = require 'require'
local assert = require 'assert'
local inspect = require 'inspect'

function TestAssertions(t)
    local s1 = "foo"
    local s2 = "foo"
    assert:Equal(t, s1, s2)
    assert:Equalf(t, s1, s2, "I really didn't expect them to be equal %d", 123)
    
    local o1 = {
        foo = "bar",
    }
    local o2 = {
        foo = "bar",
    }
    assert:Equal(t, inspect(o1), inspect(o2))
    assert:NotEqual(t, 123, 456, [[wow - they're equal?]])
    
    local err = nil
    assert:NoError(t, err, "I got an error?!?")

    assert:False(t, false, "expected false")
    assert:Falsef(t, false, "expected false for %s", "foobar")
    
    assert:True(t, true, "I wanted the truth")
    
    err = 'foo bar'
    assert:Error(t, err)
end

Documentation

Index

Constants

View Source
const (
	TType = "testing.T"
)

Variables

This section is empty.

Functions

func LoadAssert

func LoadAssert(L *lua.LState) int

func LoadAssertions

func LoadAssertions(L *lua.LState) int

func LoadRequre

func LoadRequre(L *lua.LState) int

func LoadSuite

func LoadSuite(L *lua.LState) int

func PreloadAssert

func PreloadAssert(L *lua.LState)

func PreloadAssertions

func PreloadAssertions(L *lua.LState)

func PreloadRequire

func PreloadRequire(L *lua.LState)

func PreloadSuite

func PreloadSuite(L *lua.LState)

func RunLuaTestFile

func RunLuaTestFile(t *testing.T, preload PreloadFunc, filename string) (numTests int)

RunLuaTestFile fires up a new state, registers the *testing.T and invokes all methods starting with Test. This allows the lua test files to operate similar to go tests - see shellescape/test/test_api.lua

Types

type PreloadFunc

type PreloadFunc func(L *lua.LState)

func SeveralPreloadFuncs

func SeveralPreloadFuncs(preloadFuncs ...PreloadFunc) PreloadFunc

SeveralPreloadFuncs combines several PreloadFuncs to one such as when tests want to preload theirs + inspect

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL