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
if the Option[String]
is Some
.
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 message
,
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.
if message
is null
.
if the Option[String]
is Some
.
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 message
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.
if message
is null
.
if the condition is false
.
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
if the condition is false
.
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
.
if left
is null
.
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
if the passed actual
value does not 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
if the passed actual
value does not 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.
if cause
is null
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.
if message
or cause
is null
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.
if message
is null
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
if the passed function does not complete abruptly with an exception
that's an instance of the specified type
passed expected
value.
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
if the passed clue
is null
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
,assert
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 calledexpect
that can be used as an alternative toassert
with===
. To useexpect
, you place the expected value in parentheses afterexpect
, 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."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.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.