How to run Swift Tests Serially across files
I'm going to list the guide first and then why I needed to do this.
NOTE
This is using Swift Testing
Guide
- Create a main test Suite and mark it as
@Suite(.serialized)
import Testing
@Suite(.serialized)
struct MainTestSuite {
}
- 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)
}
}
}
Repeat as you need to for all your suites
- 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.
What is FetchKit?
FetchKit is a feedback management system that helps developers collect, organize, and act on user feedback directly from their applications. Instead of relying on scattered feedback from app stores, support emails, and social media, FetchKit lets you capture feedback where it matters most: inside your app.
If you're interested in FetchKit, then make sure to join the waitlist, and you'll get weekly updates with the chance to sign up for the beta.
If you liked this post, consider buying me a coffee.
Or donate to Lambda Legal to help fight for trans and LGBTQIA+ rights.