Cucumber.Hooks (Cucumber v0.4.1)

View Source

Provides hooks for setup and teardown in Cucumber tests.

Hooks can be defined globally or filtered by tags. They are executed in the order they are defined, with Before hooks running in definition order and After hooks running in reverse order.

Examples

defmodule DatabaseSupport do
  use Cucumber.Hooks

  # Global before hook - runs for all scenarios
  before_scenario context do
    # Setup code
    {:ok, Map.put(context, :setup, true)}
  end

  # Tagged before hook - only runs for @database scenarios
  before_scenario "@database", context do
    :ok = Ecto.Adapters.SQL.Sandbox.checkout(MyApp.Repo)

    if context.async do
      Ecto.Adapters.SQL.Sandbox.mode(MyApp.Repo, {:shared, self()})
    end

    {:ok, context}
  end

  # After hooks run in reverse order
  after_scenario _context do
    # Cleanup code
    :ok
  end
end

Summary

Functions

Defines an after_scenario hook that runs after each scenario.

Defines a before_scenario hook that runs before each scenario.

Functions

after_scenario(context_var, list)

(macro)

Defines an after_scenario hook that runs after each scenario.

Can optionally be filtered by tag. After hooks run in reverse order of definition. The hook receives the test context.

after_scenario(tag, context_var, list)

(macro)

before_scenario(context_var, list)

(macro)

Defines a before_scenario hook that runs before each scenario.

Can optionally be filtered by tag. The hook receives the test context and must return one of:

  • {:ok, context}
  • :ok (keeps context unchanged)
  • map (merged into context)

before_scenario(tag, context_var, list)

(macro)