Addons/general/unittest

From J Wiki
Jump to navigation Jump to search
User Guide | Installation | Development | Categories | Git | Build Log

general/unittest - Unit Test Framework

Typical framework for test-driven development.
Test units are defined in individual script file.
Main verb unittest file is defined in the z locale.

See also: examples in demo folder in SVN; change history.


Installation

Use JAL/Package Manager.

Usage

Naming conventions

  • script_test.ijs for a test script
  • test_verb for a test verb
  • verb_attribute for attribute related to a test verb

Procedure

  • Create test scripts as shown in demo examples
  • Run the unittest verb against test scripts
  • Observe the output

Test execution can be controlled with attributes:

  • ignore_testVerb=. 1
    will ignore the testVerb
  • expect_testVerb=. 'error'
    will expect 'error' in the testVerb

Setup and tear down code can be executed before and after all and each test verb, when the following verbs are defined in the test script: before_all, after_all, before_each, after_each.

Examples

[{{#file: ""}} Download script: ]

before_all=: 3 : 0
  require'files'
)

test_add=: 3 : 0
  assert 4 = 2+2
)

test_fexist=: 3 : 0
  assert fexist jpath'~help/index1.htm'
)

ex1_expect=: 'domain error'
test_ex1=: 3 : 0
  2+'2'
)

ig2_ignore=: 1
test_ig2=: 3 : 0
  2+2
)

with the test run:

   unittest jpath '~addons/general/unittest/demo/one_test.ijs'
Test: d:\math\j602\addons\general\unittest\demo\one_test.ijs
add ..................................... OK
ex1 ..................................... OK
fexist .................................. Fail
|assertion failure: assert
|       assert fexist jpath'~help/index1.htm'
ig2 ..................................... Ignored

[{{#file: ""}} Download script: ]

before_each=: 3 : 0
  smoutput 'before ',y
)

after_each=: 3 : 0
  smoutput 'after ',y
)

test_rplc=: 3 : 0
  require 'strings'
  assert'zz123' -: 'qq123' rplc 'qq';'zz'
)

test_bad=: 3 : 0
  require 'strings'
  assert'qq123' -: 'qq123' rplc 'qq';'zz'
)

ex2_expect=: 'value error'
test_ex2=: 3 : 0
  dummy 123
)

with the test run:

   unittest jpath '~addons/general/unittest/demo/two_test.ijs'
before bad
after bad
before ex2
after ex2
before rplc
after rplc
Test: d:\math\j602\addons\general\unittest\demo\two_test.ijs
bad ..................................... Fail
|assertion failure: assert
|       assert'qq123'-:'qq123'rplc'qq';'zz'
ex2 ..................................... OK
rplc .................................... OK

See Also


Contributed by Oleg Kobchenko