简体   繁体   中英

ScalaTest with tags and ant - not running tests

I'm trying out ScalaTest with ant as my build system. I'm trying to use the example code :

package se.uu.molmed.SandBoxScalaTest

import org.scalatest.FlatSpec
import org.scalatest.Tag

object SlowTest extends Tag("com.mycompany.tags.SlowTest")
object DbTest extends Tag("com.mycompany.tags.DbTest")

class TestingTags extends FlatSpec {

  "The Scala language" must "add correctly" taggedAs(SlowTest) in {
      val sum = 1 + 1
      assert(sum === 2)
    }

  it must "subtract correctly" taggedAs(SlowTest, DbTest) in {
    val diff = 4 - 1
    assert(diff === 3)
  }
}

And I'm trying to run it using the following ant target:

<!-- Run the integration tests -->
<target name="slow.tests" depends="build">
    <taskdef name="scalatest" classname="org.scalatest.tools.ScalaTestAntTask">
        <classpath refid="build.classpath" />
    </taskdef>

    <scalatest parallel="true">
        <tagstoinclude>
            SlowTests   
        </tagstoinclude>
        <tagstoexclude>
            DbTest
        </tagstoexclude>

        <reporter type="stdout" />
        <reporter type="file" filename="${build.dir}/test.out" />

        <suite classname="se.uu.molmed.SandBoxScalaTest.TestingTags" />
    </scalatest>
</target>

It compiles just fine, and runs the suite, but does not include the tests. I would expect it to run the first of the two tests in the code above. The output looks like this:

slow.tests:
[scalatest] Run starting. Expected test count is: 0
[scalatest] TestingTags:
[scalatest] The Scala language
[scalatest] Run completed in 153 milliseconds.
[scalatest] Total number of tests run: 0
[scalatest] Suites: completed 1, aborted 0
[scalatest] Tests: succeeded 0, failed 0, ignored 0, pending 0, canceled 0
[scalatest] All tests passed.

Any ideas of why this is? Any help would be much appreciated.

The problem is that the name of a tag is the string passed to the Tag constructor. And in your example the names are "com.mycompany.tags.SlowTest" and "com.mycompany.tags.DbTest". The fix is to use these strings in your ant task's tagsToInclude and tagsToExclude elements, like this:

<scalatest parallel="true">
    <tagstoinclude>
        com.mycompany.tags.SlowTest   
    </tagstoinclude>
    <tagstoexclude>
        com.mycompany.tags.DbTest
    </tagstoexclude>

    <reporter type="stdout" />
    <reporter type="file" filename="${build.dir}/test.out" />

    <suite classname="se.uu.molmed.SandBoxScalaTest.TestingTags" />
</scalatest>

This somewhat error prone design is unfortunately forced because we want to allow annotations to be used for tagging in some cases, both when writing tests as methods or when you want to tag all tests in a class at once. You can (in ScalaTest 2.0), for example, mark every test in a class as ignored with an @Ignore annotation on the class, like this:

import org.scalatest._

@Ignore class MySpec extends FlatSpec { // All tests in here will be ignored }

But you can do that with any tag, not just org.scalatest.Ignore. So the string you pass to the Tag class is intended to be the fully qualified name of a sister annotation for that tag. More details on this design is here:

http://www.artima.com/docs-scalatest-2.0.M3/#org.scalatest.Tag

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