Tia

Test impact analysis for ExUnit: run only the test files a change can affect.

Tia records which modules and source files each test file executes. On the next run it compares content hashes of the sources and the digests of the compiled modules against the disk, and picks the test files whose recorded sources or modules changed. The map validates itself, so it needs no help from git, and it is a plain JSON file you can commit.

Installation

def deps do
  [{:tia, "~> 0.2", only: [:dev, :test], runtime: false}]
end

def cli do
  [preferred_envs: [tia: :test]]
end

Requires Elixir 1.18+ and OTP 27+ running under the JIT. There are no runtime dependencies.

Usage

$ mix tia                   # record the stale test files, update the map
$ mix tia --all             # record every test file
$ mix tia --partitions 4    # record in 4 operating system processes
$ mix tia --partitions auto # one process for every two cores
$ mix tia --check           # CI: run the affected test files, change nothing
$ mix tia --dry-run         # only print the selection

Recording runs each test file on its own, one at a time. A file whose tests fail is not recorded, stays stale, and makes the task exit with an error.

While it records, mix tia prints one line per test file and the failures in full:

tia: [12/749] test/my_app/accounts_test.exs

The formatters you configure in test_helper.exs are replaced by Tia.Formatter for the recording; mix tia --check runs mix test and keeps them.

--all records every test file again. With --only GLOB it replaces only the entries of the matching test files and keeps the rest of the map, unless a global file changed, which empties the map first.

On a large suite, --partitions N (or the :partitions option) records with N mix tia processes that run in parallel and take the test files from one queue. Each one still runs its files one at a time, and the results are merged into the map at the end. Their output is prefixed with [1] to [N] . See Partitions before you use it with a database.

--check runs the stale test files plus those whose recorded sources appear in git diff --name-only BASE. It runs the whole suite when the map is missing or when a global file changed.

Every mode compiles the project first: the selection reads the compiled .beam files.

What to commit

Commit the map, .tia/map.json: CI runs mix tia --check with it, and your colleagues start from it. Everything else in .tia/ belongs to one checkout and stays out of version control:

# .gitignore
/.tia/cache.bin
/.tia/durations.bin
/.tia/partitions/
Path In git What it is
.tia/map.json yes The map: what each test file executed, and its fingerprint
.tia/cache.bin no The hash cache
.tia/durations.bin no How long each test file took to record, for Partitions
.tia/partitions/ no Only exists while --partitions runs

Each line of the map holds one test file: its fingerprint and the modules and sources it executed, in order. Nothing in it depends on the run, so the map changes only when a selection does: recording the same code again, with mix tia --all, writes the same bytes, and mix tia with nothing stale does not write it at all. A diff of the map shows the test files whose dependencies changed, one line each.

A map recorded by Tia 0.2.0 has an ms in each line. It is ignored, and leaves a line when its test file is recorded again; mix tia --all removes all of them at once.

What forces the whole suite

A per-test map cannot see everything, so these files share one fingerprint and any change to them selects every test file:

Files that define macros are not on this list. A change to a macro, to a case template (use MyApp.DataCase) or to a module like MyAppWeb selects only the test files that executed a module compiled with it; see How recording works.

If your config imports other files (for example import_config "test/*.exs"), add them to :global_globs or :extra_global_files.

Configuration

def project do
  [
    # ...
    tia: [
      base: "develop",
      extra_global_files: ["priv/repo/seeds.exs"]
    ]
  ]
end

Tia.Config lists every option. If test_helper.exs loads other files, such as seeds, add them to :extra_global_files: Tia cannot see them otherwise.

:ignore_modules keeps modules out of the map. An atom is one module, a string is a prefix of the module name:

tia: [ignore_modules: [MyApp.PromEx, "MyApp.Telemetry"]]

Use it for the code your background processes run; see Limitations. A change to an ignored module selects no test file through it.

How recording works

Tia switches the VM to native function counters (:code.set_coverage_mode(:function_counters)) and reloads your application's modules. Nothing is recompiled, and reading or zeroing the counters of a thousand modules takes under a millisecond, so recording costs about the same as running the tests. :cover spent over a second per test file on the same project.

A module counts as executed when any of its functions was entered. Its name, its source and its @external_resource files, such as templates, go into the map. Macros count too: a case template whose __using__/1 expands while the test file is compiled is executed by that test file.

The fingerprint of a test file covers the content of those files and a digest of the compiled .beam file of each of those modules (Tia.Beam.digest/1). So when you change a macro, or a function whose result another module embeds at compile time (@limit B.limit()), Mix recompiles the dependent modules and only the test files that executed one of them, with different code, become stale. A recorded module whose .beam file is gone also makes its test files stale.

The digest is not the checksum of the file. Two compilations of the same sources do not always give the same .beam: a macro that builds code by walking a map (the slots of a HEEx component, @derive Jason.Encoder, a Commanded router) emits it in an order that changes from one compilation to the next. Tia hashes the expanded code in the debug_info chunk instead, with line numbers dropped and with the function clauses, map keys and list literals sorted. In a Phoenix application with 1112 modules, 69 had a .beam checksum that changed across four mix compile --force runs of the same sources, and none had a digest that changed; the digest of the 80 modules that expand use MyAppWeb, :live_view changed when that macro got a new function, and no digest changed when it got a new comment. That is what was verified: one project, one machine, one checkout. The rules are a heuristic, so another macro library can still give two digests for the same sources, which only selects more tests. Another Elixir or OTP version can expand the same source differently, so a map recorded with other versions may start with stale test files. A module without Elixir debug information falls back to :beam_lib.md5/1.

The digest does not depend on where the checkout is. Code that embeds __ENV__.file, as the Phoenix macros do, carries the absolute path of the project in its literals; Tia swaps the root of the project for a placeholder in every string and charlist before it hashes, so a map recorded in /home/you/my_app is still current in /builds/my_app on CI. A hash of a path is out of reach of that swap, so the definitions of __mix_recompile__?/0 and __phoenix_recompile__?/0 are ignored: they only tell Mix when to recompile a module, and embed_templates of Phoenix puts there the md5 of the absolute paths of the templates. This is covered by tests that compile one module in two directories; it has not been verified between two real machines yet.

The digest is read from the file on disk (the :ebin option, by default Mix.Project.compile_path/0), never from the loaded module, because the function counters change the loaded code.

The counters are global to the VM, which is why one VM records one test file at a time and why --partitions uses separate operating system processes instead of concurrency inside one.

The hash cache

Every run hashes all the test files, all the recorded sources and all the recorded modules to find what is stale. mix tia keeps those hashes in .tia/cache.bin, each under the absolute path of its file together with the modification time and the size the file had, and reuses a hash while both are the same. On a synthetic project with 749 test files and 1112 modules, on 16 cores, finding the stale test files took 541 ms without the cache and 74 ms with it; compiling and starting Mix are not in those numbers and take the same time either way.

The cache belongs to one checkout and is safe to delete. A cache that is damaged or comes from another version of Tia is ignored. Two things to know:

tia: [cache: false]

:cache also takes another path, relative to the project root or absolute. The durations of the recordings, durations.bin, are kept in the same directory as the cache, and are not kept at all with cache: false.

Partitions

--partitions N starts N processes and one queue. A process asks for the next test file when it finishes the one it has, so a slow file holds back only the process that runs it. The queue starts with the test files that have no known duration, followed by the others from the slowest to the fastest, so the long ones go first and the partitions finish close to each other. The durations are the ones of the last recordings on your machine, kept in .tia/durations.bin, next to the hash cache and out of version control. Without that file, as on a fresh checkout, the queue follows the order of the names. With cache: false no durations are kept. The progress counts over the whole run:

[2] tia: [12/749] test/my_app/accounts_test.exs

If a process dies, the test file it was recording and the ones it recorded since it last saved its results (it saves every 50 files) count as failed and stay stale. The other processes go on with the rest of the queue.

--partitions auto, or partitions: :auto, uses half of System.schedulers_online/0, at least one. The default stays 1: more partitions need the database setup below.

Each process of --partitions N gets MIX_TEST_PARTITION set to its number, from 1 to N, the convention of mix test --partitions. If your tests use a database, give each partition its own, or they will step on each other:

# config/test.exs
config :my_app, MyApp.Repo,
  database: "my_app_test#{System.get_env("MIX_TEST_PARTITION")}"

Each partition starts your application, so each one opens the whole pool of the Repo: N partitions hold N × pool_size connections, which can pass the max_connections of the database. With auto on a machine with 16 cores that is 8 partitions: a pool of 32 connections each would ask for 256, and PostgreSQL allows 100 by default. Recording runs one test at a time (max_cases: 1), so a partition needs few connections:

# config/test.exs
config :my_app, MyApp.Repo,
  pool_size: if(System.get_env("MIX_TEST_PARTITION"), do: 5, else: System.schedulers_online() * 2)

The project is compiled once, before the partitions start; they only load it. Every partition loads test_helper.exs, so anything it sets up, such as the Ecto sandbox or migrations, runs once per partition, and ExUnit.after_suite callbacks run once per partition with the totals of that partition. With one partition, or a single test file to record, everything runs in the calling process and MIX_TEST_PARTITION is not set.

Limitations

Selection is a speedup, not a proof. Keep a full run on your main branch.

Hub modules also limit the gain: a file that most tests execute selects most of the suite when it changes.

Credits

Tia is inspired by the --tia option of Pest 5, created by Nuno Maduro. Tia is an independent project for ExUnit, with no affiliation to Pest.