Is the ordering of it
examples guarenteed?
#1150
-
And if not, how I would re-write something like this? describe("the cache") {
let cache = Cache()
it("loads the value the first lookup") {
_ = cache.lookup(key)
expect(cache.misses) == 1
expect(cache.hits) == 0
}
it("returns the cache value on the second lookup") {
_ = cache.lookup(key)
expect(cache.misses) == 1
expect(cache.hits) == 1
}
} If the order isn't guaranteed, then in each describe("the cache") {
it("loads the value the first lookup") {
let cache = Cache()
_ = cache.lookup(key)
expect(cache.misses) == 1
expect(cache.hits) == 0
}
it("returns the cache value on the second lookup") {
// setup
let cache = Cache()
_ = cache.lookup(key)
// actual test
_ = cache.lookup(key)
expect(cache.misses) == 1
expect(cache.hits) == 0
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I believe the execution order is fairly fairly consistent by default but it I wouldn't rely on it. It's generally bad practice for a test to depend on the state from another test. Each test should be a single unit that can be run in insolation. It's easy to break if tests are removed or changed and Xcode allows running tests in a random order and running single tests, and Quick allows focusing on a single test. For example if you focused the second test it would fail. fit("returns the cache value on the second lookup") { ... } I'd generally add a describe("the cache") {
var cache: Cache!
beforeEach {
cache = Cache()
}
context("first lookup")
beforeEach {
_ = cache.lookup(key)
}
it("loads the value") {
expect(cache.misses) == 1
expect(cache.hits) == 0
}
context("second lookup") {
beforeEach {
_ = cache.lookup(key)
}
it("returns the cache value") {
expect(cache.misses) == 1
expect(cache.hits) == 0
}
}
}
} |
Beta Was this translation helpful? Give feedback.
I believe the execution order is fairly fairly consistent by default but it I wouldn't rely on it.
It's generally bad practice for a test to depend on the state from another test. Each test should be a single unit that can be run in insolation. It's easy to break if tests are removed or changed and Xcode allows running tests in a random order and running single tests, and Quick allows focusing on a single test.
For example if you focused the second test it would fail.
I'd generally add a
context
for each of the "things" that can happen to the object being tested with abeforeEach
to do it. Then the tests in thecontext
will g…