Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 25, 2024 10:20
Show Gist options
  • Save dacr/bb7230144951cc0890101c4849d26430 to your computer and use it in GitHub Desktop.
Save dacr/bb7230144951cc0890101c4849d26430 to your computer and use it in GitHub Desktop.
scalatest basics cheat sheet / published by https://github.com/dacr/code-examples-manager #fd571624-7441-472d-868c-16c4b5fa817a/c795e46d5c013e064ec34b5f94ffa0ff3ded5d8
// summary : scalatest basics cheat sheet
// keywords : scala, scalatest, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : fd571624-7441-472d-868c-16c4b5fa817a
// created-on : 2020-05-31T21:54:52+02:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli test testing-scalatest.test.scala $file
// ---------------------
//> using scala "3.4.2"
//> using test.dep "org.scalatest::scalatest:3.2.16"
// ---------------------
import org.scalatest.*
import org.scalatest.concurrent.Eventually.*
import org.scalatest.flatspec.*
import org.scalatest.matchers.*
import OptionValues.*
class ThatSpec extends AnyFlatSpec with should.Matchers {
override def suiteName = "ThatSpec"
"numbers" should "be testable with less precisions" in {
Math.PI shouldBe 3.14d +- 0.01d
}
"options" should "be testable using some symbolic helpers" in {
Some("str") shouldBe defined
None should not be defined
None shouldBe empty
Some("blah") should not be empty
Some("str").value shouldBe "str"
}
"strings" should "be empty testable" in {
"" shouldBe empty
}
it should "be matchable using regular expression" in {
"tic tac toe" should include("tac")
"tic tac toe" should include regex "(?i)ta|uc"
"tic tac toe" should startWith regex "(?i)tic"
"tic tac toe" should endWith regex "(?i)toes?"
"tic tac toe" should fullyMatch regex "(?i)tic.*toe"
}
"exceptions" should "be intercept-able" in {
intercept[Exception] {
1 / 0
}
}
"lists" should "be empty" in {
List() shouldBe empty
List() should be(empty)
List() should have size 0
}
it should "be not empty" in {
List(1) should not be (empty)
List(1) should have size 1
}
it should "should be equal to an other empty list if empty" in {
List() should equal(List())
}
it should "have no head if empty" in {
List().headOption shouldBe empty
}
it should "be possible to check if a subset of items are included" in {
List(1) should contain only (1)
List(1, 2, 3, 4) should contain only (4, 3, 2, 1)
List(1, 2, 3, 4) should contain allOf (2, 3)
List(1, 2, 3, 4) should contain atLeastOneOf (4, 11)
List(1, 2, 3, 4) should contain atMostOneOf (4, 11)
List(1, 2, 3, 4) should contain theSameElementsAs Vector(4, 3, 2, 1)
List(1, 2, 3, 4) should contain inOrderOnly (1, 2, 3, 4)
List(1, 2, 3, 4) should contain inOrder (1, 4)
List(1, 2, 3, 4) shouldBe sorted
}
"stdout" should "be testable" in {
import java.io.ByteArrayOutputStream
def toto(input: String): Unit = println(input)
val out = new ByteArrayOutputStream()
Console.withOut(out) {
toto("truc")
}
out.toString.trim shouldBe "truc"
}
"eventually" should "provide an easy way to retry a failing tests" in {
val xs = 1 to 3
val it = xs.iterator
eventually { it.next() shouldBe 3 }
}
"test reports" should "be customizable with informative messages put in test" in {
info("a list dummy test")
List(42) should have size(1)
alert("take care")
List(42,42).distinct should have size(1)
note("some notes about this test")
}
"arrays" should "" in {
// TODO - testing arrays has pitfalls...
}
}
//org.scalatest.tools.Runner.main(Array("-oDF", "-s", classOf[ThatSpec].getName))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment