Skip to main content

Command Palette

Search for a command to run...

How to run Swift Tests Serially across files

Here's how I am running Swift Tests serially.

Published
2 min read
How to run Swift Tests Serially across files

I'm going to list the guide first and then why I needed to do this.

This is using Swift Testing

Guide

  1. Create a main test Suite and mark it as @Suite(.serialized)
import Testing

@Suite(.serialized)
struct MainTestSuite {
}
  1. Make an extension on the struct and put your other Suite in side it and also mark it as @Suite(.serialized)
extension MainTestSuite {
    @Suite(.serialized)
    struct SubTestSuiteA {
        @Test
        func testA() {
            #expect(true)
        }
    }
}
  1. Run your tests and your tests should ruin serially. In my testing, they are ran in alphabetical order.

Why I needed this

I am working on FetchKit, a user feedback system for my apps (and maybe yours), and I need to write tests for the repositories, which is what acceses the databases. Since I am testing reading and writing from databases, I want to just use one test database. At the end of each test, I clean up the databse and erase everything so if tests are running parallel the data the test expected to be there could be wiped. DEFINITELY DO NOT WANT THAT.

A lot of the examples I saw online showed:

import Testing

@Suite(.serialized)
struct MainTestSuite {
    @Suite(.serialized)
    struct SubTestSuiteA {
        @Test
        func testA() {
            #expect(true)
        }
    }
    @Suite(.serialized)
    struct SubTestSuiteB {
        @Test
        func testB() {
            #expect(true)
        }
    }
}

This is fine and ultimately nothing wrong with it, except for the file gets really massive, and I'm not a fan of that. I tried asking AI and well that was a bust, so I just tried writing the "sub" test suites in extensions and it worked.

← Back