SimpleTest, a simple “testing framework” for Scala

For the purposes of writing the Scala Cookbook (2nd Edition), I thought I wanted a little “testing framework.” At first when I tried to use existing frameworks, I found that they didn’t always work with the latest release of Scala 3.0 (formerly named Dotty). Then, after working with the libraries that were often kept up to date with Scala 3.0, I further decided that I just wanted to put my tests inside Scala @main methods — I didn’t want to have some code under src/main and other code under src/test in my Scala/sbt projects.

So, in the end, I created my own testing “framework,” which I named SimpleTest. It’s really not a framework, it’s just a very little library that lets me run tests in @main methods.

What it looks like

SimpleTest is just about 100 lines of code right now, so there isn’t too much to say about it. Just use it in an @main method like this:

import com.alvinalexander.simpletest.SimpleTest._

@main def tests =

    // use `True` to assert that an expression is true
    True(1 == 1,  "1 == 1 (expecting green)")
    True(1 == 2,  "1 == 2 (expecting red)")

    // use `False` to assert that an expression is false
    False(1 == 2, "1 == 2 (expecting green)")
    False(1 == 1, "1 == 1 (expecting red)")

    // use `Equals` to assert that two objects are `==`
    Equals(1, 1,  "i expect green")
    Equals(1, 2,  "i expect red here")

    // in case you want to note tests that you intend to run
    Todo("don’t forget to test 2 == 3")

    // just run some tests and simpletest will keep track of
    // the test numbers for you
    True(1 == 1)
    False(1 == 2)
    Equals(1, 1)

Then when you run those tests you’ll see output that looks like this:

Scala SimpleTest output

About the capitalized names

In regard to the capitalized names for the tests, I tried quite a few different naming approaches, including these:

  • _true
  • _True
  • true_?
  • isTrue
  • assertTrue

In the end I thought the capitalized names were easier to read in the VS Code IDE, as you can see here:

Scala SimpleTest test names

So I went with the names True, False, Equals, and Todo.

The Github repository

If you’re interested in learning more, as I mentioned, the code is only about 100 lines long (excluding comments). You can find the source code repository for this project here:

Currently all of the code is in this file:

  • com.alvinalexander.simpletest.SimpleTest.scala

Ironically there aren’t any tests to go with SimpleTest, but you can find an @main method in a file named Tests.scala that show how this project works.

Please note that this is only at Version 0.3 — more like 0.0.3 — so everything about it is subject to change.


As a brief acknowledgment, this blog post about Minitest by Alexandru Nedelcu inspired some of my approach. The section titled “All the asserts that you need” is specifically what inspired me, even though I didn’t follow exactly what he wrote there. (As I noted, everything is subject to change.)