How can I tag each test in a class automatically tag a specific tag
I am using the flatspec property to create my tests, and I would like to create a base class that automatically puts those tests in that class with a specific tag.
For example, any tests in classes that inherit from the IntegrationTest class will automatically be labeled appropriately. So instead of:
class ExampleSpec extends FlatSpec {
"The Scala language" must "add correctly" taggedAs(IntegrationTest) in {
val sum = 1 + 1
assert(sum === 2)
}
I would like to do this and still have a tag marked as IntegrationTest
class ExampleSpec extends IntegrationSpec {
"The Scala language" must "add correctly" in {
val sum = 1 + 1
assert(sum === 2)
}
Thank!
source to share
If you want to use direct annotation for the test class and not the parent class, you can use the example at https://github.com/kciesielski/tags-demo . Adapted for your example, you need to declare a Java class:
package tags;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@org.scalatest.TagAnnotation
@Retention(RUNTIME)
@Target({METHOD, TYPE})
public @interface MyAnnotation {
}
Then you use it to comment out the Scala test class:
@tags.MyAnnotation
class ExampleSpec extends FlatSpec {
"The Scala language" must "add correctly" in {
val sum = 1 + 1
assert(sum === 2)
}
Then you need to use the actual string tags.MyAnnotation
to indicate the tag you want to run (or ignored).
I tried adding a comment to the parent class instead, but I can't seem to get it to work. I could imagine this is a significant problem for you or not, depending on what else you are trying to do.
Actually, the online doc for the org.scalatest.Tag class does a fair job of describing it all, although I say this after getting it working by following the above project on GitHub ..
source to share
Since ScalaTest 2.2.0 tags can be inherited ( http://www.scalatest.org/release_notes/2.2.0 ).
-
Add @ As per your definition of annotation.
package tags; import java.lang.annotation.Retention; import java.lang.annotation.Target; import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.ElementType.TYPE; import static java.lang.annotation.RetentionPolicy.RUNTIME; **@Inherited** @org.scalatest.TagAnnotation @Retention(RUNTIME) @Target({METHOD, TYPE}) public @interface RequiresIntegrationStuff { }
-
Annotate your base specification.
@RequiresIntegrationStuff class IntegrationSpec extends FlatSpec {}
-
Just use the base spec as your base class.
class ExampleSpec extends IntegrationSpec { "The Scala language" must "add correctly" in { val sum = 1 + 1 assert(sum === 2) }
After that, ExampleSpec will be tagged. RequiresIntegrationStuff.
You will find a working draft here: https://github.com/wojda/tags-demo (based on https://github.com/kciesielski/tags-demo from Spiro Mikhailov 's answer )
source to share