ZIO Test TestAspect: Avoid @@ to all test

50 views Asked by At

In this sample code, I have 2 Specs, and I want to do some before action to both of them, then I have to call @@ TestAspect.before to both test. If the number of tests increase, add @@ TestAspect to all test/suite sounds not a good idea, how to avoid this?

import zio._
import zio.test._
import zio.test.Assertion._


object WorldSpec extends ZIOSpecDefault {   
  def spec = suite("HelloWorldSpec")(
    test("simple test") {
      assertTrue(1 + 1 == 2)
    }   
  ) @@ TestAspect.before(
      doBeforeFunctions   
  ) 
}


object UniverseSpec extends ZIOSpecDefault {   
  def spec = suite("HelloUniverseSpec")(
    test("sayHello correctly displays output") {
     assertTrue(2 + 2 == 4)
    }
  ) @@ TestAspect.before(
      doBeforeFunction   
  ) 
}
1

There are 1 answers

2
Dima On

Well, you could always do old-good-subclassing if you wanted to ...

abstract class MySpec { 

   def actualSpec:  Spec[TestEnvironment with Scope, Any]
   override def spec = actualSpec @@ before(doBeforeFunction)
}

But ... why? At this point, it seems like writing @@ before in every spec is almost less effort, and in no time, you are going to diverge more anyway ...