tools - scalatests run from command line and sc...

This is the simplest form of the all.

suppose that we have the following code 

// 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. 

Below is the command line for building . 

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.scala
you 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".
you will see the following. 
tools - scalatests run from command line and sc..._第1张图片


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.scala
which 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.scala
and 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.

Scala Interpreter running ScalaTest

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)
  }
}

and we will do the compiling as such .
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 ..\bin
and 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)
ElementSuite:
- elem result should have passed width
and 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)
Run starting. Expected test count is: 1
ElementSuite:
- elem result should have passed width (0 milliseconds)
Run completed in 3 milliseconds.
Total number of tests run: 1
Suites: completed 1, aborted 0
Tests: succeeded 1, failed 0, ignored 0, pending 0
All tests passed.


Invoking execute 

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()
ElementSuite:
- elem result should have passed width
or you can explicitly run one test (identified by the name)

scala> (new assertionAndUnitTesting.testing.scalatests.ElementSuite).execute("el
em result should have passed width")
ElementSuite:
- elem result should have passed width

References:

Using the Runner
Using the ScalaTest shell
Invoking execute

你可能感兴趣的:(scala,tools)