How do I run a funsuite test within a for loop?

Viewed 34

I am very new to scala so trying to understand how funsuite testing works. I have a bunch of tests that take a method data() to set it as a particular method from a variety of methods namely method1(): Long, method2(): Long etc

I figured defining the method1/2/... etc within the class would be fine and then sequentially go through them for my tests rather than having to write duplicate tests for each method, but seems like the data cannot be found by the tests. I also tried putting both the tests inside the for loop but then I am having issues with the IDE not being able to find tests. What am I doing wrong?

Example

class controlTest extends Anyfunsuite {
   protected def method1(): Long = { a() }
   protected def method2(): Long = { b() }
   protected def method3(): Long = { c() }

  for(i <- Seq(method1(), method2(), method3()) {
     def data(): Long = i
  }

  test("testing1"){
    data()
    ....
  }

 test("testing2"){
   data()
   ...
 }
1 Answers

You can call all your methods from within another method and use a tuple to store all results at once:

class ControlTest extends AnyFunSuite {

  def data(): (Long, Long, Long) = (a(), b(), c())

  test("testing1") {
    val res = data()
    // assert result
  }

  test("testing2") {
    val res = data()
    // assert result
  }
}

Using a def will actually trigger data's evaluation each time it's called (in each test). If they result in the same values every time, you might want to avoid that, and use a val instead, to evaluate it only once:

  val data: (Long, Long, Long) = (d(), b(), c())

Note that testing multiple methods in each test method is not really unit testing as per se. "Unit" is defined as the the smallest testable part of an application, that should be tested individually and independently.

Related