繁体   English   中英

带标签和蚂蚁的ScalaTest-不运行测试

[英]ScalaTest with tags and ant - not running tests

我正在尝试使用Ant作为我的构建系统的ScalaTest。 我正在尝试使用示例代码

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

我正在尝试使用以下ant目标运行它:

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

为什么会有这个想法? 任何帮助将非常感激。

问题在于标记的名称是传递给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批注将类中的每个测试标记为已忽略,如下所示:

导入org.scalatest._

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

但是您可以使用任何标签进行操作,而不仅仅是org.scalatest.Ignore。 因此,传递给Tag类的字符串旨在成为该标签的姐妹注释的完全限定名称。 此设计的更多详细信息在这里:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM