Class/Object

org.scalatest

Tag

Related Docs: object Tag | package scalatest

Permalink

class Tag extends AnyRef

Class whose subclasses can be used to tag tests in style traits in which tests are defined as functions.

ScalaTest has two ways to tag tests: annotations and instances of this Tag class. To tag a test method or an entire test class, you use a tag annotation, whereas to tag a test function, you use a Tag object. Though not required, it is usually a good idea to define both an annotation and a corresponding Tag object for each conceptual tag you want, so you can tag anything: test functions, test classes, and test methods. The name of the conceptual tag is the fully qualified name of the annotation interface, so you must pass this name to the Tag constructor.

For example, imagine you want to tag integration tests that use the actual database, and are, therefore, generally slower. You could create a tag annotation and object called DbTest. To give them both the same simple name, you can declare them in different packages. The tag annotation must be written in Java, not Scala, because annotations written in Scala are not accessible at runtime. Here's an example:

package com.mycompany.myproject.testing.tags;

import java.lang.annotation.*;
import org.scalatest.TagAnnotation

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

Given this annotation's fully qualified name is com.mycompany.myproject.testing.tags.DbTest the corresponding Tag object decaration must have that name passed to its constructor, like this:

package com.mycompany.myproject.testing.tagobjects

object DbTest extends Tag("com.mycompany.myproject.testing.tags.DbTest")

Given these definitions, you could tag a test function as a DbTest in, for example, a FlatSpec like this:

import org.scalatest.FlatSpec
import com.mycompany.myproject.testing.tagobjects.DbTest

class ExampleSpec extends FlatSpec {

  "Integration tests" can "sometimes be slow" taggedAs(DbTest) in {
    Thread.sleep(1000)
  }
}

You could tag a test method as a DbTest in, for example, a Suite like this:

import org.scalatest.Suite
import com.mycompany.myproject.testing.tags.DbTest

class ExampleSuite extends Suite {

  @DbTest
  def `integration tests can sometimes be slow` {
    Thread.sleep(1000)
  }
}

And you could tag all the tests in an entire test class by annotating the class, like this:

import org.scalatest.FlatSpec
import com.mycompany.myproject.testing.tags.DbTest

@DBTest
class ExampleSpec extends FlatSpec {

  "Integration tests" can "sometimes be slow" in {
    Thread.sleep(1000)
  }

  they should "likely sometimes be excluded " in {
    Thread.sleep(1000)
  }
}

In the previous example, both tests will be tagged as DBTests even though the tests are not tagged as such individually.

When you run ScalaTest and want to either include or exclude DbTests, you'd give the fully qualified name of the tag annotation (which is also the name passed to the corresponding Tag constructor) to Runner. For example, here's how you'd exclude DbTests on the Runner command line:

-l com.mycompany.myproject.testing.tags.DbTest

For examples of tagging in other style traits, see the "Tagging tests" section in the documentation for the trait:

Source
Tag.scala
Linear Supertypes
AnyRef, Any
Known Subclasses
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Tag
  2. AnyRef
  3. Any
  1. Hide All
  2. Show all
Visibility
  1. Public
  2. All

Instance Constructors

  1. new Tag(name: String)

    Permalink

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  5. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  6. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  7. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  8. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  9. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  10. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  11. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  12. val name: String

    Permalink
  13. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  14. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  15. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  16. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  17. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  18. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  19. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  20. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped