Swift 6.2 lets you write better test names


You’re more of a video kind of person? I’ve got you covered! Here’s a video with the same content than this article 🍿



Sponsors like Superwall really help me grow my content creation, so if you have time please make sure to check out their product: it’s a direct support to my content creation ☺️


If you write tests using Swift Testing, I’m sure that you’ve written this kind of code before:

You use the @Test annotation to write an accurate description of your test:

But you also have to provide a function name, because Swift requires you to do it.

And very often this function name ends up being an abridged version of the description of the test:

That’s not great by any means and it would be really nice if we could avoid having to provide this superfluous function name.

The good news is that since Swift 6.2 this has become possible!

Indeed, Swift 6.2 supports using raw identifiers for function names, which allows us to directly use the description of the test as the function name:

This way, we now have much more freedom on which characters we can use in the function name.

We can of course use white spaces, but we're also free to start the identifier with a digit or to use an operator inside the identifier:

So if you’re using Swift Testing, you might want to consider starting to use this new syntax, because it will make writing your tests a smoother experience.

Here’s the code if you want to experiment with it!

import Testing

@Test
func `Division returns correct result`() throws {
    let calculator = Calculator()
    let result = try calculator.divide(10, 2)
    #expect(result == 5.0)
}
Next
Next

The modifier .task() has a hidden feature