ExUnitFixtures v0.3.1 ExUnitFixtures.AutoImport

A mechanism for automatically importing fixtures into the current test module.

In a relatively large test suite you’ll most likely need to declare some fixtures that are to be shared between all tests in a project or module. ExUnitFixtures.AutoImport provides a method for automatically importing fixtures into a test module based on the module’s path.

When a module uses ExUnitFixtures.AutoImport it will automatically lookup fixtures.exs files in the current and parent directories and import the fixtures they contain into the current test file for use.

Note: By default, any call to use ExUnitFixtures or use ExUnitFixtures.FixtureModule will automatically use ExUnitFixtures.AutoImport so this module should not need to be manually used most of the time. This can be controlled by the auto_import setting as described in ExUnitFixtures.start/1

For example, we could use the following directory structure & tests:

tests/
    fixtures.exs
        defmodule GlobalFixtures do
          use ExUnitFixtures.FixtureModule

          deffixture db do
            create_db_conn()
          end
        end

    model_tests/
        fixtures.exs
            defmodule ModelFixtures do
              use ExUnitFixtures.FixtureModule

              deffixture user(db) do
                user = %User{name: "Graeme"}
                insert(db, user)
                user
              end
            end

        user_tests.exs
            defmodule UserTests do
              use ExUnitFixtures

              @tag fixtures: [:user]
              test "user has name", context do
                assert context.user.name == "Graeme"
              end

Here we declare a fixtures.exs file at our top-level that contains a database fixture that any of our tests can access. We then define some fixtures for all of our model tests, and use the user fixture inside one of those tests.

Summary

Functions

Finds any fixture files in the current directory or parent directories

Macros

Imports a the fixture module module into the calling module

Functions

relevant_fixture_files(directory, fixture_regex)

Specs

relevant_fixture_files(String.t, Regex.t) :: [String.t]

Finds any fixture files in the current directory or parent directories.

Uses fixture_regex to match fixture files. Returns the results in descending directory hierarchy order, but files of the same level are not in a guaranteed order.

Macros

require_fixture_module(module)

Imports a the fixture module module into the calling module.