watermelon v0.1.1 Watermelon.Case

Helpers for generating feature test modules.

This module needs to be used within your ExUnit.Case module to provide functionalities needed for building feature tests within your regular ExUnit tests.

For documentation about defining steps, check out Watermelon.DSL.

Example

defmodule MyApp.FeatureTest do
  use ExUnit.Case, async: true
  use Watermelon.Case

  feature """
  Feature: Example
    Scenario: simple test
      Given empty stack
      And pushed 1
      And pushed 2
      When execute sum function
      Then have 3 on top of stack
  """

  defgiven match when "empty stack", do: {:ok, stack: []}

  defgiven match(val) when "pushed {num}", context: %{stack: stack} do
    {:ok, stack: [val | stack]}
  end

  defwhen match when "execute sum function", context: ctx do
    assert [a, b | rest] = ctx.stack

    {:ok, stack: [a + b | rest]}
  end

  defthen match(result) when "have {num} on top of stack", context: ctx do
    assert [^result | _] = ctx.stack
  end
end

Which is rough equivalent of:

defmodule MyApp.FeatureTest do
  use ExUnit.Case, async: true

  test "simple test" do
    stack = [1, 2]
    assert [a, b | _] = stack
    assert 3 == a + b
  end
end

Importing steps from different modules

In time amount of steps can grow and grow, and a lot of them will repeat between different tests, so for your convenience Watermelon.Case provide a way for importing steps definitions from other modules via setting @step_modules module attribute. For example to split above steps we can use:

defmodule MyApp.FeatureTest do
  use ExUnit.Case, async: true
  use Watermelon.Case

  @step_modules [
    MyApp.StackSteps
  ]

  feature_file "stack.feature"
end

Setup and teardown

Nothing special there, just use old ExUnit.Callbacks.setup/2 or ExUnit.Callbacks.setup_all/2 like in any other of Your test modules.

Link to this section Summary

Functions

Define inline feature description

Load file from features directory

Link to this section Functions

Link to this macro

feature(string) (macro)

Define inline feature description.

It accepts inline feature description declaration.

Example

defmodule ExampleTest do
  use ExUnit.Case
  use Watermelon.Case

  feature """
  Feature: Inline feature
    Scenario: Example
      Given foo
      When bar
      Then baz
  """

  # Steps definitions
end
Link to this macro

feature_file(filename) (macro)

Load file from features directory.

Default features directory is set to test/features, however you can change it by setting config :watermelon, features_path: "my_features_dir/" in your configuration file.

Example

defmodule ExampleTest do
  use ExUnit.Case
  use Watermelon.Case

  feature_file "my_feature.feature"

  # Steps definitions
end