CINXE.COM
ScalaTest
<!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <title>ScalaTest</title> <link rel="Stylesheet" href="/assets/stylesheets/main.css" type="text/css" media="screen"/> <link rel="stylesheet" type="text/css" href="/assets/stylesheets/print.css" media="print" /> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-NJWCS90VG5"></script> <script defer src='/assets/javascripts/gtag.js'></script> </head> <body> <div class="FullContainer"> <!-- Top of scalatest.org 660 x 60 [async] --> <script id="adsArtimaScript" type="text/javascript" src="https://www.artima.com/assets/javascripts/4ca150665e51d0b1c3890ca1b891c507-ads.js?product=ScalaTest"></script> <div id="Header"> <div id="Nav"> <a href="/">Home</a> | <a href="/quick_start">Quick Start</a> | <a href="/install">Install</a> | <a href="/user_guide">User Guide</a> | <span><em>At A Glance</em></span> | <a href="/scaladoc">Scaladoc</a> | <a href="/supersafe">SuperSafe</a> | <a href="/plus">Plus</a> | <!-- <a href="/videos">Videos</a> | --> <!-- Temporarily removed per Bill request 171114 PL --> <a href="/donate">Donate</a> | <a href="/about">About</a> </div> <!-- Nav --> </div> <!-- Header --> <div id="body"> <div class="message"> <a class="scalatestLogo" href="/"><img src="/assets/images/scalaTestLogo.gif" width="400" height="60" alt="ScalaTest"/></a> </div> <div style="text-align: left"> <h1>ScalaTest At A Glance</h1> <table style="border-collapse: collapse; border: 1px solid black; width: 100%"> <tr> <div id="StyleTraitOptions"> <th class="ataglance" colspan="4"><a name="style"></a>Style - <span><em>FlatSpec</em></span> | <a href="/at_a_glance/FunSuite">FunSuite</a> | <a href="/at_a_glance/FunSpec">FunSpec</a> | <a href="/at_a_glance/WordSpec">WordSpec</a> | <a href="/at_a_glance/FreeSpec">FreeSpec</a> | <a href="/at_a_glance/RefSpec">RefSpec</a> | <a href="/at_a_glance/PropSpec">PropSpec</a> | <a href="/at_a_glance/FeatureSpec">FeatureSpec</a> </div> <!-- Nav --> </th> </tr> <tr> <td class="ataglance" colspan="4"> <p> FlatSpec is a good first step for teams wishing to move from xUnit to BDD. Its structure is flat like xUnit, so simple and familiar, but the test names must be written in a specification style: "X should Y," "A must B," etc. </p> <pre class="stTight"><span class="stImport">import org.scalatest._</span> <span class="stImport">import flatspec._</span> <span class="stReserved">class</span> <span class="stType">SetSpec</span> <span class="stReserved">extends</span> <span class="stType">AnyFlatSpec</span> { <span class="stReserved">override</span> <span class="stReserved">def</span> withFixture(test: <span class="stType">NoArgTest</span>) = { <span class="stExplain">// Define a shared fixture</span> <span class="stExplain">// Shared setup (run at beginning of each test)</span> <span class="stReserved">try</span> test() <span class="stReserved">finally</span> { <span class="stExplain">// Shared cleanup (run at end of each test)</span> } } <span class="stExplain">// Define the first test for a <em>subject</em>, in this case: "An empty Set"</span> <span class="stLiteral">"An empty Set"</span> should <span class="stLiteral">"have size 0"</span> in { <span class="stExplain">// Name the subject, write 'should', then the rest of the test name</span> assert(<span class="stType">Set</span>.empty.size == <span class="stLiteral">0</span>) <span class="stExplain">// (Can use 'must' or 'can' instead of 'should')</span> } it should <span class="stLiteral">"produce NoSuchElementException when head is invoked"</span> in { <span class="stExplain">// Define another test for the same</span> intercept[<span class="stType">NoSuchElementException</span>] { <span class="stExplain">// subject with 'it'</span> <span class="stType">Set</span>.empty.head } } ignore should <span class="stLiteral">"be empty"</span> in { <span class="stExplain">// To ignore a test, change 'it' to 'ignore'...</span> assert(<span class="stType">Set</span>.empty.isEmpty) } it should <span class="stLiteral">"not be non-empty"</span> ignore { <span class="stExplain">// ...or change 'in' to 'ignore'</span> assert(!<span class="stType">Set</span>.empty.nonEmpty) } <span class="stLiteral">"A non-empty Set"</span> should <span class="stLiteral">"have the correct size"</span> in { <span class="stExplain">// Describe another subject</span> assert(<span class="stType">Set</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>).size == <span class="stLiteral">3</span>) } <span class="stExplain">// 'it' now refers to 'A non-empty Set'</span> it should <span class="stLiteral">"return a contained value when head is invoked"</span> is (pending) <span class="stExplain">// Define a pending test</span> <span class="stImport">import tagobjects.Slow</span> it should <span class="stLiteral">"be non-empty"</span> taggedAs(<span class="stType">Slow</span>) in { <span class="stExplain">// Tag a test</span> assert(<span class="stType">Set(1, 2, 3)</span>.nonEmpty) } } <span class="stExplain">// Can also pass fixtures into tests with FixtureAnyFlatSpec</span> <span class="stReserved">class</span> <span class="stType">StringSpec</span> <span class="stReserved">extends</span> <span class="stType">FixtureAnyFlatSpec</span> { <span class="stReserved">type</span> FixtureParam = <span class="stType">String</span> <span class="stExplain">// Define the type of the passed fixture object</span> <span class="stReserved">override</span> <span class="stReserved">def</span> withFixture(test: <span class="stType">OneArgTest</span>) = { <span class="stExplain">// Shared setup (run before each test), including...</span> <span class="stReserved">val</span> fixture = <span class="stLiteral">"a fixture object"</span> <span class="stExplain">// ...creating a fixture object</span> <span class="stReserved">try</span> test(fixture) <span class="stExplain">// Pass the fixture into the test</span> <span class="stReserved">finally</span> { <span class="stExplain">// Shared cleanup (run at end of each test)</span> } } <span class="stLiteral">"The passed fixture"</span> can <span class="stLiteral">"be used in the test"</span> in { s => <span class="stExplain">// Fixture passed in as s</span> assert(s == <span class="stLiteral">"a fixture object"</span>) } } @DoNotDiscover <span class="stExplain">// Disable discovery of a test class</span> <span class="stReserved">class</span> <span class="stType">InvisibleSpec</span> <span class="stReserved">extends</span> <span class="stType">AnyFlatSpec</span> { <span class="stBlockComment">/*code omitted*/</span> } @Ignore <span class="stExplain">// Ignore all tests in a test class</span> <span class="stReserved">class</span> <span class="stType">IgnoredSpec</span> <span class="stReserved">extends</span> <span class="stType">AnyFlatSpec</span> { <span class="stBlockComment">/*code omitted*/</span> } <span class="stImport">import tags.Slow</span> @Slow <span class="stExplain">// Mark all tests in a test class with a tag</span> <span class="stReserved">class</span> <span class="stType">SlowSpec</span> <span class="stReserved">extends</span> <span class="stType">AnyFlatSpec</span> { <span class="stBlockComment">/*code omitted*/</span> } </pre> </td> </tr> <tr> <td class="ataglance" colspan="4"> Note: The remainder of this page is identical no matter which style trait you use. </td> </tr> <tr> <th class="ataglance" colspan="4"><a name="inside"></a>Assertions</th> </tr> <tr> <td class="ataglance" colspan="4"> <pre class="stTight"><span class="stImport">import org.scalatest._ import Assertions._ val (a, b, c, d) = (1, 2, 3, 4)</span> assert(a == b || c >= d) <span class="stExplain">// Error message: 1 did not equal 2, and 3 was not greater than or equal to 4</span> assertResult(2) { c + d } <span class="stExplain">// Error message: Expected 2, but got 7</span> intercept[<span class="stType">IllegalArgumentException</span>] { <span class="stExplain">// Error message: Expected exception java.lang.IllegalArgumentException</span> c / <span class="stLiteral">0</span> <span class="stExplain">// to be thrown, but java.lang.ArithmeticException was thrown.</span> } fail(<span class="stLiteral">"I've got a bad feeling about this"</span>) <span class="stExplain">// Error message: I've got a bad feeling about this</span> <span class="stExplain">// Succeeds if doesn't compile because of type or syntax error</span> assertDoesNotCompile(<span class="stLiteral">"val a: Int = 1"</span>) <span class="stExplain">// Error message: Expected a compiler error, but got none for code:</span> <span class="stExplain">// val a: Int = 1</span> <span class="stExplain">// Succeeds only if a type error</span> assertTypeError(<span class="stLiteral">"val a: Int = 1"</span>) <span class="stExplain">// Error message: Expected a type error, but got none for code:</span> <span class="stExplain">// val a: Int = 1</span> <span class="stExplain">// Succeeds only if no type or syntax error</span> assertCompiles(<span class="stLiteral">"_"</span>) <span class="stExplain">// Error message: Expected no compiler error, but got the following parse error:</span> <span class="stExplain">// "unbound placeholder parameter", for code: _</span> <span class="stExplain">// Tests that can't run because of missing preconditions should be <em>canceled</em></span> cancel(<span class="stLiteral">"Network was down"</span>) <span class="stExplain">// Error message: Network was down</span> <span class="stExplain">// Tests can also be canceled with assume</span> <span class="stImport">case class Database(available: Boolean) val db = Database(false)</span> assume(db.available) <span class="stExplain">// Error message: db.available was false</span> <span class="stExplain">// You can add clues to error messages</span> assert(a == b, <span class="stLiteral">", but you already knew that"</span>) <span class="stExplain">// Error message: 1 did not equal 2, but you already knew that</span> assertResult(2, <span class="stLiteral">", what a bummer!"</span>) { a + b } <span class="stExplain">// Error message: Expected 2, but got 3, what a bummer!</span> assume(db.available, <span class="stLiteral">"yet again"</span>) <span class="stExplain">// Error message: db.available was false yet again</span> withClue(<span class="stLiteral">"prepended clue"</span>) { <span class="stExplain">// Error message: prepended clue 1 did not equal 2</span> assert(a == b) } <span class="stImport">import AppendedClues._</span> assert(a == b) withClue <span class="stLiteral">"appended clue"</span> <span class="stExplain">// Error message: 1 did not equal 2 appended clue</span> </pre> </tr> <tr> <th colspan="4" class="ataglance"><a name="matchers_equality_identity"></a>Matchers - Equality and identity</th> </tr> <tr> <td class="ataglance" colspan="4"> <pre class="stTight"> <span class="stImport">import org.scalatest.Matchers._ def incr(i: Int) = i + 1 val result = incr(2)</span> result should equal (<span class="stLiteral">3</span>) <span class="stExplain">// By default, calls <em>left</em> <code>==</code> <em>right</em>, except for arrays</span> result should be (<span class="stLiteral">3</span>) <span class="stExplain">// Calls <em>left</em> <code>==</code> <em>right</em>, except for arrays</span> result should === (<span class="stLiteral">3</span>) <span class="stExplain">// By default, calls <em>left</em> <code>==</code> <em>right</em>, except for arrays</span> <span class="stType">Array</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>) should equal (<span class="stType">Array</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>)) <span class="stExplain">// Arrays are compared structurally,</span> <span class="stType">Array</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>) should be (<span class="stType">Array</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>)) <span class="stExplain">// by calling <em>left</em><code>.deep</code> <code>==</code> <em>right</em><code>.deep</code></span> <span class="stType">Array</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>) should === (<span class="stType">Array</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>)) result shouldEqual <span class="stLiteral">3</span> <span class="stExplain">// Alternate forms for equal and be</span> result shouldBe <span class="stLiteral">3</span> <span class="stExplain">// that don't require parentheses</span> <span class="stExplain">// The === syntax can give you a type error for suspicious equality comparisons</span> <span class="stImport">import org.scalactic._ import TypeCheckedTripleEquals._</span> result should === ("three") <span class="stExplain">// Fails to compile with error message: types String and Int // do not adhere to the type constraint selected for the === and // !== operators; the missing implicit parameter is of type // org.scalactic.Constraint[String,Int] // greeting should === (3) // ^</span> <span class="stExplain">// The equal matcher and === syntax compare with an implicit Equality[L],</span> <span class="stExplain">// where L is the left hand type (i.e., the type of greeting below)</span> val greeting = "Hi" greeting should equal ("hi") <span class="stExplain">// Both fail with error message: "[H]i" did not equal "[h]i"</span> greeting should === ("hi") <span class="stImport">import org.scalactic._</span> <span class="stExplain">// Can provide a custom Equality[L] explicitly</span> <span class="stImport">import Explicitly._</span> <span class="stImport">import StringNormalizations._</span> greeting should equal ("hi") (after being lowerCased) <span class="stExplain">// Both succeed, because 'after being lowercased'</span> (greeting should === ("hi")) (after being lowerCased) <span class="stExplain">// defines an Equality[String] that ignores case</span> <span class="stImport">implicit val strEq = lowerCased.toEquality</span> <span class="stExplain">// Can provide a custom Equality[L] implicitly</span> greeting should equal ("hi") <span class="stExplain">// Succeeds because equal the implicit Equality[String]</span> greeting should === ("hi") <span class="stExplain">// (defined as strEq) that ignores case</span> greeting should not equal "bye" <span class="stExplain">// equal and be are negated with not</span> greeting should not be "such sweet sorrow" greeting should !== ("ho") <span class="stExplain">// === is negated as !==</span> <span class="stImport">val list = List(1, 2, 3) <span class="stExplain">// Ensure two objects have the same identity</span> val ref1 = list val ref2 = list</span> ref1 should be theSameInstanceAs ref2 <span class="stExplain">// Calls <em>left</em> <code>eq</code> <em>right</em></span> </pre> </tr> <tr> <th class="ataglance" colspan="4"><a name="object_class"></a>Matchers - Object's class</th> </tr> <tr> <td class="ataglance" colspan="4"> <pre class="stTight"> <span class="stImport">import org.scalatest.Matchers._ class Tiger val tiger = new Tiger</span> tiger shouldBe a [<span class="stType">Tiger</span>] <span class="stExplain">// Ensure an object is an instance of a specified class or trait</span> <span class="stImport">val tigerList = List(tiger)</span> <span class="stExplain">// Because type parameters are erased on the JVM, you must use</span> tigerList shouldBe a [<span class="stType">List</span>[_]] <span class="stExplain">// an underscore for any type parameters when using this syntax</span> </pre> </td> </tr> <tr> <th colspan="4" class="ataglance"><a name="matchers_expected_exceptions"></a>Matchers - Expected exceptions</th> </tr> <tr> <td class="ataglance" colspan="4"> <pre class="stTight"> <span class="stImport">import org.scalatest.Matchers._ val s = "hai"</span> an [<span class="stType">IndexOutOfBoundsException</span>] should be thrownBy { <span class="stExplain">// Ensure a particular exception type is thrown</span> s.charAt(-<span class="stLiteral">1</span>) } <span class="stReserved">val</span> caught = the [<span class="stType">IndexOutOfBoundsException</span>] thrownBy { <span class="stExplain">// Capturing an expected exception in a variable</span> s.charAt(-<span class="stLiteral">1</span>) } the [<span class="stType">IndexOutOfBoundsException</span>] thrownBy { <span class="stExplain">// Inspecting an expected exception</span> s.charAt(-<span class="stLiteral">1</span>) } should have message (<span class="stQuotedString">"String index out of range: -1"</span>) </pre> </td> </tr> <tr> <th colspan="4" class="ataglance"><a name="matchers_size_length"></a>Matchers - Length and size</th> </tr> <tr> <td class="ataglance" colspan="4"> <pre class="stTight"> <span class="stImport">import org.scalatest.Matchers._ val result = "hai"</span> result should have length <span class="stLiteral">3</span> <span class="stExplain">// Works for any type T for which an implicit Length[T] is available, including String,</span> <span class="stExplain">// Array, scala.collection.GenSeq, java.util.List, and any object with length field or</span> <span class="stExplain">// method or getLength method; the length value can be Int or Long.</span> result should have size <span class="stLiteral">3</span> <span class="stExplain">// Works for any type T for which an implicit Size[T] is available, including String, Array,</span> <span class="stExplain">// scala.collection.GenTraversable, java.util.Collection, java.util.Map, and any object with</span> <span class="stExplain">// size field or method or getSize method; the size value can be Int or Long.</span> </pre> </td> </tr> <tr> <th colspan="4" class="ataglance"><a name="matchers_string_regex"></a>Matchers - Strings and regular expressions</th> </tr> <tr> <td class="ataglance" colspan="4"> <pre class="stTight"> <span class="stImport">import org.scalatest.Matchers._ val string = "Hello World"</span> string should startWith (<span class="stQuotedString">"Hello"</span>) <span class="stExplain">// starts with the given substring</span> string should endWith (<span class="stQuotedString">"world"</span>) <span class="stExplain">// ends with the given substring</span> string should include (<span class="stQuotedString">"seven"</span>) <span class="stExplain">// includes the given substring</span> string should startWith regex <span class="stQuotedString">"Hel*o"</span> <span class="stExplain">// starts with a substring matching the regular expression</span> string should startWith regex <span class="stQuotedString">"Hel*o"</span>.r <span class="stExplain">// (works with String or Regex)</span> string should endWith regex <span class="stQuotedString">"wo.ld"</span>.r <span class="stExplain">// ends with a substring matching the regular expression</span> string should include regex <span class="stQuotedString">"wo.ld"</span>.r <span class="stExplain">// includes substring matching regular expression</span> <span class="stImport">val re = """(-)?(\d+)(\.\d*)?""".r</span> string should fullyMatch regex re <span class="stExplain">// includes substring matching regular expression</span> </pre> </tr> <tr> <th colspan="4" class="ataglance"><a name="matchers_order_ranges"></a>Matchers - Order and ranges</th> </tr><tr> <td class="ataglance" colspan="4"> <pre class="stTight"> <span class="stImport">import org.scalatest.Matchers._ val one = 1</span> one should be < <span class="stLiteral">7</span> <span class="stExplain">// works for any T when an implicit Ordered[T] exists</span> one should be <= <span class="stLiteral">7</span> one should be >= <span class="stLiteral">0</span> <span class="stImport">val num = 8</span> num should (be >= <span class="stLiteral">1</span> and be <= <span class="stLiteral">10</span>) <span class="stExplain">// one way to ensure a value is within a range or tolerance.</span> <span class="stImport">val seven = 7</span> seven should be (<span class="stLiteral">6</span> +- <span class="stLiteral">2</span>) <span class="stExplain">// another way to verify a range</span> 7.0 should be (<span class="stLiteral">6.9</span> +- <span class="stLiteral">0.2</span>) <span class="stExplain">// works for both integral and floating point types</span> </pre> </td> </tr> <tr> <th colspan="4" class="ataglance"><a name="matchers_boolean_be"></a>Matchers - Checking boolean properties with <code>be</code></th> </tr> <tr> <td class="ataglance" colspan="4"> <pre class="stTight"> <span class="stImport">import org.scalatest.Matchers._</span> <span class="stExplain">// access a boolean property dynamically (via reflection)</span> <span class="stType">Set</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>) should be (<span class="stQuotedString">'nonEmpty</span>) <span class="stExplain">// will also match isNonEmpty</span> <span class="stImport">import java.io.File val temp = new File("aFile.txt")</span> temp should be a (<span class="stQuotedString">'file</span>) <span class="stExplain">// can use an optional 'a' or 'an'</span> <span class="stImport">import java.awt._ import event.KeyEvent val canvas = new Canvas val keyEvent = new KeyEvent(canvas, 0, 0, 0, 0, '0')</span> keyEvent should be an (<span class="stQuotedString">'actionKey</span>) <span class="stImport">import org.scalatest._</span> <span class="stReserved">class</span> <span class="stType">FileBePropertyMatcher</span> <span class="stReserved">extends</span> <span class="stExplain">// For static checking, a <a href="http://www.artima.com/docs-scalatest-3.2.0-RC3/#org.scalatest.matchers.BePropertyMatcher">BePropertyMatcher</a> can be defined.</span> <span class="stType">BePropertyMatcher</span>[<span class="stType">java.io.File</span>] { <span class="stReserved">def</span> apply(left: <span class="stType">File</span>) = <span class="stType">BePropertyMatchResult</span>(left.isFile, <span class="stType">"file"</span>) } <span class="stReserved">val</span> file = <span class="stReserved">new</span> <span class="stType">FileBePropertyMatcher</span> <span class="stReserved">val</span> temp = new <span class="stType">File</span>(<span class="stLiteral">"aFile.txt"</span>) temp should be a file </pre></td> </tr> <tr> <th colspan="4" class="ataglance"><a name="matchers_be_matchers"></a>Matchers - Using arbitrary <code>be</code> matchers</th> </tr> <tr> <td class="ataglance" colspan="4"> <pre class="stTight"><span class="stImport">import org.scalatest._ <span class="stExplain">// To place an arbitrary token after <code>be</code>, use a <a href="http://www.artima.com/docs-scalatest-3.2.0-RC3/#org.scalatest.matchers.BeMatcher">BeMatcher</a>.</span> import Matchers._ import matchers._</span> <span class="stReserved">class</span> <span class="stType">OddMatcher</span> <span class="stReserved">extends</span> <span class="stType">BeMatcher</span>[<span class="stType">Int</span>] { <span class="stReserved">def</span> apply(left: <span class="stType">Int</span>) = <span class="stType">MatchResult</span>( left % 2 == 1, left.toString + <span class="stLiteral">" was even"</span>, left.toString + <span class="stLiteral">" was odd"</span> ) } <span class="stReserved">val</span> odd = <span class="stReserved">new</span> <span class="stType">OddMatcher</span> <span class="stLiteral">7</span> should be (odd)</pre> </td> </tr> <tr> <th colspan="4" class="ataglance"><a name="matchers_have_prop"></a>Matchers - Checking arbitrary properties with <code>have</code></th> </tr> <tr> <td class="ataglance" colspan="4"> <pre class="stTight"> <span class="stImport">import org.scalatest.Matchers._</span> <span class="stImport">case class Book(title: String, author: String, pubYear: Int) val book = Book("A Book", "Sally", 2008)</span> book should have ( <span class="stExplain">// check arbitrary properties dynamically (via reflection)</span> <span class="stQuotedString">'title</span> (<span class="stQuotedString">"A Book"</span>), <span class="stExplain">// see <a href="http://www.artima.com/docs-scalatest-3.2.0-RC3/#org.scalatest.Matchers$HavePropertyMatcherGenerator">HavePropertyMatcherGenerator</a></span> <span class="stQuotedString">'author</span> (<span class="stQuotedString">"Sally"</span>), <span class="stQuotedString">'pubYear</span> (<span class="stLiteral">2008</span>) ) <span class="stImport">import org.scalatest._ <span class="stExplain">// check arbitrary properties statically with <a href="http://www.artima.com/docs-scalatest-3.2.0-RC3/#org.scalatest.matchers.HavePropertyMatcher">HavePropertyMatchers</a></span> import Matchers._ import matchers._</span> <span class="stReserved">case</span> <span class="stReserved">class</span> <span class="stType">Book</span>(title: <span class="stType">String</span>, author: <span class="stType">String</span>) <span class="stReserved">def</span> title(expectedValue: <span class="stType">String</span>) = <span class="stReserved">new</span> <span class="stType">HavePropertyMatcher</span>[<span class="stType">Book</span>, <span class="stType">String</span>] { <span class="stReserved">def</span> apply(book: <span class="stType">Book</span>) = <span class="stType">HavePropertyMatchResult</span>( book.title == expectedValue, <span class="stLiteral">"title"</span>, expectedValue, book.title ) } <span class="stReserved">def</span> author(expectedValue: <span class="stType">String</span>) = <span class="stReserved">new</span> <span class="stType">HavePropertyMatcher</span>[<span class="stType">Book</span>, <span class="stType">String</span>] { <span class="stReserved">def</span> apply(book: <span class="stType">Book</span>) = <span class="stType">HavePropertyMatchResult</span>( book.author == expectedValue, <span class="stLiteral">"author"</span>, expectedValue, book.author ) } <span class="stLiteral">val</span> book = <span class="stType">Book</span>(<span class="stLiteral">"A Book"</span>, <span class="stLiteral">"Sally"</span>) book should have ( <span class="stQuotedString">title</span> (<span class="stQuotedString">"A Book"</span>), <span class="stQuotedString">author</span> (<span class="stQuotedString">"Sally"</span>) ) </pre> </td> </tr> <tr> <th colspan="4" class="ataglance"><a name="matchers_scala_java_col"></a>Matchers - Working with Scala and Java collections</th> </tr> <tr> <td class="ataglance" colspan="4"> <pre class="stTight"><span class="stImport">import org.scalatest.Matchers._</span> <span class="stType">List</span>.empty[<span class="stType">Int</span>] should be (empty) <span class="stExplain">// various ways of checking whether a collection is empty </span> <span class="stType">List</span>.empty[<span class="stType">Int</span>] should be (<span class="stType">Nil</span>) <span class="stType">Map</span>.empty[<span class="stType">Int</span>, <span class="stType">String</span>] should be (<span class="stType">Map()</span>) <span class="stType">Set</span>.empty[<span class="stType">Int</span>] should be (<span class="stType">Set.empty</span>) <span class="stType">Set</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>) should have size (<span class="stLiteral">3</span>) <span class="stExplain">// check the size of a collection </span> <span class="stType">Set</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>) should have size (<span class="stLiteral">3</span>) <span class="stType">List</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>) should have length (<span class="stLiteral">3</span>) <span class="stType">Set</span>(<span class="stLiteral">"three"</span>, <span class="stLiteral">"five"</span>, <span class="stLiteral">"seven"</span>) should contain (<span class="stQuotedString">"five"</span>) <span class="stExplain">// check whether a traversable contains an element</span> <span class="stImport">val map = Map(1 -> "one", 2 -> "two", 3 -> "three")</span> map should contain key (<span class="stLiteral">1</span>) <span class="stExplain">// check whether a map contains a particular key or value</span> should contain value (<span class="stQuotedString">"two"</span>) <span class="stImport">import PartialFunctionValues._</span> <span class="stExplain">// check both that a map is defined at a key as</span> <span class="stType">Map</span>(<span class="stLiteral">"I"</span> -> <span class="stLiteral">1</span>, <span class="stLiteral">"II"</span> -> <span class="stLiteral">2</span>, <span class="stLiteral">"III"</span> -> <span class="stLiteral">3</span>).valueAt(<span class="stQuotedString">"II"</span>) should equal (<span class="stQuotedString">2</span>) <span class="stExplain">// well as something about its value</span> <span class="stImport">import java.util._ val javaCol = new ArrayList[Int]</span> javaCol should be (empty) <span class="stExplain">// Check if a java.util.Collection is empty</span></span> <span class="stImport"> val javaMap = new java.util.HashMap[Int, String] javaMap.put(1, "one") javaMap.put(2, "two") javaMap.put(3, "three")</span> javaMap should have size (3) <span class="stExplain">// check the size of a java.util.Map</span> <span class="stImport">val javaList = new ArrayList[Int] javaList.add(1) javaList.add(2) javaList.add(3)</span> javaList should have length (<span class="stLiteral">3</span>) <span class="stExplain">// check the length of a java.util.List</span> <span class="stImport">val javaCol = new HashSet[String] javaCol.add("three") javaCol.add("five") javaCol.add("seven")</span> <span class="stExplain">// Check whether a java.util.Collection contains a</span> javaCol should contain (<span class="stQuotedString">"five"</span>) <span class="stExplain">// particular element</span> <span class="stImport">val javaMap = new HashMap[Int, String] javaMap.put(1, "one") javaMap.put(2, "two") javaMap.put(3, "three")</span> javaMap should contain key (<span class="stLiteral">1</span>) <span class="stExplain">// Check whether a java.util.Map contains a particular key</span> <span class="stImport">val javaMap = new HashMap[Int, String] javaMap.put(1, "Hi") javaMap.put(2, "Howdy") javaMap.put(3, "Hai")</span> javaMap should contain value (<span class="stQuotedString">"Howdy"</span>) <span class="stExplain">// Check whether a java.util.Map contains a particular value</span> </pre> </td> </tr> <tr> <th colspan="4" class="ataglance"><a name="matchers_option_either"></a>Matchers - Working with <code>Option</code> and <code>Either</code></th> </tr> <tr> <td class="ataglance" colspan="4"> <pre class="stTight"><span class="stImport">import org.scalatest.Matchers._ val option = Some(1)</span> option should be (<span class="stType">Some</span>(<span class="stLiteral">1</span>)) <span class="stExplain">// Check whether an Option contains a certain value</span> <span class="stImport">val option = Some(1)</span> option shouldBe <span class="stLiteral">defined</span> <span class="stExplain">// Check whether an Option is defined</span> <span class="stImport">val option = Some(1)</span> option shouldBe empty <span class="stExplain">// Check whether an Option is empty</span> <span class="stImport">val option = None</span> option should be (<span class="stType">None</span>) <span class="stExplain">// Another way to check whether an Option is empty</span> <span class="stImport">val either = Left(1)</span> either should be (<span class="stType">'left</span>) <span class="stExplain">// Check whether an Either is a Left</span> <span class="stImport">val either = Left(1)</span> either should be (<span class="stType">Left</span>(<span class="stLiteral">1</span>)) <span class="stExplain">// Check whether an Either is Left with a certain value</span> <span class="stImport">val either = Right(1)</span> either should be (<span class="stType">'right</span>) <span class="stExplain">// Check whether an Either is a Right</span> <span class="stImport">val either = Right(1)</span> either should be (<span class="stType">Right</span>(<span class="stLiteral">1</span>)) <span class="stExplain">// Check whether an Either is Right with a certain value</span> <span class="stImport">import org.scalatest.EitherValues._ val either = Right(3)</span> <span class="stExplain">// Check both that an Either is defined as well as something</span> either.right.value should be < <span class="stLiteral">7</span> <span class="stExplain">// about its value</span> </pre> </td> </tr> <tr> <th class="ataglance" colspan="4"><a name="matchers_emptiness"></a>Checking for emptiness</th> </tr> <tr> <td class="ataglance" colspan="4"> <pre class="stTight"><span class="stImport">import org.scalatest.Matchers._</span> <span class="stType">List</span>.empty shouldBe empty <span class="stExplain">// Check if a List (and any GenTraversable) is empty</span> <span class="stType">None</span> shouldBe empty <span class="stExplain">// Check if an Option is empty</span> <span class="stLiteral">""</span> shouldBe empty <span class="stExplain">// Check if a String is empty</span> new java.util.<span class="stType">HashMap</span>[<span class="stType">Int</span>, <span class="stType">Int</span>] shouldBe empty <span class="stExplain">// Check if a java.util.Map is empty</span> <span class="stReserved">new</span> { <span class="stReserved">def</span> isEmpty = <span class="stReserved">true</span> } shouldBe empty <span class="stExplain">// Check if a structural type is empty</span> <span class="stType">Array</span>.empty shouldBe empty <span class="stExplain">// Check if an Array is empty</span> </pre> </td> </tr> <tr> <th class="ataglance" colspan="4"><a name="matchers_containers"></a>Matchers - Working with <code>"containers"</code></th> </tr> <tr> <td class="ataglance" colspan="4"> <pre class="stTight"><span class="stImport">import org.scalatest.Matchers._</span> <span class="stType">List</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>) should contain (<span class="stLiteral">2</span>) <span class="stExplain">// Check if a List (or any GenTraversable) contains the</span> <span class="stExplain">// specified element</span> <span class="stType">Map</span>(<span class="stLiteral">'a'</span> -> <span class="stLiteral">1</span>, <span class="stLiteral">'b'</span> -> <span class="stLiteral">2</span>, <span class="stLiteral">'c'</span> -> <span class="stLiteral">3</span>) should contain (<span class="stLiteral">'b'</span> -> <span class="stLiteral">2</span>) <span class="stExplain">// Check if a Map contains the specified mapping</span> <span class="stType">Set</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>) should contain (<span class="stLiteral">2</span>) <span class="stExplain">// Check if various collections contain a specified element</span> <span class="stType">Array</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>) should contain (<span class="stLiteral">2</span>) <span class="stLiteral">"123"</span> should contain (<span class="stLiteral">'2'</span>) <span class="stType">Some</span>(<span class="stLiteral">2</span>) should contain (<span class="stLiteral">2</span>) <span class="stImport">import org.scalactic.Explicitly._ <span class="stExplain">// Using <a href="http://www.artima.com/docs-scalatest-3.2.0-RC3/#org.scalactic.StringNormalizations"><code>StringNormalizations</code></a> explicitly</span> import org.scalactic.StringNormalizations._</span> (<span class="stType">List</span>(<span class="stLiteral">"Hi"</span>, <span class="stLiteral">"Di"</span>, <span class="stLiteral">"Ho"</span>) should contain (<span class="stLiteral">"ho"</span>)) (after being lowerCased) </pre> </td> </tr> <tr> <th class="ataglance" colspan="4"><a name="matchers_aggregations"></a>Matchers - Working with <code>"aggregations"</code></th> </tr> <tr> <td class="ataglance" colspan="4"> <pre class="stTight"> <span class="stImport">import org.scalatest.Matchers._</span> <span class="stType">List</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>) should contain atLeastOneOf (<span class="stLiteral">2</span>, <span class="stLiteral">3</span>, <span class="stLiteral">4</span>) <span class="stExplain">// Check if a List (or any GenTraversable) contains at least one of</span> <span class="stExplain">// the specified elements</span> <span class="stType">Array</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>) should contain atLeastOneOf (<span class="stLiteral">3</span>, <span class="stLiteral">4</span>, <span class="stLiteral">5</span>) <span class="stExplain">// Check if an Array contains at least one of the specified</span> <span class="stExplain">// elements</span> <span class="stLiteral">"abc"</span> should contain atLeastOneOf (<span class="stLiteral">'c'</span>, <span class="stLiteral">'a'</span>, <span class="stLiteral">'t'</span>) <span class="stExplain">// Check if a String contains at least one of the specified</span> <span class="stExplain">// characters</span> <span class="stImport">import org.scalactic.Explicitly._ <span class="stExplain">// Using <a href="http://www.artima.com/docs-scalatest-3.2.0-RC3/#org.scalactic.StringNormalizations"><code>StringNormalizations</code></a> explicitly</span> import org.scalactic.StringNormalizations._</span> (<span class="stType">Vector</span>(<span class="stLiteral">" A"</span>, <span class="stLiteral">"B "</span>) should contain atLeastOneOf (<span class="stLiteral">"a "</span>, <span class="stLiteral">"b"</span>, <span class="stLiteral">"c"</span>)) (after being lowerCased and trimmed) <span class="stType">List</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>, <span class="stLiteral">4</span>, <span class="stLiteral">5</span>) should contain atMostOneOf (<span class="stLiteral">5</span>, <span class="stLiteral">6</span>, <span class="stLiteral">7</span>) <span class="stExplain">// Check if a List (or any GenTraversable) contains at most</span> <span class="stExplain">// one of the specified element</span> <span class="stType">List</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>, <span class="stLiteral">4</span>, <span class="stLiteral">5</span>) should contain allOf (<span class="stLiteral">2</span>, <span class="stLiteral">3</span>, <span class="stLiteral">5</span>) <span class="stExplain">// Check if a List (or any GenTraversable) contains all of</span> <span class="stExplain">// the specified elements</span> <span class="stType">List</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>, <span class="stLiteral">2</span>, <span class="stLiteral">1</span>) should contain only (<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>) <span class="stExplain">// Check if a List (or any GenTraversable) contains only the</span> <span class="stExplain">// specified elements</span> <span class="stReserved">val</span> expected = <span class="stType">Vector</span>(<span class="stLiteral">3</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>, <span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>) <span class="stExplain">// Check if a List (or any GenTraversable) contains</span> <span class="stType">List</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>, <span class="stLiteral">3</span>, <span class="stLiteral">3</span>) should contain theSameElementsAs expected <span class="stExplain">// only the same elements as the specified Vector</span> <span class="stExplain">// (or any GenTraversable)</span> </pre> </td> </tr> <tr> <th class="ataglance" colspan="4"><a name="matchers_sequences"></a>Matchers - Working with <code>"sequences"</code></th> </tr> <tr> <td class="ataglance" colspan="4"> <pre class="stTight"> <span class="stImport">import org.scalatest.Matchers._</span> <span class="stExplain">// Check if a List (or any GenTraversable)</span> <span class="stType">List</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>, <span class="stLiteral">3</span>, <span class="stLiteral">3</span>) should contain inOrderOnly (<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>) <span class="stExplain">// contains only the specified elements in same</span> <span class="stExplain">// order</span> <span class="stType">Array</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>, <span class="stLiteral">3</span>, <span class="stLiteral">3</span>) should contain inOrderOnly (<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>) <span class="stExplain">// Check if an Array contains only the specified</span> <span class="stExplain">// elements in same order</span> <span class="stLiteral">"122333"</span> should contain inOrderOnly (<span class="stLiteral">'1'</span>, <span class="stLiteral">'2'</span>, <span class="stLiteral">'3'</span>) <span class="stExplain">// Check if a String contains only the specified</span> <span class="stExplain">// characters in same order</span> <span class="stType">List</span>(<span class="stLiteral">0</span>, <span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">2</span>, <span class="stLiteral">99</span>, <span class="stLiteral">3</span>, <span class="stLiteral">3</span>, <span class="stLiteral">3</span>, <span class="stLiteral">5</span>) should contain inOrder (<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>) <span class="stExplain">// Check if a List (or any GenTraversable)</span> <span class="stExplain">// contains the specified elements in same order</span> <span class="stType">List</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>) should contain theSameElementsInOrderAs <span class="stType">Vector</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>) <span class="stExplain">// Check if a List (or any GenTraversable)</span> <span class="stExplain">// contains the same elements in same order as the</span> <span class="stExplain">// specified Vector (or any GenTraversable)</span> </pre> </tr> <tr> <th class="ataglance" colspan="4"><a name="matchers_iterators"></a>Matchers - Working with <code>iterators</code></th> </tr> <tr> <td class="ataglance" colspan="4"> <pre class="stTight"> <span class="stImport">import org.scalatest.Matchers._</span><span class="stExplain">// Use toStream to convert Iterator to Stream to work with contain</span> <span class="stReserved">val</span> it = <span class="stType">List</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>).iterator it.toStream should contain (<span class="stLiteral">2</span>) </pre> </tr> <tr> <th class="ataglance" colspan="4"><a name="matchers_file"></a>Matchers - Working with <code>File</code></th> </tr> <tr> <td class="ataglance" colspan="4"> <pre class="stTight"> <span class="stImport">import org.scalatest.Matchers._ import java.io.File val javaHomeDir = new File(System.getProperty("java.home"))</span> javaHomeDir should exist <span class="stExplain">// Check if a directory exists </span> <span class="stImport">val javaHomeLicenseFile = javaHomeDir + System.getProperty("file.separator") + "LICENSE")</span> javaHomeLicenseFile should exist <span class="stExplain">// Check if a file exists </span> javaHomeLicenseFile shouldBe readable <span class="stExplain">// Check if a file is readable </span> javaHomeLicenseFile shouldBe writable <span class="stExplain">// Check if a file is writable </span> </pre> </tr> <tr> <th class="ataglance" colspan="4"><a name="matchers_inspector_shorthands"></a>Matchers - Inspector Shorthands</th> </tr> <tr> <td class="ataglance" colspan="4"> <pre class="stTight"> <span class="stImport">import org.scalatest.Matchers._ val xs = List(1, 2, 3, 4, 5)</span> all (xs) should be < 10 <span class="stExplain">// check that all elements in xs are less than 10</span> atMost (2, xs) should be >= 4 <span class="stExplain">// check that at most 2 elements in xs are greater than or</span> <span class="stExplain">// equal to 4</span> atLeast (3, xs) should be < 5 <span class="stExplain">// check that at least 3 elements in xs are lesser than 5</span> between (2, 3, xs) should (be > 1 and be < 5) <span class="stExplain">// check that 2 to 3 elements in xs are greater than 5 and less</span> <span class="stExplain">// than 5</span> exactly (2, xs) should be <= 2 <span class="stExplain">// check that 2 elements in xs are less than or equal to 2</span> every (xs) should be < 10 <span class="stExplain">// check that all elements in xs are less than 10</span> </pre> </td> </tr> <tr> <th colspan="4" class="ataglance"><a name="matchers_and_or_not"></a>Matchers - Logical expressions with <code>and</code>, <code>or</code>, and <code>not</code></th> </tr> <tr> <td class="ataglance" colspan="4"> <pre class="stTight"> <span class="stImport">import org.scalatest.Matchers._ val result = 8</span> result should (be > <span class="stLiteral">0</span> and be < <span class="stLiteral">10</span>) <span class="stExplain">// You can <em>and</em> matcher expressions together</span> <span class="stImport">val map = Map("hi" -> "HI", "hei" -> "HEI", "he" -> "HE")</span> map should (contain key (<span class="stQuotedString">"hi"</span>) or contain key (<span class="stQuotedString">"ho"</span>)) <span class="stExplain">// You can <em>or</em> matcher expressions together</span> <span class="stImport">val result = "Hello Word"</span> <span class="stExplain">// You can negate a matcher expression</span> result should not be (<span class="stReserved">null</span>) <span class="stImport">val map = Map("one" -> 1, "two" -> 2, "three" -> 3)</span> map should (contain key (<span class="stQuotedString">"two"</span>) and not contain value (<span class="stLiteral">7</span>)) <span class="stExplain">// Another example of negation</span> </pre> </td> </tr> <tr> <th colspan="4" class="ataglance"><a name="matchers_diff_matcher"></a>Matchers - When you need a different matcher</th> </tr> <tr> <td class="ataglance" colspan="4"> <pre class="stTight"><span class="stImport">import org.scalatest.Matchers._ import java.awt._ import event.KeyEvent val canvas = new Canvas <span class="stExplain">// You can check boolean properties dynamically with be and</span> val keyEvent = new KeyEvent(canvas, 0, 0, 0, 0, '0')</span> <span class="stExplain">// a Symbol (the tick mark syntax). For statically typed</span> keyEvent should be an (<span class="stQuotedString">'actionKey</span>) <span class="stExplain">// approach, use a <a href="http://www.artima.com/docs-scalatest-3.2.0-RC3/#org.scalatest.matchers.BePropertyMatcher">BePropertyMatcher</a> or <a href="http://www.artima.com/docs-scalatest-3.2.0-RC3/#org.scalatest.matchers.BeMatcher">BeMatcher</a>.</span> <span class="stImport">case class Book(title: String, author: String) <span class="stExplain">// You can check arbitrary properties dynamically with have</span> val book = Book("A Tale of Two Cities", "Sally")</span> <span class="stExplain">// and a Symbol (the tick mark syntax). For a statically</span> book should have ('title(<span class="stQuotedString">"A Tale of Two Cities"</span>)) <span class="stExplain">// typed approach, use a <a href="http://www.artima.com/docs-scalatest-3.2.0-RC3/#org.scalatest.matchers.HavePropertyMatcher">HavePropertyMatchers</a>.</span> <span class="stReserved">val</span> defined = <span class="stQuotedString">'defined</span> <span class="stExplain">// One way to get rid of the tick mark for a dynamic</span> <span class="stType">Some</span>(<span class="stQuotedString">"hi"</span>) should be (defined) <span class="stExplain">// property check</span> <span class="stReserved">val</span> beDefined = be (<span class="stQuotedString">'defined</span>) <span class="stExplain">// One way to get rid of the tick mark and a pair of</span> <span class="stType">Some</span>(<span class="stQuotedString">"hi"</span>) should beDefined <span class="stExplain">// parentheses</span> <span class="stReserved">val</span> beWithinTolerance = be >= <span class="stLiteral">0</span> and be <= <span class="stLiteral">10</span> <span class="stExplain">// You can combine matchers with and, or, and not</span> <span class="stLiteral">8</span> should beWithinTolerance <span class="stImport">import matchers._</span> <span class="stExplain">// You can compose a matcher with a function that</span> <span class="stReserved">val</span> beOdd = <span class="stType">Matcher</span>((i: <span class="stType">Int</span>) => <span class="stExplain">// transforms the input type</span> <span class="stType">MatchResult</span>( i % <span class="stLiteral">2</span> != <span class="stLiteral">0</span>, i + <span class="stLiteral">" is not odd"</span>, i + <span class="stLiteral">" is odd"</span>)) <span class="stReserved">val</span> beOddAsInt = beOdd compose { (s: <span class="stType">String</span>) => s.toInt } <span class="stQuotedString">"3"</span> should beOddAsInt <span class="stImport">import java.io.File</span> <span class="stExplain">// You can also use matcher composition to create a new</span> <span class="stReserved">def</span> endWithExtension(ext: <span class="stType">String</span>) = <span class="stExplain">// matcher given a parameter</span> endWith(ext) compose { (f: <span class="stType">File</span>) => f.getPath } <span class="stReserved">new</span> <span class="stType">File</span>(<span class="stQuotedString">"output.txt"</span>) should endWithExtension("txt") <span class="stReserved">val</span> beOdd = <span class="stType">Matcher</span> { (left: <span class="stType">Int</span>) => <span class="stExplain">// You can use a factory method to define a custom matcher</span> <span class="stType">MatchResult</span>( <span class="stExplain">// (such factory methods also exist for <a href="http://www.artima.com/docs-scalatest-3.2.0-RC3/#org.scalatest.matchers.BeMatcher"><code>BeMatcher</code></a>,</span> left % <span class="stLiteral">2</span> == <span class="stLiteral">1</span>, <span class="stExplain">// <a href="http://www.artima.com/docs-scalatest-3.2.0-RC3/#org.scalatest.matchers.BePropertyMatcher">BePropertyMatcher</a>, and <a href="http://www.artima.com/docs-scalatest-3.2.0-RC3/#org.scalatest.matchers.HavePropertyMatcher">HavePropertyMatcher</a></span> left + <span class="stQuotedString">" was not odd"</span>, left + <span class="stQuotedString">" was odd"</span>) } <span class="stLiteral">3</span> should beOdd <span class="stReserved">val</span> beOdd = <span class="stReserved">new</span> <span class="stType">Matcher[Int]</span> { <span class="stExplain">// Or you can write a full-blown matcher.</span> <span class="stReserved">def</span> apply(left: <span class="stType">Int</span>) = <span class="stType">MatchResult</span>( left % <span class="stLiteral">2</span> == <span class="stLiteral">1</span>, left + <span class="stQuotedString">" was not odd"</span>, left + <span class="stQuotedString">" was odd"</span>) } <span class="stLiteral">3</span> should beOdd </pre> </td> </tr> <tr> <th class="ataglance" colspan="4"><a name="selenium_dsl"></a>Selenium DSL</th> </tr> <tr> <td class="ataglance" colspan="4"> <pre class="stTight"><span class="stImport">import org.scalatest._ import selenium._</span> <span class="stReserved">class</span> <span class="stType">BlogSpec</span> <span class="stReserved">extends</span> <span class="stType">FlatSpec</span> <span class="stReserved">with</span> <span class="stType">WebBrowser</span> <span class="stReserved">with</span> <span class="stType">HtmlUnit</span> { <span class="stReserved">val</span> host = <span class="stLiteral">"http://localhost:9000/"</span> <span class="stLiteral">"The blog app home page"</span> should <span class="stLiteral">"have the correct title"</span> in { go to (host + <span class="stLiteral">"index.html"</span>) pageTitle should be (<span class="stLiteral">"Awesome Blog"</span>) } } go to <span class="stLiteral">"http://www.scalatest.org"</span> <span class="stExplain">// Go to ScalaTest website</span> pageTitle should be (<span class="stLiteral">"ScalaTest"</span>) <span class="stExplain">// Check page title</span> click on <span class="stLiteral">"q"</span> <span class="stExplain">// Click on element which has attribute id or name = "q"</span> click on id(<span class="stLiteral">"q"</span>) <span class="stExplain">// Click on element which has attribute id = "q"</span> click on name(<span class="stLiteral">"name"</span>) <span class="stExplain">// Click on element which has attribute name = "q"</span> click on tagName(<span class="stLiteral">"input"</span>) <span class="stExplain">// Click on element which is an 'input' tag</span> click on className(<span class="stLiteral">"quickref"</span>) <span class="stExplain">// Click on element which has the CSS class 'quickref' tag</span> cssSelector(<span class="stLiteral">"a[id='aLink']"</span>).element.tagName should be (<span class="stLiteral">"a"</span>) <span class="stExplain">// Check a element's tag name, selected by using CSS</span> <span class="stExplain">// selector</span> linkText(<span class="stLiteral">"Click Me!"</span>).element.tagName should be (<span class="stLiteral">"a"</span>) <span class="stExplain">// Check a element's tag name, selected by using the link</span> <span class="stExplain">// text</span> partialLinkText(<span class="stLiteral">"Click"</span>).element.tagName should be (<span class="stLiteral">"a"</span>) <span class="stExplain">// Check a element's tag name, selected by using the</span> <span class="stExplain">// partial link text</span> enter(<span class="stLiteral">"Cheese!"</span>) <span class="stExplain">// Enter "Cheese!" into currently selected text element</span> submit() <span class="stExplain">// Submit form</span> textField(<span class="stLiteral">"q"</span>).value = "<span class="stLiteral">Cheese!"</span> <span class="stExplain">// Set text field (which has attribute id or name = "q")</span> <span class="stExplain">// to "Cheese!"</span> textField(<span class="stLiteral">"q"</span>).value should be (<span class="stLiteral">"Cheese!"</span>) <span class="stExplain">// Read and check a text field (which has attribute id or</span> <span class="stExplain">// name = "q") value</span> radioButtonGroup(<span class="stLiteral">"group1"</span>).value = <span class="stLiteral">"Option 2"</span> <span class="stExplain">// Set radio button group (which has group name = "group1")</span> <span class="lineComment">// or</span> <span class="stExplain">// to choose "Option 2"</span> radioButtonGroup(<span class="stLiteral">"group1"</span>).selection = <span class="stType">Some</span>(<span class="stLiteral">"Option 2"</span>) radioButtonGroup(<span class="stLiteral">"group1"</span>).value should be (<span class="stLiteral">"Option 2"</span>) <span class="stExplain">// Read and check radio button group (which has group</span> <span class="lineComment">// or</span> <span class="stExplain">// name = "group1") chosen value</span> radioButtonGroup(<span class="stLiteral">"group1"</span>).selection should be (<span class="stType">Some</span>(<span class="stLiteral">"Option 2"</span>)) click on radioButton(<span class="stLiteral">"opt1"</span>) <span class="stExplain">// Click on a radio button (which has attribute id or</span> <span class="stExplain">// name = "opt1")</span> radioButton(<span class="stLiteral">"opt1"</span>).isSelected should be (<span class="stLiteral">true</span>) <span class="stExplain">// Check if a radio button (which has attribute id or</span> <span class="stExplain">// name = "opt1") is selected</span> checkbox(<span class="stLiteral">"cbx1"</span>).select() <span class="stExplain">// Select a check box (which has attribute id or name =</span> <span class="stExplain">// "cbx1")</span> checkbox(<span class="stLiteral">"cbx1"</span>).clear() <span class="stExplain">// Clear a check box (which has attribute id or name =</span> <span class="stExplain">// "cbx1")</span> checkbox(<span class="stLiteral">"cbx1"</span>).isSelected should be (<span class="stLiteral">true</span>) <span class="stExplain">// Check if a check box (which has attribute id or name =</span> <span class="stExplain">// "cbx1") is selected</span> singleSel(<span class="stLiteral">"select1"</span>).value = <span class="stLiteral">"option2"</span> <span class="stExplain">// Set a single-selection dropdown list (which has</span> <span class="lineComment">// or</span> <span class="stExplain">// attribute id or name = "select1") to choose "option2"</span> singleSel(<span class="stLiteral">"select1"</span>).selection = <span class="stType">Some</span>(<span class="stLiteral">"option2"</span>) singleSel(<span class="stLiteral">"select1"</span>).clear() <span class="stExplain">// Clear the selection of a single-selection dropdown list</span> <span class="lineComment">// or</span> <span class="stExplain">// (which has attribute id or name = "select1")</span> singleSel(<span class="stLiteral">"select1"</span>).selection = <span class="stType">None</span> singleSel(<span class="stLiteral">"select1"</span>).value should be (<span class="stLiteral">"option2"</span>) <span class="stExplain">// Read and check currently selected value of a</span> <span class="lineComment">// or</span> <span class="stExplain">// single-selection dropdown list (which has attribute id</span> singleSel(<span class="stLiteral">"select1"</span>).selection should be (<span class="stType">Some</span>(<span class="stLiteral">"option2"</span>)) <span class="stExplain">// or name = "select1")</span> multiSel(<span class="stLiteral">"select2"</span>).values = <span class="stType">Seq</span>(<span class="stLiteral">"option5"</span>, <span class="stLiteral">"option6"</span>) <span class="stExplain">// Set the selection of a multi-selection list (which has</span> <span class="stExplain">// attribute id or name = "select2")</span> multiSel(<span class="stLiteral">"select2"</span>).values += <span class="stLiteral">"option3"</span> <span class="stExplain">// Select "option3" in addition to current selection of a</span> <span class="stExplain">// multi-selection list (which has attribute id or name =</span> <span class="stExplain">// "select2")</span> multiSel(<span class="stLiteral">"select2"</span>).clear(<span class="stLiteral">"option5"</span>) <span class="stExplain">// Clear "option5" from current selection of a</span> <span class="stExplain">// multi-selection list (which has attribute id or name =</span> <span class="stExplain">// "select2")</span> multiSel(<span class="stLiteral">"select2"</span>).clearAll() <span class="stExplain">// Clear all selection of a multi-selection list (which</span> <span class="stExplain">// has attribute id or name = "select2")</span> multiSel(<span class="stLiteral">"select2"</span>).values should have size <span class="stLiteral">2</span> <span class="stExplain">// Read and check currently selected values of a</span> multiSel(<span class="stLiteral">"select2"</span>).values(<span class="stLiteral">0</span>) should be (<span class="stLiteral">"option5"</span>) <span class="stExplain">// multi-selection list (which has attribute id or name =</span> multiSel(<span class="stLiteral">"select2"</span>).values(<span class="stLiteral">1</span>) should be (<span class="stLiteral">"option6"</span>) <span class="stExplain">// "select2")</span> goBack() <span class="stExplain">// Go back to previous page in history</span> goForward() <span class="stExplain">// Go forward to next page in history</span> reloadPage() <span class="stExplain">// Reload the current page</span> add cookie (<span class="stLiteral">"name1"</span>, <span class="stLiteral">"value1"</span>) <span class="stExplain">// Add new cookie with name = "name1" and value = "value1"</span> cookie(<span class="stLiteral">"name1"</span>).value should be (<span class="stLiteral">"value1"</span>) <span class="stExplain">// Read and check a cookie's value</span> delete cookie <span class="stLiteral">"name1"</span> <span class="stExplain">// Delete cookie "name1</span> delete all cookies <span class="stExplain">// Delete all cookies under the same domain.</span> capture to <span class="stLiteral">"MyScreenShot"</span> <span class="stExplain">// Capture the screen and save as "MyScreenShot.png"</span> setCaptureDir(<span class="stLiteral">"/home/your_name/screenshots"</span>) <span class="stExplain">// Set the directory to save captured pictures.</span> withScreenshot { <span class="stExplain">// Auto capture screen when something goes wrong (e.g.</span> assert(<span class="stLiteral">"Gold"</span> == <span class="stLiteral">"Silver"</span>, <span class="stLiteral">"Expected gold, but got silver"</span>) <span class="stExplain">// test failed)</span> } close() <span class="stExplain">// Close current browser window</span> quit() <span class="stExplain">// Close all windows and exit the driver</span> </pre> </td> </tr> <tr> <th class="ataglance" colspan="4"><a name="concurrent"></a>Concurrent Support</th> </tr> <tr> <td class="ataglance" colspan="4"> <pre class="stTight"><span class="stImport">import org.scalatest._ <span class="stExplain">// <a href="http://www.artima.com/docs-scalatest-3.2.0-RC3/index.html#org.scalatest.concurrent.Eventually">eventually</a> retries the block until it</span> import concurrent.Eventually._ <span class="stExplain">// no longer throws an exception, using a</span> import Matchers._</span> <span class="stExplain">// timeout and interval taken from an</span> <span class="stExplain">// implicit <a href="http://www.artima.com/docs-scalatest-3.2.0-RC3/index.html#org.scalatest.concurrent.AbstractPatienceConfiguration$PatienceConfig">PatienceConfig</a>. The default</span> <span class="stExplain">// <a href="http://www.artima.com/docs-scalatest-3.2.0-RC3/index.html#org.scalatest.concurrent.AbstractPatienceConfiguration$PatienceConfig">PatienceConfig</a> is 150 milliseconds</span> <span class="stExplain">// timeout and 15 milliseconds interval.</span> <span class="stExplain">// If you import IntegrationPatience,</span> <span class="stExplain">// you'll get a 15 second timeout and 150</span> <span class="stExplain">// milliseconds interval. If you want</span> <span class="stExplain">// something else, you can define your own</span> <span class="stExplain">// implicit <a href="http://www.artima.com/docs-scalatest-3.2.0-RC3/index.html#org.scalatest.concurrent.AbstractPatienceConfiguration$PatienceConfig">PatienceConfig</a> like the next</span> <span class="stExplain">// example.</span> <span class="stReserved">val</span> it = <span class="stType">Vector</span>(<span class="stLiteral">"sad"</span>, <span class="stLiteral">"happy"</span>).iterator eventually { it.next shouldBe <span class="stLiteral">"happy"</span> } <span class="stImport">import org.scalatest.time.SpanSugar._</span> <span class="stExplain">// Define custom implicit <a href="http://www.artima.com/docs-scalatest-3.2.0-RC3/index.html#org.scalatest.concurrent.AbstractPatienceConfiguration$PatienceConfig">PatienceConfig</a></span> <span class="stReserved">implicit</span> <span class="stReserved">val</span> patienceConfig = <span class="stType">PatienceConfig</span>(timeout = scaled(<span class="stLiteral">2</span> seconds), interval = scaled(<span class="stLiteral">5</span> millis)) <span class="stImport">import org.scalatest.time._</span> <span class="stExplain">// Use <a href="http://www.artima.com/docs-scalatest-3.2.0-RC3/index.html#org.scalatest.concurrent.Eventually">eventually</a> with explicit timeout</span> eventually (timeout(<span class="stType">Span</span>(<span class="stLiteral">5</span>, <span class="stType">Seconds</span>))) { <span class="stBlockComment">/*code omitted*/</span> } eventually (timeout(<span class="stLiteral">5</span> seconds), interval(<span class="stLiteral">5</span> millis)) { <span class="stBlockComment">/*code omitted*/</span> } <span class="stExplain">// Use <a href="http://www.artima.com/docs-scalatest-3.2.0-RC3/index.html#org.scalatest.concurrent.Eventually">eventually</a> with explicit timeout</span> <span class="stExplain">// and interval</span> </pre> </td> </tr> <tr> <th class="ataglance" colspan="4"><a name="inspectors"></a>Inspectors</th> </tr> <tr> <td class="ataglance" colspan="4"> <pre class="stTight"><span class="stImport">import org.scalatest._ import Matchers._ import Inspectors._</span> forAll(<span class="stType">List</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>)) { _ should be > <span class="stLiteral">0</span> } <span class="stExplain">// Check that every element passes the assertion block</span> forAtLeast(<span class="stLiteral">1</span>, <span class="stType">List</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>)) { _ should be > <span class="stLiteral">2</span> } <span class="stExplain">// Check that at least the specified number of elements</span> <span class="stExplain">// pass the assertion block</span> forAtMost(<span class="stLiteral">2</span>, <span class="stType">List</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>)) { _ should be > <span class="stLiteral">1</span> } <span class="stExplain">// Check that at most the specified number of elements pass</span> <span class="stExplain">// the assertion block</span> forBetween(<span class="stLiteral">2</span>, <span class="stLiteral">4</span>, <span class="stType">List</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>, <span class="stLiteral">4</span>, <span class="stLiteral">5</span>)) { _ should be > <span class="stLiteral">2</span> } <span class="stExplain">// Check that the specified minimum and maximum number of</span> <span class="stExplain">// elements (inclusive) pass the assertion block</span> forEvery(<span class="stType">List</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>)) { _ should be > <span class="stLiteral">0</span> } <span class="stExplain">// Check that every element passes the assertion block,</span> <span class="stExplain">// listing all failing elements on failure (whereas forAll</span> <span class="stExplain">// just reports the first failing element)</span> forExactly(<span class="stLiteral">2</span>, <span class="stType">List</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>)) { _ should be > <span class="stLiteral">1</span> } <span class="stExplain">// Check that the exact specified number of elements pass</span> <span class="stExplain">// the assertion block</span> </pre> </tr> <tr> <th class="ataglance" colspan="4"><a name="lone_element"></a>Single-element collections</th> </tr> <tr> <td class="ataglance" colspan="4"> <pre class="stTight"> <span class="stImport">import org.scalatest._ <span class="stExplain">// Use <code>loneElement</code> to check that the <code>Set</code> (or any</span> import Matchers._ <span class="stExplain">// <code>GenTraversable</code>) contains single element, and that</span> import LoneElement._</span> <span class="stExplain">// element is greater than or equal to <code>10</code>. If the</span> <span class="stExplain">// <code>GenTraversable</code> does not contain single element,</span> <span class="stType">Set</span>(<span class="stLiteral">18</span>).loneElement should be >= <span class="stReserved">10</span> <span class="stExplain">// <code>TestFailedException</code> will be thrown.</span> </pre> </tr> <tr> <th class="ataglance" colspan="4"><a name="inside"></a>Inside</th> </tr> <tr> <td class="ataglance" colspan="4"> <pre class="stTight"><span class="stImport">import org.scalatest._ import Matchers._ import Inside._</span> <span class="stExplain">// Checking nested object graph using <a href="http://www.artima.com/docs-scalatest-3.2.0-RC3/index.html#org.scalatest.Inside">Inside</a></span> <span class="stReserved">case</span> <span class="stReserved">class</span> <span class="stType">Name</span>(first: <span class="stType">String</span>, middle: <span class="stType">String</span>, last: <span class="stType">String</span>) <span class="stReserved">case</span> <span class="stReserved">class</span> <span class="stType">Record</span>(name: <span class="stType">Name</span>, age: <span class="stType">Int</span>) <span class="stReserved">val</span> rec = <span class="stType">Record</span>( <span class="stType">Name</span>(<span class="stLiteral">"Sally"</span>, <span class="stLiteral">"Anna"</span>, <span class="stLiteral">"Jones"</span>), <span class="stLiteral">38</span> ) inside (rec) { case <span class="stType">Record</span>(name, age) => inside (name) { case <span class="stType">Name</span>(first, middle, last) => first should be (<span class="stLiteral">"Sally"</span>) } } </pre> </tr> <tr> <th class="ataglance" colspan="4"><a name="option_values"></a>OptionValues</th> </tr> <tr> <td class="ataglance" colspan="4"> <pre class="stTight"><span class="stImport">import org.scalatest._ import OptionValues._</span> <span class="stReserved">val</span> anOption = <span class="stType">Some</span>(<span class="stLiteral">18</span>) <span class="stExplain">// Use <a href="http://www.artima.com/docs-scalatest-3.2.0-RC3/index.html#org.scalatest.OptionValues">OptionValues</a> to check Option's value, using assert</span> assert(anOption.value > <span class="stLiteral">9</span>) <span class="stImport">import Matchers._</span> <span class="stReserved">val</span> anOption = <span class="stType">Some</span>(<span class="stLiteral">18</span>) <span class="stExplain">// Use <a href="http://www.artima.com/docs-scalatest-3.2.0-RC3/index.html#org.scalatest.OptionValues">OptionValues</a> to check Option's value, using Matchers syntax</span> anOption.value should be > <span class="stLiteral">9</span> </pre> </td> </tr> <tr> <th class="ataglance" colspan="4"><a name="either_values"></a>EitherValues</th> </tr> <tr> <td class="ataglance" colspan="4"> <pre class="stTight"> <span class="stImport">import org.scalatest._ import EitherValues._</span> <span class="stReserved">val</span> either1: <span class="stType">Either</span>[<span class="stType">String</span>, <span class="stType">Int</span>] = <span class="stType">Right</span>(<span class="stLiteral">16</span>) <span class="stExplain">// Use <a href="http://www.artima.com/docs-scalatest-3.2.0-RC3/index.html#org.scalatest.EitherValues">EitherValues</a> to check left and right value</span> assert(either1.right.value > <span class="stLiteral">9</span>) <span class="stExplain">// of an Either, using assert</span> <span class="stReserved">val</span> either2: <span class="stType">Either</span>[<span class="stType">String</span>, <span class="stType">Int</span>] = <span class="stType">Left</span>(<span class="stLiteral">"Muchas problemas"</span>) assert(either2.left.value == <span class="stLiteral">"Muchas problemas"</span>) <span class="stImport">import Matchers._</span> <span class="stReserved">val</span> either1: <span class="stType">Either</span>[<span class="stType">String</span>, <span class="stType">Int</span>] = <span class="stType">Right</span>(<span class="stLiteral">16</span>) <span class="stExplain">// Use <a href="http://www.artima.com/docs-scalatest-3.2.0-RC3/index.html#org.scalatest.EitherValues">EitherValues</a> to check left and right value</span> either1.right.value should be > <span class="stLiteral">9</span> <span class="stExplain">// of an Either, using Matchers syntax</span> <span class="stReserved">val</span> either2: <span class="stType">Either</span>[<span class="stType">String</span>, <span class="stType">Int</span>] = <span class="stType">Left</span>(<span class="stLiteral">"Muchas problemas"</span>) either2.left.value should be (<span class="stLiteral">"Muchas problemas"</span>) </pre> </tr> <tr> <th class="ataglance" colspan="4"><a name="partial_function_values"></a>PartialFunctionValues</th> </tr> <tr> <td class="ataglance" colspan="4"> <pre class="stTight"> <span class="stImport">import org.scalatest._ import PartialFunctionValues._</span> <span class="stReserved">val</span> map = <span class="stType">Map</span>(<span class="stLiteral">"one"</span> -> <span class="stLiteral">1</span>, <span class="stLiteral">"two"</span> -> <span class="stLiteral">2</span>, <span class="stLiteral">"three"</span> -> <span class="stLiteral">3</span>) <span class="stExplain">// Use <a href="http://www.artima.com/docs-scalatest-3.2.0-RC3/index.html#org.scalatest.PartialFunctionValues">PartialFunctionValues</a> to check value of a</span> assert(map.valueAt(<span class="stLiteral">"two"</span>) == <span class="stLiteral">2</span>) <span class="stExplain">// PartialFunction, using assert</span> <span class="stImport">import Matchers._</span> <span class="stReserved">val</span> map = <span class="stType">Map</span>(<span class="stLiteral">"one"</span> -> <span class="stLiteral">1</span>, <span class="stLiteral">"two"</span> -> <span class="stLiteral">2</span>, <span class="stLiteral">"three"</span> -> <span class="stLiteral">3</span>) <span class="stExplain">// Use <a href="http://www.artima.com/docs-scalatest-3.2.0-RC3/index.html#org.scalatest.PartialFunctionValues">PartialFunctionValues</a> to check value of a</span> map.valueAt(<span class="stLiteral">"two"</span>) should equal (<span class="stLiteral">2</span>) <span class="stExplain">// PartialFunction, using Matchers syntax</span> </pre> </tr> <tr> <th class="ataglance" colspan="4"><a name="private_method_tester"></a>PrivateMethodTester</th> </tr> <tr> <td class="ataglance" colspan="4"> <pre class="stTight"> <span class="stImport">import org.scalatest.PrivateMethodTester._</span> <span class="stReserved">class</span> <span class="stType">TaxCalculator</span> { <span class="stExplain">// Use <a href="http://www.artima.com/docs-scalatest-3.2.0-RC3/index.html#org.scalatest.PrivateMethodTester">PrivateMethodTester</a> to invoke a private</span> <span class="stReserved">private</span> <span class="stReserved">def</span> calc(amount: <span class="stType">Double</span>, percentage: <span class="stType">Double</span>): <span class="stType">Double</span> = <span class="stExplain">// method for testing purpose</span> amount * percentage / 100.00 <span class="stReserved">def</span> calculateTax(amount: <span class="stType">Double</span>): <span class="stType">Double</span> = calc(amount, <span class="stLiteral">5.00</span>) } <span class="stReserved">val</span> calculator = <span class="stReserved">new</span> <span class="stType">TaxCalculator</span> <span class="stReserved">val</span> calcMethod = <span class="stType">PrivateMethod</span>[<span class="stType">Double</span>](<span class="stQuotedString">'calc</span>) calculator invokePrivate calcMethod(<span class="stLiteral">1000.00</span>, <span class="stLiteral">8.88</span>)</pre> </td> <td class="ataglance" colspan="2"></td> </tr> </table> <br /> <div id="quickSections"> <h2>Sections</h2> <ul> <li><a href="#style">Style</a></li> <li><a href="#do_not_discover">Do Not Discover</a></li> <li><a href="#test">Test</a></li> <li><a href="#fixture">Fixture</a></li> <li>Matchers <ul> <li><a href="#matchers_equality_identity">Equality and identity</a></li> <li><a href="#object_class">Object's class</a></li> <li><a href="#matchers_expected_exceptions">Expected exceptions</a></li> <li><a href="#matchers_size_length">Length and size</a></li> <li><a href="#matchers_string_regex">Strings and regular expressions</a></li> <li><a href="#matchers_order_ranges">Order and ranges</a></li> <li><a href="#matchers_boolean_be">Checking boolean properties with <code>be</code></a></li> <li><a href="#matchers_be_matchers">Using arbitrary <code>be</code> matchers</a></li> <li><a href="#matchers_have_prop">Checking arbitrary properties with <code>have</code></a></li> <li><a href="#matchers_scala_java_col">Working with Scala and Java collections</a></li> <li><a href="#matchers_option_either">Working with <code>Option</code> and <code>Either</code></a></li> <li><a href="#matchers_emptiness">Checking for emptiness</a></li> <li><a href="#matchers_containers">Working with <code>"containers"</code></a></li> <li><a href="#matchers_aggregations">Working with <code>"aggregations"</code></a></li> <li><a href="#matchers_sequences">Working with <code>"sequences"</code></a></li> <li><a href="#matchers_iterators">Working with <code>iterators</code></a></li> <li><a href="#matchers_file">Working with <code>File</code></a></li> <li><a href="#matchers_inspector_shorthands">Inspector Shorthands</a></li> <li><a href="#matchers_and_or_not">Logical expressions with <code>and</code>, <code>or</code>, and <code>not</code></a></li> <li><a href="#matchers_diff_matcher">When you need a different matcher</a></li> </ul> </li> <li><a href="#selenium_dsl">Selenium DSL</a></li> <li><a href="#concurrent">Concurrent Support</a></li> <li><a href="#inspectors">Inspectors</a></li> <li><a href="#lone_element">Single-element collections</a></li> <li><a href="#inside">Inside</a></li> <li><a href="#option_values">OptionValues</a></li> <li><a href="#either_values">EitherValues</a></li> <li><a href="#partial_function_values">PartialFunctionValues</a></li> <li><a href="#private_method_tester">PrivateMethodTester</a></li> </ul> </div> </div> </div> <!-- body --> <div style="font-size: 66%; margin-top: 60px"> <p> ScalaTest is brought to you by Bill Venners and Artima.<br /> ScalaTest is free, open-source software released under the <a href="http://www.apache.org/licenses/LICENSE-2.0.html">Apache 2.0 license</a>. </p> <p> If your company loves ScalaTest, please consider <a href="/sponsor">sponsoring the project</a>. </p> <p> Copyright © 2009-2025 Artima, Inc. All Rights Reserved. </p> <p> <a href="http://www.artima.com" class="no_link_hover"> <img src="/assets/images/artima100Black.png" style="margin-top: 2px" width="100" height="38" alt="artima"/> </a> </p> </div> <!-- style="..." --> </div> <!-- Container --> </body> </html>