Class used via an implicit conversion to enable any two objects to be compared with
===
in assertions in tests.
Assert that an Option[String]
is None
.
Assert that an Option[String]
is None
.
If the condition is None
, this method returns normally.
Else, it throws TestFailedException
with the String
value of the Some
included in the TestFailedException
's
detail message.
This form of assert
is usually called in conjunction with an
implicit conversion to Equalizer
, using a ===
comparison, as in:
assert(a === b)
For more information on how this mechanism works, see the documentation for
Equalizer
.
the Option[String]
to assert
Assert that an Option[String]
is None
.
Assert that an Option[String]
is None
.
If the condition is None
, this method returns normally.
Else, it throws TestFailedException
with the String
value of the Some
, as well as the
String
obtained by invoking toString
on the
specified clue
,
included in the TestFailedException
's detail message.
This form of assert
is usually called in conjunction with an
implicit conversion to Equalizer
, using a ===
comparison, as in:
assert(a === b, "extra info reported if assertion fails")
For more information on how this mechanism works, see the documentation for
Equalizer
.
the Option[String]
to assert
An objects whose toString
method returns a message to include in a failure report.
Assert that a boolean condition, described in String
message
, is true.
Assert that a boolean condition, described in String
message
, is true.
If the condition is true
, this method returns normally.
Else, it throws TestFailedException
with the
String
obtained by invoking toString
on the
specified clue
as the exception's detail message.
the boolean condition to assert
An objects whose toString
method returns a message to include in a failure report.
Assert that a boolean condition is true.
Assert that a boolean condition is true.
If the condition is true
, this method returns normally.
Else, it throws TestFailedException
.
the boolean condition to assert
Assume that an Option[String]
is None
.
Assume that an Option[String]
is None
.
If the condition is None
, this method returns normally.
Else, it throws TestCanceledException
with the String
value of the Some
included in the TestCanceledException
's
detail message.
This form of assume
is usually called in conjunction with an
implicit conversion to Equalizer
, using a ===
comparison, as in:
assert(a === b)
For more information on how this mechanism works, see the documentation for
Equalizer
.
the Option[String]
to assert
Assume that an Option[String]
is None
.
Assume that an Option[String]
is None
.
If the condition is None
, this method returns normally.
Else, it throws TestCanceledException
with the String
value of the Some
, as well as the
String
obtained by invoking toString
on the
specified clue
,
included in the TestCanceledException
's detail message.
This form of assume
is usually called in conjunction with an
implicit conversion to Equalizer
, using a ===
comparison, as in:
assume(a === b, "extra info reported if assertion fails")
For more information on how this mechanism works, see the documentation for
Equalizer
.
the Option[String]
to assert
An objects whose toString
method returns a message to include in a failure report.
Assume that a boolean condition, described in String
message
, is true.
Assume that a boolean condition, described in String
message
, is true.
If the condition is true
, this method returns normally.
Else, it throws TestCanceledException
with the
String
obtained by invoking toString
on the
specified clue
as the exception's detail message.
the boolean condition to assume
An objects whose toString
method returns a message to include in a failure report.
Assume that a boolean condition is true.
Assume that a boolean condition is true.
If the condition is true
, this method returns normally.
Else, it throws TestCanceledException
.
the boolean condition to assert
Throws TestCanceledException
, with the passed
Throwable
cause, to indicate a test failed.
Throws TestCanceledException
, with the passed
Throwable
cause, to indicate a test failed.
The getMessage
method of the thrown TestCanceledException
will return cause.toString
.
a Throwable
that indicates the cause of the cancellation.
Throws TestCanceledException
, with the passed
String
message
as the exception's detail
message and Throwable
cause, to indicate a test failed.
Throws TestCanceledException
, with the passed
String
message
as the exception's detail
message and Throwable
cause, to indicate a test failed.
A message describing the failure.
A Throwable
that indicates the cause of the failure.
Throws TestCanceledException
, with the passed
String
message
as the exception's detail
message, to indicate a test was canceled.
Throws TestCanceledException
, with the passed
String
message
as the exception's detail
message, to indicate a test was canceled.
A message describing the cancellation.
Throws TestCanceledException
to indicate a test was canceled.
Implicit conversion from Any
to Equalizer
, used to enable
assertions with ===
comparisons.
Implicit conversion from Any
to Equalizer
, used to enable
assertions with ===
comparisons.
For more information on this mechanism, see the documentation for Equalizer.
Because trait Suite
mixes in Assertions
, this implicit conversion will always be
available by default in ScalaTest Suite
s. This is the only implicit conversion that is in scope by default in every
ScalaTest Suite
. Other implicit conversions offered by ScalaTest, such as those that support the matchers DSL
or invokePrivate
, must be explicitly invited into your test code, either by mixing in a trait or importing the
members of its companion object. The reason ScalaTest requires you to invite in implicit conversions (with the exception of the
implicit conversion for ===
operator) is because if one of ScalaTest's implicit conversions clashes with an
implicit conversion used in the code you are trying to test, your program won't compile. Thus there is a chance that if you
are ever trying to use a library or test some code that also offers an implicit conversion involving a ===
operator,
you could run into the problem of a compiler error due to an ambiguous implicit conversion. If that happens, you can turn off
the implicit conversion offered by this convertToEqualizer
method simply by overriding the method in your
Suite
subclass, but not marking it as implicit:
// In your Suite subclass override def convertToEqualizer(left: Any) = new Equalizer(left)
the object whose type to convert to Equalizer
.
Expect that the value passed as expected
equals the value passed as actual
.
Expect that the value passed as expected
equals the value passed as actual
.
If the actual
value equals the expected
value
(as determined by ==
), expectResult
returns
normally. Else, expect
throws a
TestFailedException
whose detail message includes the expected and actual values.
the expected value
the actual value, which should equal the passed expected
value
Expect that the value passed as expected
equals the value passed as actual
.
Expect that the value passed as expected
equals the value passed as actual
.
If the actual
equals the expected
(as determined by ==
), expectResult
returns
normally. Else, if actual
is not equal to expected
, expectResult
throws a
TestFailedException
whose detail message includes the expected and actual values, as well as the String
obtained by invoking toString
on the passed clue
.
the expected value
An object whose toString
method returns a message to include in a failure report.
the actual value, which should equal the passed expected
value
Throws TestFailedException
, with the passed
Throwable
cause, to indicate a test failed.
Throws TestFailedException
, with the passed
Throwable
cause, to indicate a test failed.
The getMessage
method of the thrown TestFailedException
will return cause.toString
.
a Throwable
that indicates the cause of the failure.
Throws TestFailedException
, with the passed
String
message
as the exception's detail
message and Throwable
cause, to indicate a test failed.
Throws TestFailedException
, with the passed
String
message
as the exception's detail
message and Throwable
cause, to indicate a test failed.
A message describing the failure.
A Throwable
that indicates the cause of the failure.
Throws TestFailedException
, with the passed
String
message
as the exception's detail
message, to indicate a test failed.
Throws TestFailedException
, with the passed
String
message
as the exception's detail
message, to indicate a test failed.
A message describing the failure.
Throws TestFailedException
to indicate a test failed.
Intercept and return an exception that's expected to be thrown by the passed function value.
Intercept and return an exception that's expected to
be thrown by the passed function value. The thrown exception must be an instance of the
type specified by the type parameter of this method. This method invokes the passed
function. If the function throws an exception that's an instance of the specified type,
this method returns that exception. Else, whether the passed function returns normally
or completes abruptly with a different exception, this method throws TestFailedException
.
Note that the type specified as this method's type parameter may represent any subtype of
AnyRef
, not just Throwable
or one of its subclasses. In
Scala, exceptions can be caught based on traits they implement, so it may at times make sense
to specify a trait that the intercepted exception's class must mix in. If a class instance is
passed for a type that could not possibly be used to catch an exception (such as String
,
for example), this method will complete abruptly with a TestFailedException
.
the function value that should throw the expected exception
an implicit Manifest
representing the type of the specified
type parameter.
the intercepted exception, if it is of the expected type
Executes the block of code passed as the second parameter, and, if it
completes abruptly with a ModifiableMessage
exception,
prepends the "clue" string passed as the first parameter to the beginning of the detail message
of that thrown exception, then rethrows it.
Executes the block of code passed as the second parameter, and, if it
completes abruptly with a ModifiableMessage
exception,
prepends the "clue" string passed as the first parameter to the beginning of the detail message
of that thrown exception, then rethrows it. If clue does not end in a white space
character, one space will be added
between it and the existing detail message (unless the detail message is
not defined).
This method allows you to add more information about what went wrong that will be reported when a test fails. Here's an example:
withClue("(Employee's name was: " + employee.name + ")") { intercept[IllegalArgumentException] { employee.getTask(-1) } }
If an invocation of intercept
completed abruptly with an exception, the resulting message would be something like:
(Employee's name was Bob Jones) Expected IllegalArgumentException to be thrown, but no exception was thrown
This expect
method has been deprecated; Please use expectResult
instead.
This expect
method has been deprecated; Please use expectResult
instead.
To get rid of the deprecation warning, simply replace expect
with
expectResult
. The name expect
will be used for a different purposes in
a future version of ScalaTest.
This expect method has been deprecated. Please replace all invocations of expect with an identical invocation of expectResult instead.
This expect
method has been deprecated; Please use expectResult
instead.
This expect
method has been deprecated; Please use expectResult
instead.
To get rid of the deprecation warning, simply replace expect
with
expectResult
. The name expect
will be used for a different purposes in
a future version of ScalaTest.
This expect method has been deprecated. Please replace all invocations of expect with an identical invocation of expectResult instead.
Trait that contains ScalaTest's basic assertion methods.
You can use the assertions provided by this trait in any ScalaTest
Suite
, becauseSuite
mixes in this trait. This trait is designed to be used independently of anything else in ScalaTest, though, so you can mix it into anything. (You can alternatively import the methods defined in this trait. For details, see the documentation for theAssertions
companion object.In any Scala program, you can write assertions by invoking
assert
and passing in aBoolean
expression, such as:If the passed expression is
true
,assert
will return normally. Iffalse
, Scala'sassert
will complete abruptly with anAssertionError
. This behavior is provided by theassert
method defined in objectPredef
, whose members are implicitly imported into every Scala source file. ThisAssertions
traits defines anotherassert
method that hides the one inPredef
. It behaves the same, except that iffalse
is passed it throwsTestFailedException
instead ofAssertionError
. The reason it throwsTestFailedException
is becauseTestFailedException
carries information about exactly which item in the stack trace represents the line of test code that failed, which can help users more quickly find an offending line of code in a failing test.If you pass the previous
Boolean
expression,left == right
toassert
in a ScalaTest test, a failure will be reported, but without reporting the left and right values. You can alternatively encode these values in aString
passed as a second argument toassert
, like this:Using this form of
assert
, the failure report will include the left and right values, thereby helping you debug the problem. However, ScalaTest provides the===
operator to make this easier. You use it like this:Because you use
===
here instead of==
, the failure report will include the left and right values. For example, the detail message in the thrownTestFailedException
from theassert
shown previously will include, "2 did not equal 1". From this message you will know that the operand on the left had the value 2, and the operand on the right had the value 1.If you're familiar with JUnit, you would use
===
in a ScalaTestSuite
where you'd useassertEquals
in a JUnitTestCase
. The===
operator is made possible by an implicit conversion fromAny
toEqualizer
. If you're curious to understand the mechanics, see the documentation forEqualizer
and theconvertToEqualizer
method.Expected results
Although
===
provides a natural, readable extension to Scala'sassert
mechanism, as the operands become lengthy, the code becomes less readable. In addition, the===
comparison doesn't distinguish between actual and expected values. The operands are just calledleft
andright
, because if one were namedexpected
and the otheractual
, it would be difficult for people to remember which was which. To help with these limitations of assertions,Suite
includes a method calledexpectResult
that can be used as an alternative toassert
with===
. To useexpectResult
, you place the expected value in parentheses afterexpectResult
, followed by curly braces containing code that should result in the expected value. For example:In this case, the expected value is
2
, and the code being tested isa - b
. This expectation will fail, and the detail message in theTestFailedException
will read, "Expected 2, but got 3."Forcing failures
If you just need the test to fail, you can write:
Or, if you want the test to fail with a message, write:
Intercepted exceptions
Sometimes you need to test whether a method throws an expected exception under certain circumstances, such as when invalid arguments are passed to the method. You can do this in the JUnit 3 style, like this:
If
charAt
throwsIndexOutOfBoundsException
as expected, control will transfer to the catch case, which does nothing. If, however,charAt
fails to throw an exception, the next statement,fail()
, will be run. Thefail
method always completes abruptly with aTestFailedException
, thereby signaling a failed test.To make this common use case easier to express and read, ScalaTest provides an
intercept
method. You use it like this:This code behaves much like the previous example. If
charAt
throws an instance ofIndexOutOfBoundsException
,intercept
will return that exception. But ifcharAt
completes normally, or throws a different exception,intercept
will complete abruptly with aTestFailedException
.intercept
returns the caught exception so that you can inspect it further if you wish, for example, to ensure that data contained inside the exception has the expected values.Assumptions
Trait
Assertions
also provides methods that allow you to cancel a test. You would cancel a test if a resource required by the test was unavailable. For example, if a test requires an external database to be online, and it isn't, the test could be canceled to indicate it was unable to run because of the missing database. Such a test assumes a database is available, and you can use theassume
method to indicate this at the beginning of the test, like this:For each overloaded
assert
method, traitAssertions
provides an overloadedassume
method with an identical signature and behavior, except theassume
methods throwTestCanceledException
whereas theassert
methods throwTestFailedException
. As withassert
,assume
hides a Scala method inPredef
that performs a similar function, but throwsAssertionError
. And just as you can withassert
, you can optionally provide a clue string, or use===
to get a more detailed error message. Here are some examples:Forcing cancelations
For each overloaded
fail
method, there's a correspondingcancel
method with an identical signature and behavior, except thecancel
methods throwTestCanceledException
whereas thefail
methods throwTestFailedException
. Thus if you just need to cancel a test, you can write:If you want to cancel the test with a message, just place the message in the parentheses:
Getting a clue
If you want more information that is provided by default by the methods if this trait, you can supply a "clue" string in one of several ways. The extra information (or "clues") you provide will be included in the detail message of the thrown exception. Both
assert
andexpect
provide a way for a clue to be included directly,intercept
does not. Here's an example of clues provided directly inassert
:and in
expect
:The exceptions thrown by the previous two statements will include the clue string,
"this is a clue"
, in the exception's detail message. To get the same clue in the detail message of an exception thrown by a failedintercept
call requires usingwithClue
:The
withClue
method will only prepend the clue string to the detail message of exception types that mix in theModifiableMessage
trait. See the documentation forModifiableMessage
for more information. If you wish to place a clue string after a block of code, see the documentation forAppendedClues
.