I have a file : ./src/main/scala/alain/main.scala :
package alain
import scala.Array
import scala.annotation.newMain
import fansi.Str
import fansi.Color.*
object myFunctions:
def mySum(x:Int,y:Int):Int ={
return x+y
}
object MyTest:
private class sumTest:
def testMySum():Unit=
if myFunctions.mySum(1, 2) == 5 then
println("OK")
else
println("Error")
@main
private def main(args: String*): Int = {
val fansiStr: fansi.Str = fansi.Color.Red("Hello World")
println(fansiStr)
(new sumTest).testMySum()
0
}
But i have totally no clue how to do unit testing.
Which libraries do i use ?
What do i put in build.sbt ?
Where do i put my code , in a separate file in ./src/test/ or not ?
What do i import there ?
Which sbt command do i run to test ?
As is typical for Scala, there are many potential test libraries you could use. But the default is generally ScalaTest, which is extremely thorough and pretty well-documented.
It remains very unclear to me, following questions remained unanswered,
What do i put in build.sbt ?
Where do i put my code , in a separate file in ./src/test/ or not ?
What do i import there ?
Which sbt command do i run to test ?
sbt new scala/scalatest-example.g8 will use the template to make a sample project with a build.sbt that adds the ScalaTest dependency and example tests in src/test, including imports.
You might also consider using scala-cli instead of sbt; the learning curve is less steep. sbt really shines for large, sophisticated builds. Most smaller projects can get by just fine with scala-cli.
This seemed to be very helpfull. Because i have a working example to start with.
I found out i didn’t needed any imports.
Just a class in test directory which extends org.scalatest.funsuite.AnyFunSuite.
The command to run was “sbt test”
Easy as a piece of cake. But without the example i would never figured it out of the documentation.