Skip to content

Commit 2f71229

Browse files
Merge pull request #514 from yadavan88/utest
Added sample code for uTest
2 parents 2c06bca + 35b0846 commit 2f71229

File tree

8 files changed

+127
-0
lines changed

8 files changed

+127
-0
lines changed

build.sbt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,13 @@ lazy val scala_libraries_3 = (project in file("scala-libraries-3"))
284284
libraryDependencies += "com.lihaoyi" %% "os-lib" % "0.8.1"
285285
)
286286

287+
lazy val scala_libraries_4 = (project in file("scala-libraries-4"))
288+
.settings(
289+
name := "scala-libraries-4",
290+
libraryDependencies += "com.lihaoyi" %% "utest" % "0.8.1" % "test",
291+
testFrameworks += new TestFramework("utest.runner.Framework")
292+
)
293+
287294
lazy val scala_strings = (project in file("scala-strings"))
288295
.settings(
289296
name := "scala-strings",
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.scala.utest
2+
3+
import utest._
4+
5+
import scala.concurrent.Future
6+
import scala.concurrent.ExecutionContext.Implicits.global
7+
8+
object AsyncTest extends TestSuite {
9+
def getFromDB(): Future[Int] = Future { 42 }
10+
11+
override def tests: Tests = Tests {
12+
test("get something from database") {
13+
getFromDB().map(v => assert(v == 42))
14+
}
15+
}
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.scala.utest
2+
3+
import utest._
4+
5+
object BeforeAfterTest extends TestSuite {
6+
println("This is executed before the tests")
7+
override def utestAfterAll() = {
8+
println("This method will be executed after all the tests")
9+
}
10+
override def tests: Tests = Tests {
11+
test("simple test") {
12+
assert(1 == 1)
13+
}
14+
test("simple test 2") {
15+
assert(2 == 2)
16+
}
17+
}
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.scala.utest
2+
3+
import utest._
4+
5+
object ExceptionHandling extends TestSuite {
6+
override def tests: Tests = Tests {
7+
def funnyMethod: String = throw new RuntimeException("Uh oh...")
8+
test("Handle an exception") {
9+
val ex = intercept[RuntimeException] {
10+
funnyMethod
11+
}
12+
assert(ex.getMessage == "Uh oh...")
13+
}
14+
}
15+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.scala.utest
2+
3+
import utest._
4+
5+
object NestedTest extends TestSuite {
6+
override def tests: Tests = Tests {
7+
test("outer test") - {
8+
val list = List(1, 2)
9+
println("This is an outer level of the test.")
10+
test("inner test 1") - {
11+
val list2 = List(10, 20)
12+
list.zip(list2) ==> List((1, 10), (2, 20))
13+
}
14+
test("inner test 2") - {
15+
val str = List("a", "b")
16+
list.zip(str) ==> List((1, "a"), (2, "b"))
17+
}
18+
}
19+
test("outer test 2") - {
20+
println("there is no nesting level here")
21+
assert(true)
22+
}
23+
}
24+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.scala.utest
2+
3+
import utest._
4+
5+
import scala.util.Random
6+
7+
object RetryTest extends TestSuite {
8+
override def tests: Tests = Tests {
9+
def flakyMethod: Int =
10+
Random.nextInt(2) // change this value to test failure
11+
test("retry flaky test") - retry(3) {
12+
val value = flakyMethod
13+
assert(value < 2)
14+
}
15+
}
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.scala.utest
2+
3+
import utest._
4+
5+
object RetryTestSuite extends TestSuite with TestSuite.Retries {
6+
override def utestRetryCount: Int = 3
7+
override def tests: Tests = Tests {
8+
test("retryable test 1") {
9+
assert(true)
10+
}
11+
test("retryable test 2") {
12+
assert(true)
13+
}
14+
}
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.scala.utest
2+
3+
import utest._
4+
5+
object SimpleUTest extends TestSuite {
6+
override def tests: Tests = Tests {
7+
test("str") {
8+
val name = "Baeldung"
9+
assert(name.length == 8)
10+
}
11+
test("arrow assert") {
12+
val name = "Baeldung"
13+
name.length ==> 8
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)