Code Monkey home page Code Monkey logo

u-test's Introduction

u-test

License

u-test is a sane and simple unit testing framework for Lua. It has all essential unit test framework features: defining test cases, test suites, set of build-in assertions, configurable tests output, protected calls and etc.

Top features that are not present in other lua test frameworks

  1. Nice command line interface (like gtest).
  2. Backtrace in failed assertions.
  3. Ordered test execution (as written in source file).
  4. Support 5.1/5.2/5.3 and LuaJIT.
  5. Select particular tests with regexp.

How to install

GitHub

Just copy u-test.lua to your projct or add this repo as submodule.

$ git clone git://github.com/iudalov/u-test

Just run:

$ luarocks install u-test

How to use

local test = require 'u-test'

-- You can use 'assert' to check invariants.
test.hello_world = function ()
    test.assert(true)
    test.assert(1 ~= 2)
end

-- This is how you can create your first test case
test.addition = function ()
    test.equal(1 + 1, 2)
    test.not_equal("1 + 1", "2")
    test.almost_equal(1 + 1, 2.1, 0.2)
end

-- You can enable custom start_up and tear_down actions
-- Thse actions will be invoked:
-- start_up - before test case
-- tear_down - after test case
local global_state = 0
test.start_up = function () global_state = 1 end
test.tear_down = function () global_state = 0 end

test.dummy1 = function()
    test.equal(global_state, 1)
    test.is_number(global_state)
end

-- You can separate tests by test suites
test.string.format = function ()
    test.equal(string.format("%d + %d", 1, 1), "1 + 1")
    test.not_equal(string.format("Sparky %s", "bark"), "Fluffy bark")
end

test.string.find = function ()
    test.is_nil(string.find("u-test", "banana"))
    test.is_not_nil(string.find("u-test", "u"))
end

-- You can declare test case with parameters
test.string.starts_with = function (str, prefix)
    test.equal(string.find(str, prefix), 1)
end

-- Then, run it with multiple parameters
test.string.starts_with("Lua rocks", "Lua")
test.string.starts_with("Wow", "Wow")

local global_table = {}

-- Each test suite can be customized by start_up and tear_down
test.table.start_up = function ()
    global_table = { 1, 2, "three", 4, "five" }
end
test.table.tear_down = function ()
    global_table = {}
end

test.table.concat = function ()
    test.equal(table.concat(global_table, ", "), "1, 2, three, 4, five")
end

-- you can disable broken test case like this
test.broken.skip = true
test.broken.bad_case = function ()
    test.equal(1, 2)
    there_is_no_such_function()
end

-- obtain total number of tests and numer of failed tests
local ntests, nfailed = test.result()

-- this code prints tests summary and invokes os.exit with 0 or 1
test.summary()

Output

List of all assertions

test.assert(true)
test.equal(1, 1)
test.not_equal(1, 2)
test.is_false(false)
test.is_true(true)
test.is_not_nil("Something")
test.is_nil(nil)
test.is_boolean(true)
test.is_boolean(false)
test.is_string("I am string! look at me!")
test.is_number(3)
test.is_table({"I am table now"})
test.is_function(function () end)
test.is_userdata(userdata_value)
test.error_raised(function() error("error 10") end, "error 10")

Custom assertions

local function is_elephant(animal)
    if animal ~= "elephant" then
        local failure_msg = "Expected elephant, but got "..tostring(animal)
        return false, msg
    end

    return true
end

test.register_assert("is_elephant", is_elephant)

test.is_elephant("dolphin") -- fails!

u-test's People

Contributors

bownie avatar firas-assaad avatar iudalov avatar mihacooper avatar superzazu avatar svenmarcus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

u-test's Issues

FEATURE: Assert equal for tables

An assertion for tables the does a deep comparison.

Also, if the error messages could serialize tables that would be nice too.
Right now it just shows table: 0x7fac41603780 ~= table: 0x7fac41700130.
The inspect library is good for this.

Error on 'test.assert': Attempt to call a table value (field 'assert')

Steps to reproduce:

require 'luarocks.loader'
local test = require 'u-test'

test.hello_world = function()
  test.assert(true)
end

Output:

[----------]
[ RUN      ] hello_world
test.lua:6: attempt to call a table value (field 'assert')
[      FAIL] hello_world 0 sec

Versions

u-test 1.1.0-0
Lua 5.3.3
luarocks 3.0.4

Additional info

Turns out that

type(test.assert) == "table"
getmetatable(test.assert).__call == nil

Misleading example

The example shows how to run test cases, but it is difficult to spot among the other pieces of the example, and the output screenshot still shows them running.

Instead of showing how to run a limited set of tests around the middle of the example, perhaps all tests demonstrated in the output could be run at the end.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.