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="Container"> <!-- 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> | <span><em>Quick Start</em></span> | <a href="/install">Install</a> | <a href="/user_guide">User Guide</a> | <a href="/at_a_glance">At A Glance</a> | <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 quick start</h1> <p> <!-- To get started with ScalaTest, copy this <a href="ATlatestScaladoc/org/scalatest/flatspec/AnyFlatSpec.html"><code>AnyFlatSpec</code></a> into a file --> To get started with ScalaTest, copy this <a href='/scaladoc/3.2.19/org/scalatest/flatspec/AnyFlatSpec.html'><code>AnyFlatSpec</code></a> into a file named <code>ExampleSpec.scala</code>: </p> <pre class="stHighlighted"> <span class="stReserved">import</span> collection.mutable.Stack <span class="stReserved">import</span> org.scalatest._ <span class="stReserved">import</span> flatspec._ <span class="stReserved">import</span> matchers._ <span class="stReserved">class</span> <span class="stType">ExampleSpec</span> <span class="stReserved">extends</span> <span class="stType">AnyFlatSpec</span> <span class="stReserved">with</span> <span class="stType">should.Matchers</span> { <span class="stQuotedString">"A Stack"</span> should <span class="stQuotedString">"pop values in last-in-first-out order"</span> in { <span class="stReserved">val</span> stack = <span class="stReserved">new</span> <span class="stType">Stack[Int]</span> stack.push(<span class="stLiteral">1</span>) stack.push(<span class="stLiteral">2</span>) stack.pop() should be (<span class="stLiteral">2</span>) stack.pop() should be (<span class="stLiteral">1</span>) } it should <span class="stQuotedString">"throw NoSuchElementException if an empty stack is popped"</span> in { <span class="stReserved">val</span> emptyStack = <span class="stReserved">new</span> <span class="stType">Stack[Int]</span> a [<span class="stType">NoSuchElementException</span>] should be thrownBy { emptyStack.pop() } } } </pre> <p> You can compile this <code>FlatSpec</code> (using <a href="https://oss.sonatype.org/content/groups/public/org/scalatest/scalatest-app_3/3.2.19/scalatest-app_3-3.2.19.jar">this Jar file</a>) like this: </p> <pre class="scala"> $ scalac -cp scalatest-app_3-3.2.19.jar ExampleSpec.scala </pre> <p> To run it, you will need one more artifact, <a href="https://repo1.maven.org/maven2/org/scala-lang/modules/scala-xml_3/2.3.0/scala-xml_3-2.3.0.jar">the Jar file for Scala's XML module</a>. Once you've downloaded that Jar file, you can run <code>ExampleSpec</code> like this: </p> <pre class="scala"> $ CLASSPATH=scalatest-app_3-3.2.19.jar:scala-xml_3-2.3.0.jar $ scala -cp $CLASSPATH org.scalatest.run ExampleSpec <span class="cyanincolor">Run starting. Expected test count is: 2</span> <span class="greenincolor">ExampleSpec: A Stack - should pop values in last-in-first-out order - should throw NoSuchElementException if an empty stack is popped</span> <span class="cyanincolor">Run completed in 76 milliseconds. Total number of tests run: 2 Suites: completed 1, aborted 0 Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0</span> <span class="greenincolor">All tests passed.</span> </pre> <p> Your tests passed! As a reward, take a moment to <a href="/install">install ScalaTest</a> in your project. </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> </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>