简体   繁体   中英

Exclude particular test subclass in ScalaTest (maven)

I have one superclass with WordSpecLike and several subclasses for each backend.

abstract class A  extends TestKit(_system) with WordSpecLike {
    "backend" must {
        "do something useful" in {
            //test useful stuff
         }
        "print something pretty" in {
           //test pretty print
        }
    }    
}

class B extends A {

}

class C extends A {

}

C and B tests different backends but the thing is I need to turn on/off each backend-test separately for integration tests (using exclude groups). Obviously, I can't use taggedAs. Using a separate traits like in a bellow example didn't work :

    trait backendB { this: Tag => 
        override val name = "backendB"
    }

    class B extends A with backendB {
    //...
    }

}

Pure java annotation didn't work ether:

@TagAnnotation 
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface backendB { 
}

So, the question is: Have I any other options to run each backend test under a special group or I have to copy-paste code?

Scala version 2.10

ScalaTest version 2.2.2

Java version 1.8.0_20

Can't use sbt instead of maven, can't bump scala version.

If I understand correctly, you want sometimes to exclude class B and run just class C. Other times exclude class C and run just class B. If that's correct, then normally you would annotate B and C with a respective tag annotation (make using Java). For example, doing this would cause all tests of B to be tagged as backendB:

@backendB
class B extends A {
}

What you didn't show is where the actor system is coming from. One thing about tags is that it only excludes tests, an instance of B will still be created. That means any state it holds onto will be initialized, and that might in your case mean an actor system. This might be what you meant by "didn't work." If so, my usual advice is to make that state lazily initialized. The instance will still be created, but because no tests run, he state will never be accessed, and since it is lazy, will never be initialized.

Since there are a lot of unknowns in this reply, you might do better on scalatest-users, where it is easier to go back and forth.

Ok, my final solution is not so elegant as it could be but it's work at least (still kinda more java-way solution).

Several small changes:

trait A-suite extends WordSpecLike {
   val tagToExclude = Tag("here we can write anything")

   def createTestBackend: Backend

   "backend" must {

     "do something useful" taggedAs(tagToExclude) in {
        val backend = createTestBackend
        //test useful stuff
     }

    "print something pretty" taggedAs(tagToExclude) in {
        val backend = createTestBackend
       //test pretty print
    }

   }    
}

And test classes for different backends:

`

class backendA(override val tagToExclude = "backendA") extends A-suite {
      override def createTestBackend: Backend = new backendA 
   }  

class backendB(override val tagToExclude = "backendB") extends A-suite {
      override def createTestBackend: Backend = new backendB
   }

Now exclude groups work properly with maven.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM