scalatest

Using Jenkins with Scala, ScalaTest, SBT, and Git alvin February 19, 2019 - 7:58pm

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:

I put my Scala String Utilities library on Github alvin February 12, 2019 - 10:56am

I put my Scala String Utilities library on Github a few days ago. It includes my Q String Interpolator, and several other string utility functions. It also demonstrates how to write ScalaTest and ScalaCheck tests with an SBT project.

ScalaCheck 2: A More-Complicated Example alvin December 19, 2017 - 4:55pm

“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.”

Simon Peyton Jones, in the book, Coders at Work

In this lesson I’ll share a non-trivial example of how I used ScalaCheck to test a function I wrote recently.

ScalaCheck 1: Introduction alvin December 19, 2017 - 4:51pm

Once all of your functions work like algebraic equations, when you then look at an individual function it’s a simple step to wonder:

How to use ScalaCheck in the SBT console

If you add ScalaCheck to an SBT project like this:

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.)

A collection of ScalaTest BDD examples using FunSpec

Table of Contents1 - Getting started2 - First steps3 - Adding Given/When/Then behavior (and ‘And’)4 - More on Given, When, Then, and And5 - Add more tests within ‘describe’6 - Testing Option/Some/None in a BDD test7 - Nesting describe blocks8 - Using ‘before’ and ‘after’9 - Mark tests as pending10 - Temporarily disabling tests11 - Testing expected exceptions12 - Assertions13 - Using matchers14 - Tagging your BDD tests15 - Other16 - 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.

How to run Scala project tests with SBT and 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.”

Problem

You want to set up an SBT project with ScalaTest, and run the tests with SBT.

Solution

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:

How to use a String like it’s a File in Scala (such as in testing)

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.”

Problem

Typically for the purposes of testing, you want to pretend that a String is a file.