2013-12-17 7 views
27

私はSBTの私の "テスト"と "それ"の設定の間でヘルパーの特性を共有したいと思いますが、私はどのように考え出していません。ここで"it"(統合テスト)設定で "テスト"クラスを使用できるようにするにはどうすればよいですか?

は、最小限の例です。

プロジェクト/ Build.scala

import sbt._ 
import Keys._ 

object MyBuild extends Build { 

    val scalaTest = "org.scalatest" %% "scalatest" % "2.0" % "test,it" 

    lazy val myProject = 
    Project(id = "my-project", base = file(".")) 
     .configs(IntegrationTest) 
     .settings(Defaults.itSettings: _*) 
     .settings(
     scalaVersion := "2.10.3", 
     libraryDependencies ++= Seq(
      scalaTest 
     ) 
    ) 
} 

のsrc /テスト/スカラ座/ Helpers.scala

trait Helper { 
    def help() { println("helping.") } 
} 

のsrc /テスト/scala/TestSuite.scala

import org.scalatest._ 

class TestSuite extends FlatSpec with Matchers with Helper { 
    "My code" should "work" in { 
    help() 
    true should be(true) 
    } 
} 

のsrc /それ/スカラ座/ ItSuite.scala

import org.scalatest._ 

class ItSuite extends FlatSpec with Matchers with Helper { 
    "My code" should "work" in { 
    help() 
    true should be(true) 
    } 
} 

、その後、SBTで、 "テストは" 作品:

sbt> test 
helping. 
[info] TestSuite: 
[info] My code 
[info] - should work 
[info] Run completed in 223 milliseconds. 
[info] Total number of tests run: 1 
[info] Suites: completed 1, aborted 0 
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0 
[info] All tests passed. 
[success] Total time: 0 s, completed Dec 17, 2013 1:54:56 AM 

が、 "それを:テストはありません"コンパイル:

sbt> it:test 
[info] Compiling 1 Scala source to ./target/scala-2.10/it-classes... 
[error] ./src/it/scala/ItSuite.scala:3: not found: type Helper 
[error] class ItSuite extends FlatSpec with Matchers with Helper { 
[error]             ^
[error] ./src/it/scala/ItSuite.scala:5: not found: value help 
[error]  help() 
[error] ^
[error] two errors found 
[error] (it:compile) Compilation failed 
[error] Total time: 1 s, completed Dec 17, 2013 1:55:00 AM 

答えて

14

コードを共有したい場合Test構成の場合は、Testからカスタムテスト構成を作成する方が良いでしょう。 Custom test configurationを参照してください。

あなたproject/Build.scalaは次のようになります。

import sbt._ 
import Keys._ 

object MyBuild extends Build { 
    lazy val FunTest = config("fun") extend(Test) 

    val scalaTest = "org.scalatest" %% "scalatest" % "2.0" % "test" 

    lazy val myProject = 
    Project(id = "my-project", base = file(".")) 
     .configs(FunTest) 
     .settings(inConfig(FunTest)(Defaults.testSettings) : _*) 
     .settings(
     scalaVersion := "2.10.3", 
     libraryDependencies ++= Seq(
      scalaTest 
     ) 
    ) 
} 

はまたsrc/fun/src/it/の名前を変更します。今fun:test作品:

> fun:test 
helping. 
[info] ItSuite: 
[info] My code 
[info] - should work 
[info] Run completed in 245 milliseconds. 
[info] Total number of tests run: 1 
[info] Suites: completed 1, aborted 0 
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0 
[info] All tests passed. 
[success] Total time: 1 s, completed Dec 17, 2013 8:43:17 AM 
+0

と思われます。ありがとう! –

+0

@Eugene Yokotaこのようなカスタム設定を使用する場合、特定のTestSuite(テストクラス)のみを実行することはありますか?試した 'fun:testOnly * MySpecialSuit'で運がない場合 – 3Gee

12

あなたは代わりに実行時設定(デフォルト)のテスト構成を拡張するために、プロジェクトでIntegrationTest設定を再定義することができます。これにより、テスト構成のすべてをIntegrationTest構成で使用できるようになります。

import sbt._ 
import Keys._ 

object MyBuild extends Build { 

    val scalaTest = "org.scalatest" %% "scalatest" % "2.0" % "test,it" 

    lazy val IntegrationTest = config("it") extend(Test) 

    lazy val myProject = 
    Project(id = "my-project", base = file(".")) 
     .configs(IntegrationTest) 
     .settings(Defaults.itSettings: _*) 
     .settings(
     scalaVersion := "2.10.3", 
     libraryDependencies ++= Seq(
      scalaTest 
     ) 
    ) 
} 
+0

ビルドの設定の他の部分が影の「本当の」' IntegrationTest'を見ているなら、私はそれをしていますか? –

+3

もしあなたが他のどこかのスコープに引っ張ってしまうとコンパイルできないかもしれませんが、間違ったものを明示的に選択しても、両方のconfigsが "それ"にバインドするのと同じ設定空間に解決されるはずです。 (必要に応じて、MyIntegrationTestに名前を変更できます)。 – Caoilte

関連する問題