Skip to content

struct's inits are executed before each test, but class's deinits only after a suite is complete. #605

@pitt500

Description

@pitt500

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)
    }
}
Screenshot 2024-08-07 at 10 58 53 a m

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:

Screenshot 2024-08-07 at 12 34 45 p m Screenshot 2024-08-07 at 10 58 17 a m

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

  1. Run the tests above in a new project as final class (not struct).
  2. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug🪲 Something isn't workingconcurrency🔀 Swift concurrency/sendability issues

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions