Balanced XCTest Life Cycle
Test classes must implement balanced setUp and tearDown methods
- Identifier:
balanced_xctest_lifecycle
- Enabled by default: No
- Supports autocorrection: No
- Kind: lint
- Analyzer rule: No
- Minimum Swift compiler version: 5.0.0
- Default configuration:
Key Value severity warning test_parent_classes [“QuickSpec”, “XCTestCase”]
Rationale
The setUp
method of XCTestCase
can be used to set up variables and resources before each test is run (or with the class
variant, before all tests are run).
This rule verifies that every class with an implementation of a setUp
method also has a tearDown
method (and vice versa).
The tearDown
method should be used to cleanup or reset any resources that could otherwise have any effects on subsequent tests, and to free up any instance variables.
Non Triggering Examples
final class FooTests: XCTestCase {
override func setUp() {}
override func tearDown() {}
}
final class FooTests: XCTestCase {
override func setUpWithError() throws {}
override func tearDown() {}
}
final class FooTests: XCTestCase {
override func setUp() {}
override func tearDownWithError() throws {}
}
final class FooTests: XCTestCase {
override func setUpWithError() throws {}
override func tearDownWithError() throws {}
}
final class BarTests: XCTestCase {
override func setUpWithError() throws {}
override func tearDownWithError() throws {}
}
struct FooTests {
override func setUp() {}
}
class BarTests {
override func setUpWithError() throws {}
}
final class FooTests: XCTestCase {
override func setUpAlLExamples() {}
}
final class FooTests: XCTestCase {
class func setUp() {}
class func tearDown() {}
}
Triggering Examples
final class ↓FooTests: XCTestCase {
override func setUp() {}
}
final class ↓FooTests: XCTestCase {
override func setUpWithError() throws {}
}
final class FooTests: XCTestCase {
override func setUp() {}
override func tearDownWithError() throws {}
}
final class ↓BarTests: XCTestCase {
override func setUpWithError() throws {}
}
final class ↓FooTests: XCTestCase {
class func tearDown() {}
}
final class ↓FooTests: XCTestCase {
override func tearDown() {}
}
final class ↓FooTests: XCTestCase {
override func tearDownWithError() throws {}
}
final class FooTests: XCTestCase {
override func setUp() {}
override func tearDownWithError() throws {}
}
final class ↓BarTests: XCTestCase {
override func tearDownWithError() throws {}
}