annotation Athena::Spec::TestCase::DataProvider

Overview

Tests can be defined with arbitrary arguments. These arguments are provided by one or more DataProvider.

A data provider is a method that returns a Hash(String, Tuple), where the key is the description that will be used for the test, and the value is a tuple of the arguments that will be provided to the test.

One or more DataProvider annotations can be applied to a test with a positional argument of the name of the providing methods. An it block will be defined for each "set" of data.

Data providers can be a very powerful tool when combined with inheritance and abstract defs. A parent test case could define all the testing logic, and child implementations only provide the data.

Example

require "athena-spec"

struct DataProviderTest < ASPEC::TestCase
  @[DataProvider("get_values")]
  def test_squares(value : Int32, expected : Int32) : Nil
    (value ** 2).should eq expected
  end

  def get_values
    {
      "two"   => {2, 4},
      "three" => {3, 9},
    }
  end
end

ASPEC.run_all # =>
# DataProviderTest
#   two
#   three

Defined in:

test_case.cr