KScript docs
EN

Testing

Writing tests

Test files and folders

A test file is a normal script named name.test.kscript or name.test.ks.xml. The examples here use the .kscript format: switch them to XML to see <describe>, <test> and <expect>. Test files may use KScript.Test without an import, and so may the editor.

kscript make test checkout creates tests/checkout.test.kscript in the project (--syntax xml for .test.ks.xml). kscript make project starts new projects with the rules of a small to-do app in lib/, a script, and tests for both.

How a test file runs

Each file runs in its own interpreter, in two steps:

  1. The file's top-level code runs once: its imports, variables, fn functions and describe blocks. Each test it reaches is noted, not run.
  2. Each test then runs on its own, in a new scope inside the one it was written in. It sees the file's variables and functions, and the variables of the describe blocks around it.

A test passes if its body finishes. It fails at the first check that doesn't hold, or at any error, and the remaining tests still run. Variables a test declares belong to that test. Variables declared outside the tests are shared, so reset them in before_each.

What a test prints with echo is kept, and shown under the test only when it fails.

Groups and hooks

describe(name: …) { } groups tests. Its name goes in front of each test's name (a shopping cart › adds items), and its hooks apply to every test inside. Groups may nest.

before_each { } runs before every test in its file or describe, in the test's own scope, and outer hooks run first. after_each { } runs after every test, even one that failed, and inner hooks run first. Use it to delete files a test made:

<describe name="a shopping cart">
  <let name="cart" value="[]" />
  <before_each>
    <set name="cart" value="['apple']" />
  </before_each>

  <test name="starts with one item">
    <expect value="$cart" count="1" />
  </test>

  <test name="adds items">
    <set name="cart" value="$cart->push('pear')" />
    <expect value="$cart" equals="['apple', 'pear']" />
  </test>
</describe>
describe(name: 'a shopping cart') {
  $cart = []
  before_each {
    $cart = ['apple']
  }

  test(name: 'starts with one item') {
    expect($cart, count: 1)
  }

  test(name: 'adds items') {
    $cart = $cart->push('pear')
    expect($cart, equals: ['apple', 'pear'])
  }
}

Tests from data

A test inside a for loop makes one test per item. The loop's variables may be used in the test's name and its body, as in the slugify example above. In XML, write <for each="case" at="i" in="$cases"><test name="…">.

Skipping, focusing and timeouts

skip on a test or describe doesn't run it, and it is reported as skipped, with skip: 'reason' giving the reason. While you work on something, only runs just the tests marked only in that file. timeout: 30s fails a test that runs longer than 30 seconds:

<test name="imports a large file" timeout="30s">
  <expect value="[1, 2, 3]->sum()" equals="6" />
</test>

<test name="sends the weekly email" skip="the mail server is down">
  <expect value="true" equals="true" />
</test>
test(name: 'imports a large file', timeout: 30s) {
  expect([1, 2, 3]->sum(), equals: 6)
}

test(name: 'sends the weekly email', skip: 'the mail server is down') {
  expect(true, equals: true)
}