# Manage tests in code

> Keep your Semaloop tests as Markdown files in your app's repository, and sync them from GitHub Actions.

Rendered version: https://docs.semaloop.com/writing-tests/manage-tests-in-code/

You can keep your tests as Markdown files in your app’s repository, next to the code they test. They’re reviewed in pull requests and versioned with your app, and a GitHub Action keeps Semaloop in sync with them. Tests created in the dashboard keep working alongside them.

This uses the [Semaloop CLI](/integrations/cli/), which is in early access, so [get in touch](/troubleshooting/contact-us/) if you’d like to use it.

## Getting started

1. Install the [CLI](/integrations/cli/#install) and [authenticate](/integrations/cli/#authenticate) with an API key for your app.

2. From the root of your repository, write a file for each of your existing regression tests. Feature tests stay in the dashboard.

   ```sh
   semaloop test pull
   ```

   This creates `.semaloop/tests`, with one file per test, in a directory for each folder.

3. Check what the first sync will do. Each pulled test should show as a takeover, and nothing should be created or archived:

   ```sh
   semaloop test diff
   ```

4. Read the pulled files before you commit them. If a test has a password or other sensitive value written into it, move it to a [secret](/users-and-test-data/secrets-and-credentials/) and refer to it by slug instead.

5. Commit `.semaloop/tests`, and [add the GitHub Action](#the-github-action) to your repository.

6. Merge. The action syncs on the push to your main branch, and from then on your tests are managed in code.

To start without existing tests, create `.semaloop/tests` and add your first file by hand.

## Test files

Each test is a Markdown file in `.semaloop/tests`, which can have subdirectories. The frontmatter holds the test’s settings, and the body is the test, written in natural language:

```markdown
---
identifier: sign-in
name: Sign in with email
folder: Smoke
draft: false
tags: [smoke, regression]
secrets: [test-account]
disabled_capabilities: [audio_input]
---


Open the app and sign in with the test account. Check that the home screen
shows the account's first name.
```

| Field                   | Required | What it does                                                                                                                                          |
| ----------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- |
| `identifier`            | Yes      | How Semaloop recognises the test. Unique within the app, up to 128 characters. Starts with a letter or digit, then letters, digits, `.`, `_` and `-`. |
| `name`                  | Yes      | The test’s name in the dashboard.                                                                                                                     |
| `folder`                | No       | The test’s [folder](/writing-tests/test-organisation/). Created if it doesn’t exist.                                                                  |
| `draft`                 | No       | `true` keeps the test out of automatic runs. You can still run it by hand. Defaults to `false`.                                                       |
| `tags`                  | No       | The test’s [tags](/writing-tests/test-organisation/). Leaving it out means the test has none.                                                         |
| `secrets`               | No       | The [secrets](/users-and-test-data/secrets-and-credentials/) the test uses, by slug.                                                                  |
| `disabled_capabilities` | No       | Capabilities to treat as not needed, even if the test asks for them. Leaving it out means none are disabled.                                          |
| `take_over_test_id`     | No       | A dashboard test to take over, instead of creating a new one. See [taking over tests](#taking-over-tests).                                            |

`disabled_capabilities` takes `audio_input`, `dynamic_island` and `phone_number`.

### Identifiers

The identifier is what ties a file to its test, not the file’s name or path. You can rename or move a file freely. Changing a test’s `identifier` creates a new test and archives the old one, so the test’s run history and Skills don’t carry over. `semaloop test diff` warns when it looks like that’s happened. Before changing an identifier, remove any `take_over_test_id` from the file: the test it names is already in code under the old identifier, so the sync is refused.

### Secrets

Files name secrets by slug, never by value. A secret gets its slug when it’s created, and the slug never changes. To find it, open **Secrets**, then choose **Copy slug** from the secret’s actions menu.

When a file leaves out `secrets`, a new test gets the app’s default secrets, and an existing test keeps the ones it has. `secrets: []` means none.

## Taking over tests

`semaloop test pull` sets `take_over_test_id` in each file it writes. On the next sync, Semaloop takes over that dashboard test, instead of creating a new one, so it keeps its run history and Skills.

Once a test has been taken over, `take_over_test_id` isn’t needed any more, and `semaloop test diff` and `semaloop test sync` say which files you can remove it from. Leaving it in does no harm, unless you change the test’s [identifier](#identifiers).

## The commands

All the commands read `.semaloop/tests` unless you pass another directory. Results go to stdout, and logs go to stderr. Pass `--output json` for results a program can read.

| Command               | What it does                                                                                  |
| --------------------- | --------------------------------------------------------------------------------------------- |
| `semaloop test check` | Checks every file offline, without an API key, and reports each error with its file and line. |
| `semaloop test diff`  | Shows what a sync would change, without changing anything.                                    |
| `semaloop test sync`  | Creates, updates and archives tests in Semaloop to match the files.                           |
| `semaloop test pull`  | Writes a file for each dashboard test that isn’t in code yet.                                 |
| `semaloop test list`  | Lists the app’s tests. Filter with `--tag`, `--folder`, `--in-code` or `--in-dashboard`.      |

`diff` and `sync` show a plan that names the app first:

```plaintext
$ semaloop test diff
.semaloop/tests → Acme (com.acme.app)


  + checkout-referral   Apply a referral code at checkout   checkout/referral.md
  ~ sign-in             description, tags                   smoke/sign-in.md
  - old-onboarding      file removed, will be archived


1 to create, 1 to update, 1 to archive, 14 unchanged.
Run `semaloop test sync` to apply.
```

In a terminal, `sync` shows the plan and asks before applying it. If another sync changes the app’s tests in the meantime, it shows the new plan and asks again. An edit to a dashboard test you’re taking over doesn’t count, so avoid editing those while you move them into code. `--yes` skips the question, and in CI it never asks.

`pull` skips tests that already have a file, so it’s safe to run again. Pass `--force` to rewrite those files from the dashboard, which replaces any edits you’ve made to them. `--folder` and `--test` limit which tests it writes.

## The GitHub Action

The `semaloop/cli/actions/test-sync` action checks the files and shows the plan on a pull request, without changing anything, and syncs on a push. Add your API key to your repository’s secrets as `SEMALOOP_API_KEY`, then add a workflow:

```yaml
on:
  pull_request:
    paths: [.semaloop/tests/**]
  push:
    branches: [main]
    paths: [.semaloop/tests/**]


permissions:
  contents: read


concurrency:
  group: semaloop-tests-${{ github.ref }}
  cancel-in-progress: false


jobs:
  semaloop-tests:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: semaloop/cli/actions/test-sync@v1
        with:
          api-key: ${{ secrets.SEMALOOP_API_KEY }}
```

| Input               | Default           | What it does                                                                 |
| ------------------- | ----------------- | ---------------------------------------------------------------------------- |
| `api-key`           |                   | Your Semaloop API key. Without it, the action only checks the files.         |
| `path`              | `.semaloop/tests` | The directory of test files, relative to the repository root.                |
| `version`           | `latest`          | The version of the CLI to install.                                           |
| `allow-archive-all` | `false`           | Allows a sync that archives every test in code. See [archiving](#archiving). |

The plan goes in the job summary, and the job fails if a file has an error or Semaloop rejects the sync. The `concurrency` group stops two syncs running at once.

A sync makes Semaloop match the files at the commit it ran on, so don’t re-run an old sync job after newer commits have synced: it would undo their changes. Push again, or re-run the latest one, instead.

GitHub doesn’t give secrets to pull requests from forks, so for those the action only checks the files, and says in the job summary why the plan was skipped. Use `pull_request`, not `pull_request_target`, which would run with your secrets on a fork’s changes.

## In the dashboard

Tests in code have a **Code** label, and a notice on the test’s page that links to its file at the commit that last synced it. A sync run outside GitHub Actions has no commit to link to, so the notice then shows only when the test last synced.

The file is the source of truth, so these are locked in the dashboard, with a short reason why:

* Editing the name or the test itself
* Moving it to another folder
* Switching draft on or off
* Changing its tags, secrets or capability overrides
* Deleting it

The API and the [MCP server](/integrations/mcp-server/) refuse these changes too. Running a test, and everything about its results, work as they do for any other test.

A sync never starts runs. Changed tests run on the next build, and a changed test cancels its old version’s pending runs, as an edit in the dashboard does. So if the same push uploads a build, make the upload wait for the sync. Otherwise the build can be queued with the old version of a test, whose runs the sync then cancels, leaving the build without them.

## Archiving

Deleting a test’s file archives the test on the next sync. Restoring the file, with the same `identifier`, restores the test, with its history.

A sync that would archive every test in code is refused, because it’s usually a mistake: the wrong directory, or an API key for another app. In a terminal, `sync` asks you to type the app’s name to confirm it. Elsewhere, pass `--allow-archive-all`, or set `allow-archive-all: true` on the action.

## Unlinking a test from code

To edit a test in the dashboard again, unlink it from code. It keeps its run history, and becomes an ordinary dashboard test.

1. Open the test, choose **Unlink from code** on its notice and confirm. You need permission to edit tests. To do it from a script, call `POST /api/v1/tests/unlink` with your API key and the test’s `identifier`.
2. Delete the test’s file in the same change, and merge it.

Syncing will fail until you delete the file, so it can’t create a second test under the same identifier. To link the test again instead, keep the file and set `take_over_test_id` to the test’s ID.

Deleting the file without unlinking the test first archives it, as above.

## Related

- Index of every Semaloop docs page: https://docs.semaloop.com/llms.txt
- All Semaloop docs in one file: https://docs.semaloop.com/llms-full.txt
