|
ScalaTest 0.9.2
|
|
org/scalatest/fun/FunSuite.scala]
trait
FunSuite
extends SuiteFun ”in FunSuite stands for functional.
Here's an example FunSuite:
import org.scalatest.fun.FunSuite
class MySuite extends FunSuite {
test("addition") {
val sum = 1 + 1
assert(sum === 2)
assert(sum + 2 === 4)
}
test("subtraction") {
val diff = 4 - 1
assert(diff === 3)
assert(diff - 2 === 1)
}
}
“test” is a method defined in FunSuite, which will be invoked
by the primary constructor of MySuite. You specify the name of the test as
a string between the parentheses, and the test code itself between curly braces.
The test code is a function passed as a by-name parameter to test, which registers
it for later execution. One benefit of FunSuite compared to Suite is you need not name all your
tests starting with “test.” In addition, you can more easily give long names to
your tests, because you need not encode them in camel case, as you must do
with test methods.
Test fixtures
If you want to write tests that need the same mutable fixture objects, you can
extend one of the traits FunSuite1 through FunSuite9. If you need three
fixture objects, for example, you would extend FunSuite3. Here's an example
that extends FunSuite1, to initialize a StringBuilder fixture object for each test:
import org.scalatest.fun.FunSuite1
class EasySuite extends FunSuite1[StringBuilder] {
testWithFixture("easy test") {
sb => {
sb.append("easy!")
assert(sb.toString === "Testing is easy!")
}
}
testWithFixture("fun test") {
sb => {
sb.append("fun!")
assert(sb.toString === "Testing is fun!")
}
}
def withFixture(f: StringBuilder => Unit) {
val sb = new StringBuilder("Testing is ")
f(sb)
}
}
In the class declaration of this example, FunSuite1 is parameterized with the type of the
lone fixture object, StringBuilder. Two tests are defined with
testWithFixture. The function values provided here take the fixture object,
a StringBuilder, as a parameter and use it in the test code. Note that
the fixture object, referenced by sb, is mutated by both tests with the call to append. Lastly, a withFixture
method is provided that takes a test function. This method creates a new StringBuilder,
initializes it to "Testing is ", and passes it to the test function.
When ScalaTest runs this suite, it will pass each test function to withFixture.
The withFixture method will create and initialize a new StringBuilder object and
pass that to the test function. In this way, each test function will get a fresh copy
of the fixture. For more information on using FunSuites with fixtures, see the documentation
for FunSuite1 through FunSuite9.
Test groups
A FunSuite's tests may be classified into named groups.
As with any suite, when executing a FunSuite, groups of tests can
optionally be included and/or excluded. To place FunSuite tests into
groups, you pass objects that extend abstract class org.scalatest.fun.Group to methods
that register tests. Class Group takes one type parameter, a string name. If you have
created Java annotation interfaces for use as group names in direct subclasses of org.scalatest.Suite,
then you will probably want to use group names on your FunSuites that match. To do so, simply
pass the fully qualified names of the Java interfaces to the Group constructor. For example, if you've
defined Java annotation interfaces with fully qualified names, com.mycompany.groups.SlowTest and com.mycompany.groups.DBTest, then you could
create matching groups for FunSuites like this:
object SlowTest extends Group("com.mycompany.groups.SlowTest")
object DBTest extends Group("com.mycompany.groups.DBTest")
Given these definitions, you could place FunSuite tests into groups like this:
import org.scalatest.fun.FunSuite
class MySuite extends FunSuite {
test("addition", SlowTest) {
val sum = 1 + 1
assert(sum === 2)
assert(sum + 2 === 4)
}
test("subtraction", SlowTest, DBTest) {
val diff = 4 - 1
assert(diff === 3)
assert(diff - 2 === 1)
}
}
This code places both tests, addition and subtraction, into the com.mycompany.groups.SlowTest group,
and test subtraction into the com.mycompany.groups.DBTest group.
The primary execute method takes two Set[String]s called includes and
excludes. If includes is empty, all tests will be executed
except those those belonging to groups listed in the
excludes Set. If includes is non-empty, only tests
belonging to groups mentioned in includes, and not mentioned in excludes,
will be executed.
Ignored tests
To support the common use case of “temporarily” disabling tests, with the
good intention of resurrecting the test at a later time, FunSuite provides registration
methods that start with ignore instead of test. For example, to temporarily
disable the test named addition, just change “test” into “ignore,” like this:
import org.scalatest.fun.FunSuite
class MySuite extends FunSuite {
ignore("addition") {
val sum = 1 + 1
assert(sum === 2)
assert(sum + 2 === 4)
}
test("subtraction") {
val diff = 4 - 1
assert(diff === 3)
assert(diff - 2 === 1)
}
}
If you run this version of MySuite with:
scala> (new MySuite).execute()
It will run only subtraction and report that addition was ignored.
As with org.scalatest.Suite, the ignore feature is implemented as a group. The execute method that takes no parameters
adds org.scalatest.Ignore to the excludes Set it passes to
the primary execute method, as does Runner. The only difference between
org.scalatest.Ignore and the groups you may define and exclude is that ScalaTest reports
ignored tests to the Reporter. The reason ScalaTest reports ignored tests is as a feeble
attempt to encourage ignored tests to be eventually fixed and added back into the active suite of tests.
Reporters
One of the parameters to the primary execute method is a Reporter, which
will collect and report information about the running suite of tests.
Information about suites and tests that were run, whether tests succeeded or failed,
and tests that were ignored will be passed to the Reporter as the suite runs.
Most often the reporting done by default by FunSuite's methods will be sufficient, but
occasionally you may wish to provide custom information to the Reporter from a test.
For this purpose, you can optionally register a test with a function value that takes a Reporter parameter via the “...WithReporter
variants of the test registration methods. You can then
pass extra information to the Reporter's infoProvided method in the body of the test functions.
Here's an example:
import org.scalatest.fun.FunSuite
import org.scalatest.Report
class MySuite extends FunSuite {
testWithReporter("addition") {
reporter => {
val sum = 1 + 1
assert(sum === 2)
assert(sum + 2 === 4)
val report = new Report("MySuite.addition", "Addition seems to work.")
reporter.infoProvided(report)
}
}
}
If you run this Suite from the interpreter, you will see the following message
included in the printed report:
Info Provided: MySuite.addition: Addition seems to work.
| Method Summary | |
override def
|
groups
: scala.collection.immutable.Map[java.lang.String, scala.collection.immutable.Set[java.lang.String]]
A
Map whose keys are String group names to which tests in this FunSuite belong, and values
the Set of test names that belong to each group. If this FunSuite contains no groups, this method returns an empty Map. |
protected def
|
ignore
(testName : java.lang.String, testGroups : Group*)(f : => Unit) : Unit
Register a test to ignore, which has the specified name, optional groups, and function value that takes no arguments.
This method will register the test for later ignoring via an invocation of one of the
execute
methods. This method exists to make it easy to ignore an existing test method by changing the call to test
to ignore without deleting or commenting out the actual test code. The test will not be executed, but a
report will be sent that indicates the test was ignored. The passed test name must not have been registered previously on
this FunSuite instance. |
protected def
|
ignoreWithReporter
(testName : java.lang.String, testGroups : Group*)(f : (Reporter) => Unit) : Unit
Register a test to ignore, which has the specified name, optional groups, and function value that takes a
Reporter.
This method will register the test for later ignoring via an invocation of one of the execute
methods. This method exists to make it easy to ignore an existing test method by changing the call to testWithReporter
to ignoreWithReporter without deleting or commenting out the actual test code. The test will not be executed, but a
report will be sent that indicates the test was ignored. The passed test name must not have been registered previously on
this FunSuite instance. |
protected override def
|
runTest
(testName : java.lang.String, reporter : Reporter, stopper : Stopper, properties : scala.collection.immutable.Map[java.lang.String, Any]) : Unit
Run a test. This trait's implementation runs the test registered with the name specified by
testName. |
protected def
|
test
(testName : java.lang.String, testGroups : Group*)(f : => Unit) : Unit
Register a test with the specified name, optional groups, and function value that takes no arguments.
This method will register the test for later execution via an invocation of one of the
execute
methods. The passed test name must not have been registered previously on
this FunSuite instance. |
override def
|
testNames
: scala.collection.immutable.Set[java.lang.String]
An immutable
Set of test names. If this FunSuite contains no tests, this method returns an empty Set. |
protected def
|
testWithReporter
(testName : java.lang.String, testGroups : Group*)(f : (Reporter) => Unit) : Unit
Register a test with the specified name, optional groups, and function value that takes a
Reporter.
This method will register the test for later execution via an invocation of one of the execute
methods. The Reporter passed to execute, or a Reporter that wraps it, will be passed to the function value.
The passed test name must not have been registered previously on
this FunSuite instance. |
| Methods inherited from Suite | |
| nestedSuites, execute, execute, runTests, execute, runNestedSuites, suiteName, expectedTestCount, fail, fail, fail, fail, assert, assert, assert, assert, convertToEqualizer, intercept, intercept, expect, expect |
| Methods inherited from AnyRef | |
| getClass, hashCode, equals, clone, toString, notify, notifyAll, wait, wait, wait, finalize, ==, !=, eq, ne, synchronized |
| Methods inherited from Any | |
| ==, !=, isInstanceOf, asInstanceOf |
| Method Details |
protected
def
test(testName : java.lang.String, testGroups : Group*)(f : => Unit) : Unit
execute
methods. The passed test name must not have been registered previously on
this FunSuite instance.IllegalArgumentException - if testName had been registered previouslyprotected
def
testWithReporter(testName : java.lang.String, testGroups : Group*)(f : (Reporter) => Unit) : Unit
Reporter.
This method will register the test for later execution via an invocation of one of the execute
methods. The Reporter passed to execute, or a Reporter that wraps it, will be passed to the function value.
The passed test name must not have been registered previously on
this FunSuite instance.IllegalArgumentException - if testName had been registered previouslyprotected
def
ignore(testName : java.lang.String, testGroups : Group*)(f : => Unit) : Unit
execute
methods. This method exists to make it easy to ignore an existing test method by changing the call to test
to ignore without deleting or commenting out the actual test code. The test will not be executed, but a
report will be sent that indicates the test was ignored. The passed test name must not have been registered previously on
this FunSuite instance.IllegalArgumentException - if testName had been registered previouslyprotected
def
ignoreWithReporter(testName : java.lang.String, testGroups : Group*)(f : (Reporter) => Unit) : Unit
Reporter.
This method will register the test for later ignoring via an invocation of one of the execute
methods. This method exists to make it easy to ignore an existing test method by changing the call to testWithReporter
to ignoreWithReporter without deleting or commenting out the actual test code. The test will not be executed, but a
report will be sent that indicates the test was ignored. The passed test name must not have been registered previously on
this FunSuite instance.IllegalArgumentException - if testName had been registered previouslyoverride
def
testNames : scala.collection.immutable.Set[java.lang.String]
Set of test names. If this FunSuite contains no tests, this method returns an empty Set.
This trait's implementation of this method will return a set that contains the names of all registered tests. The set's iterator will return those names in the order in which the tests were registered.
protected override
def
runTest(testName : java.lang.String, reporter : Reporter, stopper : Stopper, properties : scala.collection.immutable.Map[java.lang.String, Any]) : Unit
testName.testName - the name of one test to execute.reporter - the Reporter to which results will be reportedstopper - the Stopper that will be consulted to determine whether to stop execution early.properties - a Map of properties that can be used by the executing Suite of tests.NullPointerException - if any of testName, reporter, stopper, or properties is null.override
def
groups : scala.collection.immutable.Map[java.lang.String, scala.collection.immutable.Set[java.lang.String]]
Map whose keys are String group names to which tests in this FunSuite belong, and values
the Set of test names that belong to each group. If this FunSuite contains no groups, this method returns an empty Map.
This trait's implementation returns groups that were passed as strings contained in Group objects passed to
methods test, testWithReporter, ignore, and ignoreWithReporter.
|
ScalaTest 0.9.2
|
|