Версия:

Module tap

Module tap

Общие сведения

The tap module streamlines the testing of other modules. It allows writing of tests in the TAP protocol. The results from the tests can be parsed by standard TAP-analyzers so they can be passed to utilities such as prove. Thus one can run tests and then use the results for statistics, decision-making, and so on.

Индекс

Below is a list of all tap functions.

Имя Использование
tap.test() Инициализация
taptest:test() Create a subtest and print the results
taptest:plan() Indicate how many tests to perform
taptest:check() Check the number of tests performed
taptest:diag() Display a diagnostic message
taptest:ok() Evaluate the condition and display the message
taptest:fail() Evaluate the condition and display the message
taptest:skip() Evaluate the condition and display the message
taptest:is() Check if the two arguments are equal
taptest:isnt() Check if the two arguments are different
taptest:is_deeply() Recursively check if the two arguments are equal
taptest:like() Check if the argument matches a pattern
taptest:unlike() Check if the argument does not match a pattern
taptest:isnil()
taptest:isstring()
taptest:isnumber()
taptest:istable()
taptest:isboolean()
taptest:isudata()
taptest:iscdata()
Check if a value has a particular type
tap.test(test-name)

Initialize.

The result of tap.test is an object, which will be called taptest in the rest of this discussion, which is necessary for taptest:plan() and all the other methods.

Параметры:
  • test-name (string) – an arbitrary name to give for the test outputs.
Возврат:

taptest

Rtype:

userdata

tap = require('tap')
taptest = tap.test('test-name')
object taptest
taptest:test(test-name, func)

Create a subtest (if no func argument specified), or (if all arguments are specified) create a subtest, run the test function and print the result.

See the example.

Параметры:
  • name (string) – an arbitrary name to give for the test outputs.
  • fun (function) – the test logic to run.
Возврат:

taptest

Rtype:

userdata or string

taptest:plan(count)

Indicate how many tests will be performed.

Параметры:
  • count (number) –
Возврат:

nil

taptest:check()

Checks the number of tests performed.

The result will be a display saying # bad plan: ... if the number of completed tests is not equal to the number of tests specified by taptest:plan(...). (This is a purely Tarantool feature: «bad plan» messages are out of the TAP13 standard.)

This check should only be done after all planned tests are complete, so ordinarily taptest:check() will only appear at the end of a script. However, as a Tarantool extension, taptest:check() may appear at the end of any subtest. Therefore there are three ways to cause the check:

  • by calling taptest:check() at the end of a script,
  • by calling a function which ends with a call to taptest:check(),
  • or by calling taptest:test(„…“, subtest-function-name) where subtest-function-name does not need to end with taptest:check() because it can be called after the subtest is complete.
Возврат:true or false.
Rtype:boolean
taptest:diag(message)

Display a diagnostic message.

Параметры:
  • message (string) – the message to be displayed.
Возврат:

nil

taptest:ok(condition, test-name)

This is a basic function which is used by other functions. Depending on the value of condition, print „ok“ or „not ok“ along with debugging information. Displays the message.

Параметры:
  • condition (boolean) – an expression which is true or false
  • test-name (string) – name of the test
Возврат:

true or false.

Rtype:

boolean

Пример:

tarantool> taptest:ok(true, 'x')
ok - x
---
- true
...
tarantool> tap = require('tap')
---
...
tarantool> taptest = tap.test('test-name')
TAP version 13
---
...
tarantool> taptest:ok(1 + 1 == 2, 'X')
ok - X
---
- true
...
taptest:fail(test-name)

taptest:fail('x') is equivalent to taptest:ok(false, 'x'). Displays the message.

Параметры:
  • test-name (string) – name of the test
Возврат:

true or false.

Rtype:

boolean

taptest:skip(message)

taptest:skip('x') is equivalent to taptest:ok(true, 'x' .. '# skip'). Displays the message.

Параметры:
  • test-name (string) – name of the test
Возврат:

nil

Пример:

tarantool> taptest:skip('message')
ok - message # skip
---
- true
...
taptest:is(got, expected, test-name)

Check whether the first argument equals the second argument. Displays extensive message if the result is false.

Параметры:
  • got (number) – actual result
  • expected (number) – expected result
  • test-name (string) – name of the test
Возврат:

true or false.

Rtype:

boolean

taptest:isnt(got, expected, test-name)

This is the negation of taptest:is().

Параметры:
  • got (number) – actual result
  • expected (number) – expected result
  • test-name (string) – name of the test
Возврат:

true or false.

Rtype:

boolean

taptest:is_deeply(got, expected, test-name)

Recursive version of taptest:is(...), which can be be used to compare tables as well as scalar values.

Возврат:

true or false.

Rtype:

boolean

Параметры:
  • got (lua-value) – actual result
  • expected (lua-value) – expected result
  • test-name (string) – name of the test
taptest:like(got, expected, test-name)

Verify a string against a pattern. Ok if match is found.

Возврат:

true or false.

Rtype:

boolean

Параметры:
  • got (lua-value) – actual result
  • expected (lua-value) – pattern
  • test-name (string) – name of the test
test:like(tarantool.version, '^[1-9]', "version")
taptest:unlike(got, expected, test-name)

This is the negation of taptest:like().

Параметры:
  • got (number) – actual result
  • expected (number) – pattern
  • test-name (string) – name of the test
Возврат:

true or false.

Rtype:

boolean

taptest:isnil(value, test-name)
taptest:isstring(value, test-name)
taptest:isnumber(value, test-name)
taptest:istable(value, test-name)
taptest:isboolean(value, test-name)
taptest:isudata(value, test-name)
taptest:iscdata(value, test-name)

Test whether a value has a particular type. Displays a long message if the value is not of the specified type.

Параметры:
  • value (lua-value) –
  • test-name (string) – name of the test
Возврат:

true or false.

Rtype:

boolean

Пример

To run this example: put the script in a file named ./tap.lua, then make tap.lua executable by saying chmod a+x ./tap.lua, then execute using Tarantool as a script processor by saying ./tap.lua.

#!/usr/bin/tarantool
local tap = require('tap')
test = tap.test("my test name")
test:plan(2)
test:ok(2 * 2 == 4, "2 * 2 is 4")
test:test("some subtests for test2", function(test)
    test:plan(2)
    test:is(2 + 2, 4, "2 + 2 is 4")
    test:isnt(2 + 3, 4, "2 + 3 is not 4")
end)
test:check()

The output from the above script will look approximately like this:

TAP version 13
1..2
ok - 2 * 2 is 4
    # Some subtests for test2
    1..2
    ok - 2 + 2 is 4,
    ok - 2 + 3 is not 4
    # Some subtests for test2: end
ok - some subtests for test2