Introduction
Welcome to your Specs2 Tutorial! This will give you an example of how to write Specs2 tests over your scala code, and how we use Specs2 at Banno.
What is Specs2?
Specs2 is a simple way to write some tests. If you’ve ever done testing before it’s often very simple. The idea is to make sure that you code does what it’s supposed to do and matches the tests.
Check out the Specs2 User Guide for more information.
Setting up your project
Take the appropriate steps to set up an SBT project. See the envionment setup document if you forgot how to structure an SBT project (src/main/scala and src/test/scala).
Make a new folder and paste the following into a build.sbt file
name := "specs2-tutorial"
libraryDependencies ++= Seq(
"org.specs2" %% "specs2-core" % "3.7.2" % "test"
)
Once you have your project set up, you may need to run the reload and update commands inside of sbt. This will tell sbt to “reload” the plugin definitions and to “update” the .jar files it uses.
Example Code
We will test the following basic functions with specs2. Put this code under src/main/scala/ in a file named BasicFunctions.scala
package com.specs2tutorial
trait BasicFunctions {
def sumUpList(ls: List[Int]): Int = {
ls.foldLeft(0)(_+_)
}
def filterList(ls: List[Int], greaterThan: Int): List[Int] = {
ls.filter(e => e > greaterThan)
}
def findGCD(n:Int,m:Int): Int =
if (m == 0) n
else findGCD(m, n % m)
}
And below are specs2 tests of those basic functions. Put this code under src/test/scala/ in a file named BasicFunctionsSpec.scala
package com.specs2tutorial
import org.specs2.mutable.Specification
object BasicFunctionsSpec extends Specification with BasicFunctions {
"when we send a list of Ints sumUpList" should {
"add them up" in {
val numbers:List[Int] = List(1,2,3,4,5)
// the triple equal sign is shorthand for checking if two values are the same
sumUpList(numbers) === 15
// This would also work
sumUpList(numbers) must beEqualTo(15)
// Or This
sumUpList(numbers) must be_==(15)
}
}
"when we send a list of Ints to filterList filterList" should {
"only contain the unfiltered Ints" in {
val numbers:List[Int] = List(1,2,3,4,5)
val filteredList = filterList(numbers,2)
//filteredList should not contain 1 or 2 so lets check that the size is right
filteredList.size must be_==(3)
//In this case we know what numbers filteredList must contain so lets check that they're there
filteredList must contain(3,4,5)
}
}
"when we send two integers findGCD" should {
"find the greatest common divisor" in {
val gcd = findGCD(45, 63)
//The gcd of 45 and 63 is 9 so lets see if thats what we got!
gcd === 9
}
}
}
How do I run Specs2 tests?
To actually run your specs2 tests, run the test command inside of sbt or just sbt test in the console (Remember you have to be in this project directory)
What can Specs2 check besides equality?
Lots of things! For example throwA[ExceptionType] can be used to check if your code throws the appropriate exception, and haveClass can be used to check the class of an object.
Most of the code you will be starting out with won’t be too complex, so this short list of Matchers should allow you to test everything that’s needed.
Practice
The best thing for you to do now is to create or use your own function(s) that do things that you can test. Be it multiplying the input by another, performing a map on a list, or whatever you want to do and test. That way you can get more comfortable with creating code and then testing it.
Have fun! Ask a mentor if you have questions!