title: Kotest test factory and beforeSpec
body: <p><strong>Update</strong> While searching for the optimal solution, I switched temporarily to an approach using a dedicated test factory that performs the init in combination with <code>forAll</code>. In this way, I don't have to repeat the init code in every test factory.</p>
  <pre><code>fun somePreStuffFactory() = funSpec {
      beforeTest {
          println(&quot;Do some init stuff here&quot;)
      }
  
      test(&quot;dummy test just to trigger beforeTest&quot;) {}
  }
  
  class MyTest : FunSpec({
      runBlocking {
          forAll(
              row(MyTestFactory::someTest1),
              row(MyTestFactory::someTest2)
          ) { testFactoryFunc -&gt;
              include(somePreStuffFactory())
              include(testFactoryFunc())
          }
      }
  })
  </code></pre>
  <p><em>End of update</em></p>
  <p>I have a lot of tests inside test factories. Before running the tests inside a test factory, I need to do some initial setup in the beginning of each test factory, and the setup is the same for all test factories.</p>
  <p>Problem is that <code>beforeSpec</code> is not invoked inside test factory, hence I'm currently using a dirty workaround by doing init stuff in the first test in each test factory. I would highly appreciate any advice on this.</p>
  <p>The optimal solution would be to have a life-cycle hook inside the the test class that runs before each test factory.</p>
  <p><strong>Code to reproduce</strong></p>
  <pre><code>class MyTest : FunSpec({
      include(someTest1())
      include(someTest2())
  })
  </code></pre>
  <pre><code>import io.kotest.core.spec.style.funSpec
  
  object MyTestFactory {
      fun someTest1() = funSpec {
          beforeSpec {
              /** Not invoked */
              println(&quot;Hello from someTest1#beforeSpec&quot;)
          }
  
          test(&quot;Init stuff is done inside a test&quot;) {
              /** some init here */
          }
  
          test(&quot;first test&quot;) {
              println(&quot;Hello from first test&quot;)
          }
  
          test(&quot;second test&quot;) {
              println(&quot;Hello from second test&quot;)
          }
      }
  
      fun someTest2() = funSpec {
          beforeSpec {
              /** Not invoked */
              println(&quot;Hello from someTest2#beforeSpec&quot;)
          }
  
          test(&quot;Init stuff is done inside a test&quot;) {
              /** some init here */
          }
  
          test(&quot;third test&quot;) {
              println(&quot;Hello from third test&quot;)
          }
      }
  }
  </code></pre>
  <p><strong>What I've tried so far</strong></p>
  <p>After bumping Kotest from 4.6.4 to 5.4.2, I was able to run the code in answer from @ocos. Problem is that BeforeSpecSample#beforeSpec is invoked just once, not for each test factory which is my requirement.</p>
  <pre><code>object BeforeSpecSample : BeforeSpecListener {
      override suspend fun beforeSpec(spec: Spec) {
          println(&quot;Hello from beforeSpec&quot;)
      }
  }
  
  class MyTest : FunSpec({
      extensions(BeforeSpecSample)
  
      include(someTest1())
      include(someTest2())
  })
  </code></pre>
  <p><strong>Update</strong>
  After reading <a href="https://github.com/kotest/kotest/issues/458" rel="nofollow noreferrer">this GitHub issue</a>, I successfully tested the following approach using a boolean var <code>initialized</code> and<code>beforeTest</code>. It would have been nice if this approach could be used inside test class instead of inside each test factory, but without any life-cycle hooks for test factories, I don't see how that can be done.</p>
  <pre><code>fun someTest1() = funSpec {
      var initialized = false
  
      beforeTest {
          if (!initialized) {
              println(&quot;Hello from someTest1#beforeTest&quot;)
              initialized = true
          }
      }
  
      /** tests goes here */
  }
  </code></pre>
  <p><strong>Environment</strong>
  Kotest 4.6.4, Kotlin 1.7.10, Micronaut 3.6.3</p>
  <p><a href="https://kotest.io/docs/framework/test-factories.html" rel="nofollow noreferrer">Kotest test factory doc</a></p>