2012-08-16 40 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