# Introduction to Test Cases ?

Where to write a test case

class LotteryDataTestSuite extends FunSuite with SmilePerTest {

  test("test1", Tag("t1"), Tag("tag")) {
    println("test1 write your tests here")
  }

  test("test2", Tag("t2"), Tag("tag")) {
    println("test2 write your tests here")
  }

}
// Runs all the testcases inside the PROJECT.
[Lottery] test
write your tests here
[info] LotteryDataTestSuite:
[info] - test1
[info] ScalaTest
[info] Run completed in 1 second, 368 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[info] Passed: Total 1, Failed 0, Errors 0, Passed 1
[success] Total time: 3 s, completed 4 Jul, 2019 3:47:10 PM

// Compiles all the testcases
[Lottery] $ test:compile
[info] Compiling 1 Scala source to /Users/pallavi/metastay/lottery/modules/lottery/LotteryData/target/scala-2.12/test-classes ...
[info] Done compiling.
[info] Compiling 1 Scala source to /Users/pallavi/metastay/lottery/modules/lottery/LotteryDomain/target/scala-2.12/test-classes ...
[info] Done compiling.
[success] Total time: 5 s, completed 4 Jul, 2019 4:07:36 PM


// To run all the testcases inside a MODULE
[Lottery] $ project LotteryData
[info] Set current project to LotteryData (in build file:/Users/pallavi/metastay/lottery/)
[LotteryData] testOnly
[info] Compiling 1 Scala source to /Users/pallavi/metastay/lottery/modules/lottery/LotteryData/target/scala-2.12/test-classes ...
[info] Done compiling.
LotteryDataTestSuite ==> write your tests here
[info] LotteryDataTestSuite:
[info] - test1
[info] ScalaTest
[info] Run completed in 950 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[info] Passed: Total 1, Failed 0, Errors 0, Passed 1
[success] Total time: 2 s, completed 4 Jul, 2019 3:47:34 PM

// To run all the testcases in the TestSuite file.
// New TestSuite file can be created within test directory
[LotteryData] testOnly *FooTestSuite
[info] Compiling 1 Scala source to /Users/pallavi/metastay/lottery/modules/lottery/LotteryData/target/scala-2.12/test-classes ...
[info] Done compiling.
In Foo
[info] FooTestSuite:
[info] - test1
[info] ScalaTest
[info] Run completed in 1 second, 91 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[info] Passed: Total 1, Failed 0, Errors 0, Passed 1
[success] Total time: 3 s, completed 4 Jul, 2019 3:48:13 PM

// To run only one testcase user "tag"
[LotteryData] testOnly *FooTestSuite -- -n t1
In Foo
[info] FooTestSuite:
[info] - test1
[info] ScalaTest
[info] Run completed in 917 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[info] Passed: Total 1, Failed 0, Errors 0, Passed 1
[success] Total time: 1 s, completed 4 Jul, 2019 3:48:37 PM

// Cannot have the test name duplicated in a TestSuite
// TestName must be unique
[LotteryData] testOnly *LotteryDataTestSuite
[info] Compiling 1 Scala source to /Users/pallavi/metastay/lottery/modules/lottery/LotteryData/target/scala-2.12/test-classes ...
[info] Done compiling.
[info] LotteryDataTestSuite:
[info] com.metastay.lottery.LotteryDataTestSuite *** ABORTED ***
[info]   Duplicate test name: test1 (LotteryDataTestSuite.scala:16)
[info] ScalaTest
[info] Run completed in 101 milliseconds.
[info] Total number of tests run: 0
[info] Suites: completed 0, aborted 1
[info] Tests: succeeded 0, failed 0, canceled 0, ignored 0, pending 0
[info] *** 1 SUITE ABORTED ***
[error] Error: Total 1, Failed 0, Errors 1, Passed 0
[error] Error during tests:
[error]     com.metastay.lottery.LotteryDataTestSuite
[error] (Test / testOnly) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 2 s, completed 4 Jul, 2019 3:49:16 PM

// Run group of testcases by TAG
[LotteryData] testOnly *LotteryDataTestSuite -- -n tag
[info] Compiling 1 Scala source to /Users/pallavi/metastay/lottery/modules/lottery/LotteryData/target/scala-2.12/test-classes ...
[info] Done compiling.
LotteryDataTestSuite ==>  test1 write your tests here
LotteryDataTestSuite ==>  test2 write your tests here
[info] LotteryDataTestSuite:
[info] - test1
[info] - test2
[info] ScalaTest
[info] Run completed in 992 milliseconds.
[info] Total number of tests run: 2
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[info] Passed: Total 2, Failed 0, Errors 0, Passed 2
[success] Total time: 3 s, completed 4 Jul, 2019 3:50:52 PM

//Can have more that one TAG for a testcase
[LotteryData] testOnly *LotteryDataTestSuite -- -n t1
[info] Compiling 1 Scala source to /Users/pallavi/metastay/lottery/modules/lottery/LotteryData/target/scala-2.12/test-classes ...
[info] Done compiling.
[info] LotteryDataTestSuite:
[info] - test1
[info] ScalaTest
[info] Run completed in 899 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[info] Passed: Total 1, Failed 0, Errors 0, Passed 1
[success] Total time: 2 s, completed 4 Jul, 2019 3:52:27 PM

# assert

use assert to check the desired output

class LotteryDataTestSuite extends FunSuite with SmilePerTest {

  def add(a: Int, b: Int): Int = a + b

  test("add pass", Tag("t1"), Tag("tag")) {
    val sum = add(2, 3)
    assert(sum == 5)
  }

  test("add not working", Tag("t2"), Tag("tag")) {
    val sum = add(2, 3)
    assert(sum != 6)
  }

}
[LotteryData] testOnly *LotteryDataTestSuite -- -n t2
[info] Compiling 1 Scala source to /Users/pallavi/metastay/lottery/modules/lottery/LotteryData/target/scala-2.12/test-classes ...
[info] Done compiling.
[info] LotteryDataTestSuite:
[info] - test2 *** FAILED ***
[info]   5 did not equal 6 (LotteryDataTestSuite.scala:21)
[info] ScalaTest
[info] Run completed in 908 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 0, failed 1, canceled 0, ignored 0, pending 0
[info] *** 1 TEST FAILED ***
[error] Failed: Total 1, Failed 1, Errors 0, Passed 0
[error] Failed tests:
[error]     com.metastay.lottery.LotteryDataTestSuite
[error] (Test / testOnly) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 2 s, completed 4 Jul, 2019 3:52:50 PM

[LotteryData] testOnly *LotteryDataTestSuite -- -n tag
[info] LotteryDataTestSuite:
[info] - test1
[info] - test2 *** FAILED ***
[info]   5 did not equal 6 (LotteryDataTestSuite.scala:21)
[info] ScalaTest
[info] Run completed in 968 milliseconds.
[info] Total number of tests run: 2
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 1, canceled 0, ignored 0, pending 0
[info] *** 1 TEST FAILED ***
[error] Failed: Total 2, Failed 1, Errors 0, Passed 1
[error] Failed tests:
[error]     com.metastay.lottery.LotteryDataTestSuite
[error] (Test / testOnly) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 1 s, completed 4 Jul, 2019 3:52:56 PM

[LotteryData] testOnly *LotteryDataTestSuite -- -n t2
[info] Compiling 1 Scala source to /Users/pallavi/metastay/lottery/modules/lottery/LotteryData/target/scala-2.12/test-classes ...
[info] Done compiling.
[info] LotteryDataTestSuite:
[info] - test2 *** FAILED ***
[info]   5 did not equal 6 add function failed (LotteryDataTestSuite.scala:21)
[info] ScalaTest
[info] Run completed in 931 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 0, failed 1, canceled 0, ignored 0, pending 0
[info] *** 1 TEST FAILED ***
[error] Failed: Total 1, Failed 1, Errors 0, Passed 0
[error] Failed tests:
[error]     com.metastay.lottery.LotteryDataTestSuite
[error] (Test / testOnly) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 2 s, completed 4 Jul, 2019 3:53:37 PM

# beforeEach afterEach

beforeEach is a common place to write some code which every testcases will call before running it.

afterEach is a common place to write some code which every testcases will call after running it.

class LotteryDomainLogicTestSuite extends FunSuite with SmilePerTest {

  override def beforeEach(testData: TestData): Unit = {
    super.beforeEach(testData)
    val lotteryName = "lotteryName"
    LotteryWriter().insert(LotteryRow(lotteryName = lotteryName))
  }

  override def afterEach(testData: TestData): Unit = {
    super.beforeEach(testData)
    val lotteryName = "lotteryName"
    val q = LotteryQuery().lotteryName.is(lotteryName)
    LotteryWriter().remove(q)
  }

  test("lotteryNameExists fails", Tag("lotteryNameExists1"), ntest) {
    val lotteryName = "random"
    val lotteryNameExists = grab[LotteryDomainLogic].lotteryNameExists(lotteryName)
    assert(lotteryNameExists == false, s"lottery name - $lotteryName seems to exist!")
  }

  test("lotteryNameExists", Tag("lotteryNameExists2"), ptest) {
    val lotteryName = "lotteryName"
    val lotteryNameExists = grab[LotteryDomainLogic].lotteryNameExists(lotteryName)
    assert(lotteryNameExists, s"lottery name - $lotteryName seems to exist!")
  }


}
[LotteryData] cd LotteryDomain
[info] Set current project to LotteryDomain (in build file:/Users/pallavi/metastay/lottery/)

[LotteryDomain] testOnly *LotteryDomainLogicTestSuite
[info] Compiling 2 Scala sources to /Users/pallavi/metastay/lottery/modules/lottery/LotteryDomain/target/scala-2.12/test-classes ...
[info] Done compiling.
[info] LotteryDomainLogicTestSuite:
[info] - lotteryNameExists
[info] ScalaTest
[info] Run completed in 1 second, 363 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[info] Passed: Total 1, Failed 0, Errors 0, Passed 1
[success] Total time: 4 s, completed 4 Jul, 2019 3:56:22 PM

[LotteryDomain] testOnly *LotteryDomainLogicTestSuite
[info] Compiling 1 Scala source to /Users/pallavi/metastay/lottery/modules/lottery/LotteryDomain/target/scala-2.12/test-classes ...
[info] Done compiling.
[info] LotteryDomainLogicTestSuite:
[info] - lotteryNameExists *** FAILED ***
[info]   lotteryNameExists was false lottery name - random seems to exist! (LotteryDomainLogicTestSuite.scala:12)
[info] ScalaTest
[info] Run completed in 1 second, 217 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 0, failed 1, canceled 0, ignored 0, pending 0
[info] *** 1 TEST FAILED ***
[error] Failed: Total 1, Failed 1, Errors 0, Passed 0
[error] Failed tests:
[error]     com.metastay.lotterydomain.domainlogic.LotteryDomainLogicTestSuite
[error] (Test / testOnly) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 2 s, completed 4 Jul, 2019 3:56:35 PM

[LotteryDomain] testOnly *LotteryDomainLogicTestSuite -- -n lotteryNameExists2
[info] Compiling 1 Scala source to /Users/pallavi/metastay/lottery/modules/lottery/LotteryDomain/target/scala-2.12/test-classes ...
[info] Done compiling.
[info] LotteryDomainLogicTestSuite:
[info] - lotteryNameExists
[info] ScalaTest
[info] Run completed in 1 second, 333 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[info] Passed: Total 1, Failed 0, Errors 0, Passed 1
[success] Total time: 3 s, completed 4 Jul, 2019 3:59:49 PM

[LotteryDomain] testOnly *LotteryDomainLogicTestSuite -- -n lotteryNameExists2
[info] Compiling 1 Scala source to /Users/pallavi/metastay/lottery/modules/lottery/LotteryDomain/target/scala-2.12/test-classes ...
[info] Done compiling.
[info] LotteryDomainLogicTestSuite:
[info] - lotteryNameExists
[info] ScalaTest
[info] Run completed in 1 second, 227 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[info] Passed: Total 1, Failed 0, Errors 0, Passed 1
[success] Total time: 3 s, completed 4 Jul, 2019 4:02:09 PM

[LotteryDomain] testOnly *LotteryDomainLogicTestSuite -- -n lotteryNameExists2
[info] Compiling 1 Scala source to /Users/pallavi/metastay/lottery/modules/lottery/LotteryDomain/target/scala-2.12/test-classes ...
[info] Done compiling.
[info] LotteryDomainLogicTestSuite:
[info] - lotteryNameExists
[info] ScalaTest
[info] Run completed in 1 second, 210 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[info] Passed: Total 1, Failed 0, Errors 0, Passed 1
[success] Total time: 3 s, completed 4 Jul, 2019 4:03:10 PM
[LotteryDomain] cd
[info] LotteryDomain (in build file:/Users/pallavi/metastay/lottery/)
[LotteryDomain] cd /
[info] Set current project to Lottery (in build file:/Users/pallavi/metastay/lottery/)