Something consider me a long time. Lets say that we have written test class:
final class BearerTokenManagerTests: XCTestCase {
private var bearerTokenManager: BearerTokenManager!
private var bearerTokenProvider: BearerTokenProvider!
private var stubKeyValueStore: KeyValueStoreDummyStub!
private var scheduler: TestScheduler!
private var disposeBag: DisposeBag!
override func setUp() {
super.setUp()
stubKeyValueStore = KeyValueStoreDummyStub()
bearerTokenProvider = BearerTokenProvider(keyValueStore: stubKeyValueStore)
bearerTokenManager = BearerTokenManager(bearerTokenProvider: bearerTokenProvider)
scheduler = TestScheduler(initialClock: 0)
disposeBag = DisposeBag()
}
override func tearDown() {
stubKeyValueStore = nil
bearerTokenProvider = nil
bearerTokenManager = nil
scheduler = nil
disposeBag = nil
super.tearDown()
}
func test_bearerToken_observeChanges() {
let bearerToken = scheduler.createObserver(BearerTokenManagerType.BearerToken.self)
bearerTokenManager.bearerToken
.bind(to: bearerToken)
.disposed(by: disposeBag)
scheduler.start()
// every update should be saved in key value store
bearerTokenManager.update(bearerToken: "123")
XCTAssertEqual(stubKeyValueStore.string(forKey: "BearerToken"), "123")
bearerTokenManager.update(bearerToken: "456")
XCTAssertEqual(stubKeyValueStore.string(forKey: "BearerToken"), "456")
bearerTokenManager.update(bearerToken: "789")
XCTAssertEqual(stubKeyValueStore.string(forKey: "BearerToken"), "789")
// every udpate should be emited
XCTAssertEqual(bearerToken.events, [
.next(0, nil), // by default (on start) token equal to nil
.next(0, "123"),
.next(0, "456"),
.next(0, "789"),
])
}
}
Is tearDown
calling for cleaning purposes necessary?
Why I thinking it could be not necessary:
- Before every next test case
setUp
resets everything. - When tests in
BearerTokenManagerTests
ends then everything should deallocates
Why I not sure
- Assuming that „When tests in
BearerTokenManagerTests
ends then everything should deallocates” could be wrong - I worried about
RxScheduler
side effects - Something I don't know yet
Could someone share their experience? Do you clean up stuff in tearDown
? Is reseting properties in setUp
enough?
Quick answer
According to this article: https://qualitycoding.org/xctestcase-teardown/
Demo
I have created demo app in Xcode 11.7 and behavior is still the same.
System Under Test
TestCase with
tearDown()
Output:
TestCase without
tearDown()
Output:
Long answer
Like you can see in the example without
tearDown()
initialize insidesetUp()
does not assign new object to the same property. Every tests create individual instance and doesn'tdeinit
it after completion. Only the end of the wholeXCTestCase
instances will be deallocated.In small project it probably doesn't matter that much.
But if you have a lot of tests in one
XCTestCase
and you are creating a lot of data insetUp()
(e.g. stubs that takes a lot of memory) you should consider usingtearDown()
because every test will keep it own copy of data fromsetUp()
until wholeXCTestCase
will be completed and you can end up with memory limits issues.