标签和蚂蚁的ScalaTest - 没有运行测试

时间:2012-08-16 09:03:01

标签: scala ant scalatest

我正在尝试使用ant作为我的构建系统的ScalaTest。我正在尝试使用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)
  }
}

我正在尝试使用以下蚂蚁目标来运行它:

<!-- 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>

它编译得很好,并运行套件,但不包括测试。我希望它能在上面的代码中运行两个测试中的第一个。输出如下:

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.

为什么会这样?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

问题是标记的名称是传递给Tag构造函数的字符串。在您的示例中,名称为“com.mycompany.tags.SlowTest”和“com.mycompany.tags.DbTest”。修复是在你的ant任务的tagsToInclude和tagsToExclude元素中使用这些字符串,如下所示:

                  

<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>

遗憾的是,这种有点容易出错的设计是强制性的,因为我们希望在某些情况下允许注释用于标记,无论是在将测试编写为方法还是想要同时标记类中的所有测试时。例如,您可以(在ScalaTest 2.0中)将类中的每个测试标记为忽略,并在类上使用@Ignore注释,如下所示:

import org.scalatest ._

@Ignore class MySpec扩展FlatSpec {   //此处的所有测试都将被忽略 }

但你可以用任何标签来做,而不仅仅是org.scalatest.Ignore。因此,传递给Tag类的字符串应该是该标记的姐妹注释的完全限定名称。有关此设计的更多详细信息,请访问:

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