Skip to content

Writing Tests ​

Preview

Testing and the related agent skills are a preview feature. Details may change as we refine them.

Tests live in "spec" files named <name>.spec.pro. Each one is a main-level program that uses Luna.

Structure ​

idl
compile_opt idl2

l = luna()

  s = l.suite('toBeA')

    it = s.test('matches all of the specified types')
      (it.expects(fltarr(5))).toBeA, ['array', 'float']
      (it.expects(42))._not_.toBeA, 'string'

    it = s.test('can run a routine')
      (it.expects('my_routine')).toRunProcedure, 5

l.generateTestSummary
end
  • l = luna() must be in a main-level program. It finds the closest idltest.json automatically
  • A suite groups related tests, a test (it) is one behavior, and a test has one or more expectations
  • compile_opt idl2 is required. Always end with l.generateTestSummary and end
  • Name tests for what they check, like "throws on bad input". Test names are how the results read
  • Define any helper routines before the main-level code

Expectations ​

Call a matcher on an expectation: (it.expects(value)).matcher, args. Negate any matcher with _not_:

idl
(it.expects(42))._not_.toBeNull
To checkUse
EqualitytoEqual (/fold_case for strings), toStrictEqual
TruthtoBeTrue, toBeFalse, toBeTruthy, toBeFalsy, toBeNull
NumberstoBeGreaterThan, toBeGreaterThanOrEqual, toBeLessThan, toBeLessThanOrEqual, toBeBetween, toBeCloseTo, toBeNaN, toBeFinite, toBeInfinite
TypestoBeA, toBeTypename, toBeInstanceOf, toBeAnArrayOf, toBeScalar, toBeArray, toBeAValidObject, toBeSameObject
StringstoContainString, toMatchRegex, toStartWith, toEndWith
Arrays and liststoContain, toContainEqual, toContainAll, toContainAny, toFindInArray, toBeSorted, toHaveUniqueValues, toBeEmpty
SizetoHaveLength, toHaveDimensions, toHaveSameDimensions
Hashes, structures, objectstoHaveKey, toHaveKeyValue, toHaveTag, toHaveMethod
FilestoBeFile, toBeDirectory, toExist
Running codetoRunProcedure, toRunFunction, toRunProcedureMethod, toRunFunctionMethod
Errors, speed, outputtoThrow, toThrowMessage, toThrowMatching, toRunWithin, toPrint
ENVI and IDL taskstoBeAValidENVITask, toBeAValidIDLTask, toExecuteENVITask, toExecuteIDLTask

Testing routines ​

idl
(it.expects('my_routine')).toThrow, bad_input                  ; procedure by default
(it.expects('my_function')).toThrow, bad_input, /is_function   ; use /IS_FUNCTION for functions
(it.expects('my_routine')).toThrowMessage, 'must be positive', -1
(it.expects('my_routine')).toRunWithin, 2.0, big_input
(it.expects('my_routine')).toPrint, 'Hello', /fold_case
(it.expects(obj)).toThrow, 'method', arg1                      ; objects take the method name first

Helpful hints ​

  • Create objects with obj_new, because the routine is passed by name: (it.expects('obj_new')).toRunFunction, 'my_class', arg
  • If your code uses scope_level(), call it directly in the test. Routines run from inside the expectation code, which has a different scope
  • If your code handles an error and moves on, call message, /reset in its catch block. Otherwise the error bubbles up through the IDL-IDLBridge
  • Only start ENVI (headless) in specs that test ENVI tasks
  • Sol and Luna can't test widgets directly

Pro Tip

Ask IDL Agent to write tests for you. The idl-write-tests skill knows every matcher and how to read the results.

Reading the output ​

Test file: src/example.spec.pro

toBeA
  can be negated
    expected not to be types byte, and string (0.00 s, line 16)

    %FAILED% expected to be type string (0.01 s)

      Expected: to be type string
      Received: long

        3 |  it = s.test('variable is a string')
      > 4 |    (it.expects(42)).toBeA, 'string'
        5 |    (it.expects(myVar)).toEqual, 'I love tests!'

      at C:\repo\src\example.spec.pro:5

FAIL
Tests:   1 failed, 1 passed, 2 total
Time:    0.2 s
  • A failure shows %FAILED%, the expected and received values, the code around the failure (> marks the line), and the location
  • Tests: counts one per it block. A test fails if any of its expectations fail
  • Each spec also writes <name>.spec.pro.log next to it with the same output plus a code coverage analysis

Licensed under MIT.