2012-08-16 39 views
0

我试着用ScalaTest作为我的构建系统。我试图使用example code带有标签和蚂蚁的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. 

任何想法,为什么这是?任何帮助将非常感激。

回答

1

问题是标记的名称是传递给标记构造函数的字符串。在你的例子中,名字是“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