I want to test such a class:
class Foo {
var number: Int = 0
}
In the iOS unit test, normally test case should be:
class FooTests: XCTestCase {
var foo: Foo!
override func setUp() {
foo = Foo()
}
override func tearDown() {
foo = nil
}
func testAbc() {
print(foo.number)
foo.number = 10
}
func testBCD() {
print(foo.number)
}
}
Then how about
class FooTests: XCTestCase {
let foo = Foo()
func testAbc() {
print(foo.number)
foo.number = 10
}
func testBCD() {
print(foo.number)
}
}
I see the output are all 0 which means when each start test case the foo
seems is initialised again. Just like use setUp
and tearDown
.
Are both way the same?
EDIT:
Thanks to @Anton 's answer, I even tested without setUp
but with tearDown
, then same as use both setUp
and tearDown
.
var foo: Foo! = Foo()
override func tearDown() {
foo = nil
}