Skip to content

Testing

Tests let you quickly validate the data behind your insights and assert invariants across your project.

Note

Tests are run with the visivo test command (docs).

A test is any boolean Python expression that can read insight data through the ${ref(...)} context. Insight prop arrays come back as numpy arrays, so numpy is available out of the box.

The combination of Python expressions and direct access to every insight's resolved data makes tests fast to write while still allowing complex assertions about your data.

Example

tests:
  - name: revenue-date-grains-match
    assertions:
      - >{ numpy.sum( ${ref(revenue-per-week).props.y} ) == numpy.sum( ${ref(revenue-per-month).props.y} ) }
tests:
  - name: recent-std-less-double-normal
    assertions:
      - >{ numpy.std( ${ref(revenue-per-week).props.y} ) * 2 > numpy.std( ${ref(revenue-per-month).props.y[:-10]} ) }
tests:
  - name: oct-week-revenue
    assertions:
      - >{ round( ${ref(revenue-per-week).props.y[10]} ) == 2901384 }

Conditional execution

Use the optional if: field to skip a test unless a condition holds. Useful for assertions that only apply to certain insight types or shapes:

tests:
  - name: scatter-only-check
    if: ${ ref(my-insight).props.type } == "scatter"
    assertions:
      - >{ len( ${ref(my-insight).props.x} ) == len( ${ref(my-insight).props.y} ) }

Available helpers

The assertpy library and numpy are always available in test expressions. Together they cover most numerical and structural assertions you'll want to write.