// file: // scalatest_funsuite.scala // package: // assertionAndUnitTesting.testing.scalatests package assertionAndUnitTesting.testing.scalatests import org.scalatest.FunSuite // test is passed in as a by-name parameter to the test method class TestSuite extends FunSuite { test("elem result should have passed width") { assert(2 == 2) } }you might first try to build it.
C:\dev\workspace\scala-train\src>scalac -cp "d:\tools\scala\scalatests\scala2.9.0\scalatest_2.9.0-1.9.1.jar;." -sourcepath "assertionAndUnitTesting\assertions;assertionAndUnitTesting\testing\scalatests" -d ..\bin assertionAndUnitTesting\testing\scalatests\TestSuite.scalayou have to include the scalatests.jar in the class path, and -sourcepath is not necessary, but it does not harm if we include them, we have -d to redirect the output path, and we have to pass the source file full path .
to run the test suite, do this:
C:\dev\workspace\scala-train>scala -cp "d:\tools\scala\scalatests\scala2.9.0\scalatest_2.9.0-1.9.1.jar;.;bin" org.scalatest.tools.Runner -R bin\assertionAndUnitTesting\testing\scalatests -s assertionAndUnitTesting.testing.scalatests.TestSuite-R will include the path used for run path, and you have to qualify the full name of the test suite. such as "assertionAndUitTesting.testing.scalatests.TestSuite".
and if you have source files with import relationship, you may change the command line a little bit, such as
// file: // scalatest_testsuite.scala // package: // assertionAndUnitTesting.testing.scalatests package assertionAndUnitTesting.testing.scalatests import org.scalatest.Suite // import assertionAndUnitTesting.testing.scalatests import assertionAndUnitTesting.assertions._ import assertionAndUnitTesting.assertions.Element._ // extends the org.scalatest.Suite and define test method with testXXX names class ElementSuite extends Suite { def testUniformElement() { val ele = elem('x', 2, 3 ) assert(ele.width == 2) } }imports from
package assertionAndUnitTesting.assertions; // file: // elements.scala // package: // assertionAndUnitTestting.elements // description: // in this post, we will examine the scala assertions, the very basic construct which allows us to do pre/post condition check object Element { //... } class Element { // ... }then you will need to build like this:
C:\dev\workspace\scala-train\src>scalac -cp "d:\tools\scala\scalatests\scala2.9.0\scalatest_2.9.0-1.9.1.jar;." -sourcepath "assertionAndUnitTesting\assertions;assertionAndUnitTesting\testing\scalatests" -d ..\bin assertionAndUnitTesting\testing\scalatests\scalatest_testsuite.scala assertionAndUnitTesting\assertions\elements.scalawhich pass two source files.
or you can first build elements.scala
C:\dev\workspace\scala-train\src>scalac -cp "d:\tools\scala\scalatests\scala2.9.0\scalatest_2.9.0-1.9.1.jar;." -d ..\bin assertionAndUnitTesting\assertions\elements.scalaand then you build again but add the build output from elements.scala (which is directed to ..\bin) to the classpath.
C:\dev\workspace\scala-train\src>scalac -cp "d:\tools\scala\scalatests\scala2.9.0\scalatest_2.9.0-1.9.1.jar;.;..\bin" -d ..\bin assertionAndUnitTesting\testing\scalatests\scalatest_testsuite.scala
There are other ways to run the test, you can actually run the scalatest from within the Scala interpreter.
using the ScalaTest shell.
the trait org.scalatest.Shell provides a run method and configuration fields that implements scala's DSL for the Scalaa interpreter.
we wil use the following FunSuite.
// file: // scalatest_funsuite.scala // package: // assertionAndUnitTesting.testing.scalatests package assertionAndUnitTesting.testing.scalatests import org.scalatest.FunSuite import assertionAndUnitTesting.assertions._ import assertionAndUnitTesting.assertions.Element._ // test is passed in as a by-name parameter to the test method class ElementSuite extends FunSuite { test("elem result should have passed width") { val ele = elem('x', 2, 3) assert(ele.width == 2) } }
C:\dev\workspace\scala-train\src>scalac -classpath d:\tools\scala\scalatests\scala2.9.0\scalatest_2.9.0-1.9.1.jar;.;..\bin assertionAndUnitTesting\testing\scalatests\scalatest_funsuite.scala -d ..\binand then we shall start the Scala interpter with the correct classpath configured.
C:\dev\workspace\scala-train\src>scala -classpath "d:\tools\scala\scalatests\scala2.9.0\scalatest_2.9.0-1.9.1.jar;.;..\bin"
first thing that you need to do is to import the ScalaTest shel.
scala> import org.scalatest._ import org.scalatest._the simplest form is to run all test from one test suite.
scala> run(new assertionAndUnitTesting.testing.scalatests.ElementSuite) [32mElementSuite:[0m [32m- elem result should have passed width[0mand you can pass/cascading configuration command to control how it should run the test suite. e.g.
scala> color.durations.stats.run(new assertionAndUnitTesting.testing.scalatests. ElementSuite) [36mRun starting. Expected test count is: 1[0m [32mElementSuite:[0m [32m- elem result should have passed width (0 milliseconds)[0m [36mRun completed in 3 milliseconds.[0m [36mTotal number of tests run: 1[0m [36mSuites: completed 1, aborted 0[0m [36mTests: succeeded 1, failed 0, ignored 0, pending 0[0m [32mAll tests passed.[0m
you can ask a Test suite to run itself. and it provides a execute method to make things simpler.
to run all test metod from one testSuite.
scala> (new assertionAndUnitTesting.testing.scalatests.ElementSuite).execute() [32mElementSuite:[0m [32m- elem result should have passed width[0mor you can explicitly run one test (identified by the name)
scala> (new assertionAndUnitTesting.testing.scalatests.ElementSuite).execute("el em result should have passed width") [32mElementSuite:[0m [32m- elem result should have passed width[0m
Using the Runner
Using the ScalaTest shell
Invoking execute