If you ever need to intentionally throw and catch an exception with ScalaTest, here’s an example of how to do that:
I’ve used Jenkins before, but hadn’t used it in a while, so when I got it running with Scala, SBT, ScalaTest, and Git, I made some notes about how to configure it. You can get Jenkins going with Docker, but I just got Jenkins running by starting its WAR file like this:
java -jar jenkins.war
Jenkins with Scala, SBT, ScalaTest, and Git
My notes on getting everything up and running are a little cryptic, but if you have a little experience with Jenkins I hope they’ll make sense. Here they are:
January 8, 2018: I just added lessons on SBT, ScalaTest, and a brief introduction to FP to the “Hello, Scala” website.
“But I was trying to think why I don’t use QuickCheck — which is a very nice tool — more. I think it’s because the situations that cause me trouble are ones that I would find it difficult to generate test data for.”
In this lesson I’ll share a non-trivial example of how I used ScalaCheck to test a function I wrote recently.
Once all of your functions work like algebraic equations, when you then look at an individual function it’s a simple step to wonder:
libraryDependencies += "org.scalacheck" %% "scalacheck" % "1.13.4" % "test"
it’s only available in the SBT “test” scope. This means that when you start a Scala REPL session inside of SBT with its
console command, the ScalaCheck library won’t be available in that scope.
To use ScalaCheck with the SBT console (REPL), don’t use its
console command — use
test:console instead. A complete example looks like this:
$ sbt > test:console scala> import org.scalacheck.Gen.choose
Note that after you type
test:console your project may be compiled, so that step may take a few moments.
In summary, use SBT’s
console command to start a “normal” Scala REPL inside SBT, and use
test:console to start a REPL that you can run tests inside of. (Note that this same advice also applies to using ScalaTest or specs2.)
Table of Contents
- Getting started
- First steps
- Adding Given/When/Then behavior (and ‘And’)
- More on Given, When, Then, and And
- Add more tests within ‘describe’
- Testing Option/Some/None in a BDD test
- Nesting describe blocks
- Using ‘before’ and ‘after’
- Mark tests as pending
- Temporarily disabling tests
- Testing expected exceptions
- Using matchers
- Tagging your BDD tests
- More information
This page is very much a work in progress, but it currently shows a small collection of ScalaTest BDD examples. I’ll keep adding more BDD examples as time goes on. Also, in this article I assume that you are familiar/comfortable with Scala and SBT, and are at least slightly familiar with using ScalaTest.
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 18.3, “How to run tests with SBT and ScalaTest.”
You want to set up an SBT project with ScalaTest, and run the tests with SBT.
Create a new SBT project directory structure as shown in Recipe 18.1, and then add the ScalaTest library dependency to your build.sbt file, as shown here:
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.7, “How to use a String like it's a file in Scala.”
Typically for the purposes of testing, you want to pretend that a
String is a file.