Skip to content

tst2005/cwtest

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cwtest

Build Status

Presentation

This is a small Lua test helper with no hard dependency (but that works better with Penlight installed).

Alternatives

Basic usage

local cwtest = require "cwtest"

local T = cwtest.new() -- instantiate a test

T:start("Multiplication"); do -- start a test
  T:eq( 2*3, 6 ) -- test equality
  T:eq( 3*3*3, 27 )
  T:eq( 3*4, 13 ) -- uh?
  T:eq( 7*7, 49 )
end; T:done() -- end a test

T:start("Squares"); do -- you can re-use T once done
  for i=1,10 do
    local x = 0
    for j=1,i do x = x+i end
    T:eq( i*i, x )
  end
end; T:done()

T:exit()

Output:

Multiplication ...x. FAILED

[FAIL] my.test.lua line 8
  expected: 13
       got: 12

Squares .......... OK

Details

do/end block

Wrapping tests in a do/end block is not mandatory. You could simply write this:

T:start("stuff")
T:eq( 6*7, 42 )
T:done()

That being said, the do/end blocks with indentation help to separate your tests visually and keep your variables local, so this style is a good practice.

Return value of done()

done() returns true if all tests have succeeded, false otherwise. Among other things this allows you to abort after a failed test:

T:start("stuff"); do
  T:eq(continue, true)
end
if not T:done() then T:exit() end

exit()

exit() leaves the interpreter, returning an error code of 1 if any call to done() has failed, 0 otherwise. You do not have to call exit(), but doing so makes cwtest work with CI software.

Other tests

  • eq called on tables uses deep comparison.
  • neq is the opposite of eq.
  • yes and no test boolean propositions.
  • seq can be used to compare two lists without considering order.
  • err tests that an error is raised by a function.

Verbosity

You can pass an optional numeric argument between 0 and 2 to cwtest.new() to set verbosity. The default is 0.

Verbosity level 1 will print errors inline as soon as they happen, which may be useful to debug an error that makes your tests crash later on.

Verbosity level 2 will print successes in full form as well as errors. You probably do not need this.

Custom tests

You can define your own tests by adding methods to T and calling pass_ and fail_ methods. You can find an example of this in fakeredis and another one in cwtest's meta-tests.

Copyright

  • Copyright (c) 2012-2013 Moodstocks SAS
  • Copyright (c) 2014-2015 Pierre Chapuis

About

Tiny Lua test helper.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Lua 76.7%
  • Shell 21.0%
  • Makefile 2.3%