Bylaw.Credo.Check.Testing.NoSetupInTests (bylaw_credo v0.1.0-alpha.1)

Copy Markdown View Source

Basics

This check is disabled by default.

Learn how to enable it via .credo.exs.

This check has a base priority of higher and works with any version of Elixir.

Explanation

Avoid setup and setup_all blocks in test modules.

Examples

Avoid:

  setup do
    {:ok, user: create_user()}
  end

  test "shows user", %{user: user} do
    assert user.active?
  end

Prefer:

  test "shows user" do
    user = create_user()
    assert user.active?
  end

Notes

Shared setup hides the inputs a test needs and encourages unrelated tests to depend on the same fixture shape. setup_all also creates shared data that can make ordering and isolation problems harder to see.

Keep each test's data close to the assertion. setup :verify_on_exit! is allowed because it supports mock verification rather than shared fixture construction.

Path exclusions are matched against the source filename and are intended for generated files or temporary migration areas.

The check uses static AST analysis, so dynamic code generation and macro-expanded code may fall outside its signal.

Options

Configure options in .credo.exs with the check tuple:

%{
  configs: [
    %{
      name: "default",
      checks: [
        {Bylaw.Credo.Check.Testing.NoSetupInTests,
         [
           excluded_paths: ["test/support/"]
         ]}
      ]
    }
  ]
}
  • :excluded_paths - Paths containing any configured string are skipped. Use this for shared test case modules that intentionally define setup callbacks.

Usage

Add this check to Credo's checks: list in .credo.exs:

%{
  configs: [
    %{
      name: "default",
      checks: [
        {Bylaw.Credo.Check.Testing.NoSetupInTests, []}
      ]
    }
  ]
}

Check-Specific Parameters

Use the following parameters to configure this check:

:excluded_paths

Paths containing any configured string are skipped. Use this for shared test case modules that intentionally define setup callbacks.

This parameter defaults to [].

General Parameters

Like with all checks, general params can be applied.

Parameters can be configured via the .credo.exs config file.