Organizing Tests
Basic Test Structure
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:
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.
Skip Tests
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
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:
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
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
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.
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:
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:
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
--tagsnarrows the selection: a test must carry every listed tag (AND), like GitHub label filters. --exclude-tagsdrops 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
- Avoid mocks - Use real implementations or in-memory versions
- Use Design for wiring - Bind test objects with
addDesign/addTestDesignand take them as test arguments; share setup through fixture traits that extendUniTest. See Reusable Fixtures - Prefer
shouldMatch- Use pattern matching for type checks instead ofasInstanceOf - One behavior per test - Keep tests focused on a single behavior
- Descriptive names - Test names should describe the expected behavior
- Use boolean expressions - For comparisons:
(x > 5) shouldBe true - Capture expected errors - Wrap code that fails on purpose in
captureLogsand assert on the warning, instead of lowering log levels. See Testing Error Paths