Search⌘ K
AI Features

Refactoring Tests for Added Safety

Explore refactoring strategies for Ecto schema tests to improve maintainability and accuracy. Learn to create self-updating tests by leveraging schema reflection functions. Understand how to compare expected field types safely and use test tags for focused testing to avoid common pitfalls in testing evolving schemas.

Most applications that use Ecto have more than one schema, and over time each schema grows to have more fields or more logic in its changeset functions. We’re going to refactor our existing test file to make our code easier to maintain, but that means we’re going to tread into some territory that makes some folk uncomfortable: we’re going to refactor our tests to be self-updating.

Before we do that, we’ll build safety into the way we test to avoid a lot of the typical caveats that leave most people burned by writing self-updating tests.

Before we do that, we’ll build safety into the way we test to avoid a lot of the typical caveats that leave most people burned by writing self-updating tests.

Adding safety into schema tests

We’re going to create a new test file, testing_ecto/test/schemas/user_basic_schema_2_test.exs. We’re going to test the same module as our previous file, ...