-
Notifications
You must be signed in to change notification settings - Fork 120
Description
Description
Hi, I'm learning how to use Swift Testing and preparing a series of tutorials for my Youtube Channel.
In my next video I want to present how to migrate from setUp
and tearDown
into init
/deinit
. According to this documentation struct's init
acts as XCTest's setUp
, and I created a small demo to see what is the time when init
is called:
var myValue = 0
struct ProductStoreTest {
init () {
print("init called")
myValue = 0
}
@Test
func test1() {
print("Test1 executed")
myValue += 10
#expect(myValue == 10)
}
@Test
func test2() {
print("Test2 executed")
myValue += 10
#expect(myValue == 10)
}
@Test
func test3() {
print("Test3 executed")
myValue += 10
#expect(myValue == 10)
}
}

As you can see, init is called before each test runs, which is expected. However, since deinit is not available on structs, I have to convert my suite into a final class. I was expecting that deinit will be executed after each test run, but that's not happening:


So, my question is: Is this expected?
From what I'm seeing, deinit is running after all tests in the suite are done. If that's the intention, I feel this is confusing in terms of marking deinit
as equivalent to tearDown
.
I will really appreciate your guidance. Thank you!
Expected behavior
Class's deinit work as teardown running after each test is complete.
Actual behavior
Class's deinit are executed after all tests in suite are done.
Steps to reproduce
- Run the tests above in a new project as final class (not struct).
- Observe
swift-testing version/commit hash
0.11.0
Swift & OS version (output of swift --version ; uname -a
)
- Language version: Swift 5
- Minimum Deployment Target: iOS 17.0
- Xcode 16 Beta 5