---
url: https://wvlet.org/uni/unitest/organizing.md
description: >-
  Structure UniTest suites — nested tests, skip, pending, flaky, and cancelled
  tests, platform detection, and test tags selected from sbt.
---

# Organizing Tests

## Basic Test Structure

```scala
import wvlet.uni.test.UniTest

class CalculatorTest extends UniTest:
  test("add two numbers") {
    val result = 2 + 3
    result shouldBe 5
  }

  test("subtract two numbers") {
    val result = 5 - 3
    result shouldBe 2
  }
```

## Nested Tests

Tests can be nested for better organization:

```scala
class MathTest extends UniTest:
  test("arithmetic operations") {
    val x = 10

    test("addition") {
      (x + 5) shouldBe 15
    }

    test("multiplication") {
      (x * 2) shouldBe 20
    }
  }
```

Nested test names include their parent context: `"arithmetic operations / addition"`.

## Test Control

All of these are in the [Assertion Cheat Sheet](./#assertion-cheat-sheet).

### Skip Tests

```scala test
test("platform-specific test") {
  if isScalaJS then
    skip("Not supported on Scala.js")

  // JVM-only code, e.g., accessing a system property
  System.getProperty("java.version") shouldNotBe null
}
```

### Pending Tests

```scala test
test("future feature") {
  pending("Implementation not complete yet")
}

test("blocked by issue") {
  pendingUntil("Issue #123 is fixed")
}
```

### Flaky Tests

Mark tests as flaky to convert failures to skipped status:

```scala test
test("network test", Flaky) {
  // Failures become skipped instead of failed
  fetchFromNetwork() shouldBe "expected"
}

// Or wrap specific code
test("partially flaky") {
  val stableResult = computeLocally()
  stableResult shouldBe 42

  flaky {
    val unstableResult = fetchFromNetwork()
    unstableResult shouldBe "expected"
  }
}
```

### Cancel and Ignore

```scala test
test("requires setup") {
  if !setupComplete then
    cancel("Setup failed, cannot run test")

  runTest()
}

test("deprecated test") {
  ignore("This test is no longer relevant")
}
```

### Platform Detection

```scala test
test("platform-specific behavior") {
  if isScalaJVM then
    // JVM-specific assertions
    System.getProperty("os.name") shouldBe defined

  if isScalaJS then
    // Scala.js-specific assertions
    skip("JVM-only test")

  if isScalaNative then
    // Scala Native-specific assertions
    skip("JVM-only test")
}
```

## Test Tags

Pass one or more tags after the test name to group tests, then run or skip a
group from sbt. A single suite can then hold unit, integration, and UI tests
and still run one testing layer at a time, as VSCode does with its separate
unit, integration, and UI test commands.

```scala test
test("renders the toolbar", UI) {
  // ...
}

test("hits the real database", Integration, Slow) {
  // ...
}

test("end-to-end happy path", Smoke) {
  // ...
}

test("retried under load", Flaky) {
  // ...
}
```

Built-in tags are in scope in any `UniTest`:

| Tag | Name | Meaning |
|-----|------|---------|
| `Integration` | `integration` | Uses real external systems |
| `Smoke` | `smoke` | Quick sanity check, run as a fast pre-merge subset |
| `Slow` | `slow` | Usually excluded from the inner-loop run |
| `Flaky` | `flaky` | A failure is reported as skipped instead of failing the build |
| `UI` | `ui` | UI-layer test |
| `Electron` | `electron` | Electron-layer test, for desktop apps built on Uni's Electron support |

Define your own tags with `TestTag("name")`, or as an object when you reuse
one across suites:

```scala test
import wvlet.uni.test.TestTag

case object Gpu extends TestTag("gpu")

test("trains on the GPU", Gpu, Slow) {
  // ...
}

test("legacy path", TestTag("legacy")) {
  // ...
}
```

A bare string also works, as in `test("x", "legacy")`, with
`import scala.language.implicitConversions` at the call site.

### Selecting Tests by Tag

Pass `--tags` and `--exclude-tags` after `--`. Both refer to a tag's name:

```bash
sbt "testOnly * -- --tags ui"                  # only tests tagged ui
sbt "testOnly * -- --tags ui --tags slow"      # tests tagged both ui and slow
sbt "testOnly * -- --exclude-tags slow"        # everything except slow tests
sbt "testOnly * -- --exclude-tags flaky"       # skip known-flaky tests
```

* Repeating `--tags` narrows the selection: a test must carry every listed tag
  (AND), like GitHub label filters.
* `--exclude-tags` drops tests carrying any listed tag, and wins over `--tags`.
* With no `--tags`, every test is included.
* Tags apply to top-level tests. Nested tests run with their parent.

## Best Practices

1. **Avoid mocks** - Use real implementations or in-memory versions
2. **Use Design for wiring** - Bind test objects with `addDesign` / `addTestDesign` and take them as test arguments; share setup through fixture traits that extend `UniTest`. See [Reusable Fixtures](./fixtures#reusable-fixtures)
3. **Prefer `shouldMatch`** - Use pattern matching for type checks instead of `asInstanceOf`
4. **One behavior per test** - Keep tests focused on a single behavior
5. **Descriptive names** - Test names should describe the expected behavior
6. **Use boolean expressions** - For comparisons: `(x > 5) shouldBe true`
7. **Capture expected errors** - Wrap code that fails on purpose in `captureLogs` and assert on the warning, instead of lowering log levels. See [Testing Error Paths](./logs)
