Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Testing with SBT</strong></p> <p>No matter which version of SBT you want to use, basically you have to do the following steps:</p> <ol> <li>Include your desired testing framework as test-dependency in your project configuration.</li> <li>Create a dedicated testing folder within your source tree, usually <em>src/test/scala</em>, if it isn't present already.</li> <li>As always: Write your tests, specs ...</li> </ol> <p>Those basic steps are identical for the sbt 0.7 branch (that's the one from google-code) and the current sbt 0.10 branch (now developed and documented on github). However, there are minor differences how to define the testing dependencies since 0.10 provides a new quick configuration method not present in 0.7.</p> <p><strong>Defining the dependency for SBT 0.7</strong></p> <p>Here is how you create a basic test (based on scalacheck) with sbt 0.7. Create a new sbt 0.7 project by calling sbt in an empty folder. Change into the automatically created <em>project</em> folder and create a new build folder</p> <pre><code># cd [your-project-root]/project # mkdir build </code></pre> <p>change into the newly created build folder and create your first project build file <em>Project.scala</em> with the follwing content:</p> <pre><code>class Project(info: ProjectInfo) extends DefaultProject(info) { val scalacheck = "org.scala-tools.testing" %% "scalacheck" % "1.9" % "test" } </code></pre> <p>Since for 0.7 the testing folder is created automatically you can then start to write your first test right away. Step to the paragraph "Create a simple test".</p> <p><strong>Defining the dependency for SBT 0.10</strong></p> <p>For 0.10 one can use the sbt console to add the dependency. Just start sbt in your project directory and enter the following commands:</p> <blockquote> <p>set libraryDependencies += "org.scala-tools.testing" %% "scalacheck" % "1.9" % "test" session save</p> </blockquote> <p>You can then close the sbt console and have a look at your projects <em>build.sbt</em> file. As you can easily spot the above libraryDependencies line was added to your projects quick configuration.</p> <p>Since 0.10 doesn't create the source folders automatically. You have to create the testing folder on your own:</p> <pre><code># cd [project-root] # mkdir -p src/test/scala </code></pre> <p>That's all it takes to get started with 0.10. Moreover, the documentation about testing with 0.10 is far more detailed then the old one. See the <a href="https://github.com/harrah/xsbt/wiki/Testing" rel="noreferrer">testing wiki</a> page for further details.</p> <p><strong>Create a simple scalacheck test</strong></p> <p>Create the following test file <em>src/test/scala/StringSpecification.scala</em> (taken from the scalacheck homepage):</p> <pre><code> import org.scalacheck._ object StringSpecification extends Properties("String") { property("startsWith") = Prop.forAll((a: String, b: String) => (a+b).startsWith(a)) property("endsWith") = Prop.forAll((a: String, b: String) => (a+b).endsWith(b)) // Is this really always true? property("concat") = Prop.forAll((a: String, b: String) => (a+b).length > a.length && (a+b).length > b.length ) property("substring") = Prop.forAll((a: String, b: String) => (a+b).substring(a.length) == b ) property("substring") = Prop.forAll((a: String, b: String, c: String) => (a+b+c).substring(a.length, a.length+b.length) == b ) } </code></pre> <p>As already indicated this basic check will fail for the "concat" specification, but that are the basic testing steps needed to get started with testing and sbt. Just adapt the included dependency if you want to use another testing framework.</p> <p><strong>Run your tests</strong></p> <p>To run your test, open the sbt console and type</p> <pre><code>&gt; test </code></pre> <p>That will run all tests present in your <em>src/test</em> tree no matter if those are java or scala based tests. So you can easily reuse your existing java unit tests and convert them step by step to scala.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload