ScalaTest: BeforeAll for Table Driven Property Check

Viewed 113

I have a scenario, where I need to run a set of tests with multiple datasets. I am using AnyFlatSpec with org.scalatest.prop.TableDrivenPropertyChecks.Table. All these tests need a data setup. Below is a snapshot of what I tried -

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.prop.TableDrivenPropertyChecks._
import org.scalatest.prop.TableFor3

class TryOutFlatSpec extends AnyFlatSpec {

  val useCases: TableFor3[String, String, Int] = Table(
    ("service", "endpoint", "value"),
    ("cart", "/create", 201),
    ("cart", "/get", 200),
  )
  forAll(useCases) {
    (service: String, endpoint: String, value: Int) => {
      println(s"call service $service$endpoint") //data setup happens here
      behavior of "cart service"
      s"$service $endpoint" should s"return a status code ${value}" in {
        println(s"status code ${value}")
      }
      s"$service $endpoint" should "return response body" in {
        println("""{"status": "success"}""")
      }
    }
  }
}

This gives an output as -

call service cart/create
call service cart/get
{"status": "success"}
status code 200
{"status": "success"}
status code 201

My desired execution order is -

call service cart/create
{"status": "success"}
status code 201

call service cart/get
{"status": "success"}
status code 200

Is there a way to combine BeforeAndAfterAll with Table Driven Property check to achieve my goal? Or is there any suggested alternative?

0 Answers
Related