Order of mixing in BeforeAndAfterEach and OneInstancePerTest

40 views
Skip to first unread message

Raymond Barlow

unread,
Sep 12, 2014, 6:27:29 AM9/12/14
to scalate...@googlegroups.com
Hi,

If we run this test:
import org.scalatest.{FunSuite, BeforeAndAfterEach, Suite, OneInstancePerTest}

trait MyFixture extends BeforeAndAfterEach { this: Suite =>
  override protected def beforeEach(): Unit = {
    super.beforeEach()
    println("beforeEach")
  }
  override protected def afterEach(): Unit = {
    println("afterEach")
    super.afterEach()
  }
}
class MyTest extends FunSuite with MyFixture with OneInstancePerTest {
  test("1") {
    assert(1 === 1)
  }
  test("2") {
    assert(1 === 1)
  }
}

we get this output:
beforeEach
afterEach
beforeEach
afterEach

But, if we swap OneInstancePerTest and MyFixture around like this:
class MyTest extends FunSuite with OneInstancePerTest with MyFixture

Then we get this:
beforeEach
beforeEach
afterEach
afterEach
beforeEach
beforeEach
afterEach
afterEach

Why?

Regards,
Raymond Barlow

Nakamura

unread,
Sep 12, 2014, 12:28:11 PM9/12/14
to scalate...@googlegroups.com

I found a similar result, I wrote a test for it on a PR on github a few days ago.  I also didn't know why.

--
You received this message because you are subscribed to the Google
Groups "scalatest-users" group.
To post to this group, send email to scalate...@googlegroups.com
To unsubscribe from this group, send email to
scalatest-use...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/scalatest-users?hl=en
ScalaTest itself, and documentation, is available here:
http://www.artima.com/scalatest
---
You received this message because you are subscribed to the Google Groups "scalatest-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalatest-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ferdinand Svehla

unread,
Apr 13, 2015, 12:50:29 PM4/13/15
to scalate...@googlegroups.com
Could you find out why this happens?

Bill Venners

unread,
Apr 13, 2015, 12:56:52 PM4/13/15
to scalate...@googlegroups.com
Hi Ferdinand,

Yes, we investigated and understand why it happens. Our current plan
is to modify BeforeAndAfter and BeforeAndAfterEach so that it checks
to see if the current instance (this reference) is also
OneInstancePerTest, and if so, behave differently such that it doesn't
matter what order you mix the two together. In either order, you'll
get only one execution of the before/after code per test. We'll aim to
put this in 2.3.0, due to RC1 end of April.

Bill
--
Bill Venners
Artima, Inc.
http://www.artima.com
Reply all
Reply to author
Forward
0 new messages