Reputation: 49
I am trying to get into Scala / sbt to test things. I am just using notepad++ and command line, no IDE.
Is there a "hello world" for tests program? I was following the sbt website example for hello world which is this:
object Hi {
def main(args: Array[String]) = println("Hi!")
}
Does anyone have a simple example test than I can run using SBT? I have my directories setup correctly but am still getting the hang of using SBT for basic tests and a simple example would help greatly.
Thanks.
EDIT:
I had tried using the intellij idea IDE at first with scala / sbt and scalatest to try a test example.
This is saved in the main scala directory.
class Hello {
def sayHello(name: String) = s"Hello, $name!"
}
This is saved in the test scala directory.
import org.scalatest.FunSuite
class HelloTest extends FunSuite {
test("sayHelloMethodWorks") {
val hello = new Hello
assert(hello.sayHello("Scala") == "Hello, Scala!")
}
}
This test runs fine in the IDE and it shows up as green. How would I be able to run that same test with just using command prompt / a text editor?
Upvotes: 1
Views: 178
Reputation: 751
I recommend Typesafe activator. There are many templates to help get you started
run activator new
choose minimal-scal
It creates a file build.sbt
name := """hello-world-app"""
version := "1.0"
scalaVersion := "2.11.6"
// Change this to another test framework if you prefer
libraryDependencies += "org.scalatest" %% "scalatest" % "2.2.4" % "test"
// Uncomment to use Akka
//libraryDependencies += "com.typesafe.akka" %% "akka-actor" % "2.3.11"
and a scala file
package com.example
object Hello {
def main(args: Array[String]): Unit = {
println("Hello, world!")
}
}
and a test HelloWorldSpec.scala
import org.scalatest._
class HelloSpec extends FlatSpec with Matchers {
"Hello" should "have tests" in {
true should === (true)
}
}
you can run sbt "~test" to continuous run your tests, or
sbt test
to run test once
Upvotes: 1
Reputation: 11518
Welcome to the world of Scala and SBT! Excellent choice. :)
If you save that "hello world" example as hw.scala
then in the same directory you can just run sbt run
and it will automatically search for this source and run it.
> sbt run
[info] Set current project to hw (in build file:/Users/ben/projects/hw/)
[info] Updating {file:/Users/ben/projects/hw/}hw...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Compiling 1 Scala source to /Users/ben/projects/hw/target/scala-2.10/classes...
[info] Running Hi
Hi!
[success] Total time: 3 s, completed 04-Jun-2015 21:17:27
>
See this bit in the documentation for more on this.
Upvotes: 0