Skip to content

Instantly share code, notes, and snippets.

@mmlac
Created January 20, 2016 04:59
Show Gist options
  • Save mmlac/e72903b19b6b99870dec to your computer and use it in GitHub Desktop.
Save mmlac/e72903b19b6b99870dec to your computer and use it in GitHub Desktop.
> testOnly com.badgeville.newton.ApiTest
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
2
2
2
2
2
2
2
2
2
2
[info] Run completed in 394 milliseconds.
[info] Total number of tests run: 10
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 10, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
class ParTest extends FunSpec with Matchers with MockitoSugar with BeforeAndAfterEach with BeforeAndAfterAll with ParallelTestExecution {
var f = 23
override def beforeAll() = {
f = 1
}
override def afterAll() = {
f = 5
}
override def beforeEach() = {
f = 2
}
override def afterEach() = {
f = 9
}
describe("This is the first test") {
println(f)
it("1") {
println(f)
}
it("2") {
println(f)
}
it("3") {
println(f)
}
describe("nested one deeper") {
println(f)
it("1") {
println(f)
}
it("2") {
println(f)
}
}
}
describe("This is the second test") {
println(f)
it("1") {
println(f)
}
it("2") {
println(f)
}
it("3") {
println(f)
}
describe("nested one deeper") {
println(f)
it("1") {
println(f)
}
it("2") {
println(f)
}
}
}
}
@bvenners
Copy link

I looked at your output and it looks correct given your test class. One thing you are observing is that ParallelTestExecution extends OneInstancePerTest. So each test runs in its own instance of the test class. Given your test class has 10 tests, you'll get 10 + 1 instances. The extra instance is used to create the other 10. (Someday I'd like to get rid of the extra instance, but it has been like that since day 1.) That's why you see 44 23's in the output initially. Those are the printfs at the top of the four describes, being printed in each of the 11 instances. Then you get 10 2's printed, one per test, because the beforeEach runs before each test and sets f to 2. (The beforeAll also ran before the beforeEach, and set f to 1, but it got overwritten by the 2.)

Perhaps you meant to say f += ... instead of f = ... in the before methods? Orh perhaps you thought you'd see 1 printed before? The reason 1 is not printed before is beforeAll can't be invoked until the test class has been instantiated, because you don't have an instance to call it on, and the describe clauses execute when the test class is instantiated. Thus the "all" in beforeAll means before all tests and nested suites. It doesn't mean before the constructor. The constructor must always execute first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment