Skip to content

Commit

Permalink
Restore v0.7.x fixture ordering
Browse files Browse the repository at this point in the history
We split up the suite-local fixture implementation into two fixtures,
one of the `before*` methods, and one for the `after*` methods.
Then we explicitly order `munitFixtures` to have the befores up front
and the afters at the end.
  • Loading branch information
valencik committed Dec 13, 2023
1 parent d39266f commit fb8cbd2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
10 changes: 8 additions & 2 deletions munit/shared/src/main/scala/munit/MUnitRunner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,25 @@ class MUnitRunner(val cls: Class[_ <: Suite], newInstance: () => Suite)

private lazy val munitTests: mutable.ArrayBuffer[Test] =
mutable.ArrayBuffer[Test](suite.munitTests(): _*)
private val suiteFixture = new Fixture[Unit](cls.getName()) {

// Suite fixtures are implemented as regular fixtures
// We split up the before*/after* methods so we can order them explicitly
private val suiteFixtureBefore = new Fixture[Unit](cls.getName()) {
def apply(): Unit = ()
override def beforeAll(): Unit =
suite.beforeAll()
override def beforeEach(context: BeforeEach): Unit =
suite.beforeEach(context)
}
private val suiteFixtureAfter = new Fixture[Unit](cls.getName()) {
def apply(): Unit = ()
override def afterEach(context: AfterEach): Unit =
suite.afterEach(context)
override def afterAll(): Unit =
suite.afterAll()
}
private lazy val munitFixtures: List[AnyFixture[_]] =
suiteFixture :: suite.munitFixtures.toList
suiteFixtureBefore :: suite.munitFixtures.appended(suiteFixtureAfter).toList

override def filter(filter: Filter): Unit = {
val newTests = munitTests.filter { t =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,29 @@ object FixtureOrderFrameworkSuite
|beforeEach(a, 1)
|beforeEach(b, 1)
|test(1)
|afterEach(ad-hoc, 1)
|afterEach(a, 1)
|afterEach(b, 1)
|afterEach(ad-hoc, 1)
| + 1 <elapsed time>
|beforeEach(ad-hoc, 2)
|beforeEach(a, 2)
|beforeEach(b, 2)
|test(2)
|afterEach(ad-hoc, 2)
|afterEach(a, 2)
|afterEach(b, 2)
|afterEach(ad-hoc, 2)
| + 2 <elapsed time>
|beforeEach(ad-hoc, 3)
|beforeEach(a, 3)
|beforeEach(b, 3)
|test(3)
|afterEach(ad-hoc, 3)
|afterEach(a, 3)
|afterEach(b, 3)
|afterEach(ad-hoc, 3)
| + 3 <elapsed time>
|afterAll(ad-hoc)
|afterAll(a)
|afterAll(b)
|afterAll(ad-hoc)
|""".stripMargin,
format = StdoutFormat
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SuiteLocalFixtureSuite extends FunSuite {
n = 1
}
override def afterAll(): Unit = {
assertEquals(n, 17)
assertEquals(n, 16)
n = -11
}
}
Expand All @@ -34,7 +34,7 @@ class SuiteLocalFixtureSuite extends FunSuite {
}

override def afterAll(): Unit = {
assertEquals(counter(), 17)
assertEquals(counter(), -10)
}

1.to(5).foreach { i =>
Expand Down

0 comments on commit fb8cbd2

Please sign in to comment.