tests

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2022 License: BSD-3-Clause Imports: 7 Imported by: 0

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

Documentation

Index

Constants

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

Variables

This section is empty.

Functions

func LoadSuite added in v0.3.2

func LoadSuite(L *lua.LState) int

func PreloadSuite added in v0.3.2

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