Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I have every test in class automatically tagged with a specific tag

Tags:

scalatest

I am using the flatspec trait to create my tests and I would like to create a base class that would automatically tag any tests in that class with a particular tag.

For example, any tests in classes that inherit from the IntegrationTest class would automatically be appropriately tagged. 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 do this and still have the test tagged as an IntegrationTest

  class ExampleSpec extends IntegrationSpec {
      "The Scala language" must "add correctly" in {
      val sum = 1 + 1
      assert(sum === 2)
  }

Thanks!

like image 309
agilefall Avatar asked Jan 08 '15 18:01

agilefall


People also ask

What is @TAG in JUnit?

@Tag is a repeatable annotation that is used to declare a tag for the annotated test class or test method. Tags are used to filter which tests are executed for a given test plan. For example, a development team may tag tests with values such as "fast" , "slow" , "ci-server" , etc.

How do you use tags in JUnit?

JUnit 5 allows various Tag Expressions which can be used to filter the tags. For example, to run everything but the integration tests, we could use ! IntegrationTest as the Tag Expression. Or for executing both UnitTest and IntegrationTest, we can use UnitTest | IntegrationTest.


2 Answers

If you're willing to use a direct annotation on the test class, rather than a parent class, you can use the example at https://github.com/kciesielski/tags-demo. Adapted somewhat 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 annotate the Scala test class:

@tags.MyAnnotation
class ExampleSpec extends FlatSpec {
  "The Scala language" must "add correctly" in {
  val sum = 1 + 1
  assert(sum === 2)
}

You then have to use the actual string tags.MyAnnotation to specify the tag you want run (or ignored).

I tried to annotate a parent class instead, but I can't get it to work. I could imagine it being a significant problem for you or not, depending on what else you're trying to do.

Actually, the online doc for the org.scalatest.Tag class does a fair job of describing all this, although I say it after getting it to work by following the above project on GitHub..

like image 61
Spiro Michaylov Avatar answered Oct 19 '22 13:10

Spiro Michaylov


Since ScalaTest 2.2.0 tags can be inherited (http://www.scalatest.org/release_notes/2.2.0).

  1. Add @Inherited to your annotation definition.

    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 {
    }
    
  2. Annotate your base spec.

    @RequiresIntegrationStuff
    class IntegrationSpec extends FlatSpec {}
    
  3. Just use your base spec as a 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 as tags.RequiresIntegrationStuff.

You will find working project here: https://github.com/wojda/tags-demo (based on https://github.com/kciesielski/tags-demo from Spiro Michaylov's answer)

like image 20
Daniel Avatar answered Oct 19 '22 14:10

Daniel