Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

class setUp/tearDown equivalents in swift-testing #225

Closed
Kyle-Ye opened this issue Feb 18, 2024 · 2 comments
Closed

class setUp/tearDown equivalents in swift-testing #225

Kyle-Ye opened this issue Feb 18, 2024 · 2 comments
Assignees
Labels
enhancement New feature or request public-api Affects public API

Comments

@Kyle-Ye
Copy link
Contributor

Kyle-Ye commented Feb 18, 2024

Description

For normal func setUp and func tearDown we can convert them to init and deinit call on our test class according to the doc here.

But what are the equivalents for class func setUp and class func tearDown here?

class TestCaseBase: XCTestCase {    
    override class func setUp() {
        // Class level init code
		print("[C] setup")
    }
    
    override class func tearDown() {
		// Class level deinit code
		print("[C] tearDown")
    }

    override func setUp() {
        // instance level init code
		...
		print("[I] setup")
    }
    
    override func tearDown() {
		// instance level deinit code
		...
		print("[I] tearDown")
    }
	
	func testMethod1() {}
	func testMethod2() {}
}

We'll get

[C] setup
[I] setup
[I] tearDown
[I] setup
[I] tearDown
[C] tearDown

After converting to swift-testing

@Suite
class TestCaseBase {
	// 🚧
   	class func setUp() {
        // Class level init code
		print("[C] setup")
    }
    
	// 🚧	
   	class func tearDown() {
		// Class level deinit code
		print("[C] tearDown")
    }

	init() {
        // instance level init code
		...
		print("[I] setup")
	}

    deinit {
		// instance level deinit code
		...
		print("[I] tearDown")
    }
	
	@Test 
	func method1() {}
	@Test
	func method2() {}
}

We'll get

[I] setup
[I] tearDown
[I] setup
[I] tearDown

Expected behavior

Get the equivalents behavior of XCTest

Actual behavior

Missing class level setUp and tearDown code.

Workaround

// Track it manually with `Bool` value to workaround class level setUp
```swift
@Suite
class AttributeTestBase {
    
    static var hasClassSetUp = false
    static var hasClassTearDown = false
    
    init() {
        if !Self.hasClassSetUp { // Ignore thread safety issues for now 🤔
            Self.setUp()
            Self.hasClassSetUp = true
        }
    }
    
    deinit {
        ...
    }
    
    class func setUp() { ... }
    
    class func tearDown() { ... }
}
final class ATest: TestBase {
	init() {
		super.init()
        // instance level init code
		...
		print("[I] setup")
	}

	deinit {
		// instance level deinit code
		...
		print("[I] tearDown")
	}

	@Test
	func method1() { ... }
	@Test
	func method2() { ... }
}

swift-testing version/commit hash

0.3.0

@Kyle-Ye Kyle-Ye added the bug Something isn't working label Feb 18, 2024
@stmontgomery stmontgomery added enhancement New feature or request public-api Affects public API and removed bug Something isn't working labels Feb 18, 2024
@stmontgomery stmontgomery self-assigned this Feb 18, 2024
@stmontgomery
Copy link
Contributor

Thank you, yes this is missing functionality we intend to add. I have explored this some so assigning to myself for now.

@stmontgomery
Copy link
Contributor

Duplicate of #36

@stmontgomery stmontgomery marked this as a duplicate of #36 Feb 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request public-api Affects public API
Projects
None yet
Development

No branches or pull requests

2 participants