A scalatest helper for loading csv files as Table
.
scalatest-csv-table is currently available for Scala 2.12 and 2.13.
Add the following lines to your build.sbt
.
libraryDependencies += "io.github.akiomik" %% "scalatest-csv-table" % "1.2.2" % Test
NOTE: The groupid has been changed from com.github.akiomik
to io.github.akiomik
because the maven repository has been changed from bintray to sonatype.
scalatest-csv-table supports some different versions of scalatest.
scalatest-csv-table version | scalatest version | scala version |
---|---|---|
1.0.2 | 3.0.x | 2.11.x/2.12.x |
1.1.0 | 3.1.x | 2.12.x/2.13.x |
1.2.2 | 3.2.x | 2.12.x/2.13.x |
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.prop.TableDrivenPropertyChecks._
import com.github.akiomik.scalatest._
class FizzBuzzSpec extends AnyFlatSpec {
"A FizzBuzz" should "pass tests from a string" in {
val csv =
"""n,expected
|1,1
|2,2
|3,Fizz
|4,4
|5,Buzz""".stripMargin
val tests = CsvTable.fromString[Int, String](csv)
forAll (tests) { (n: Int, expected: String) =>
assert(FizzBuzz(n) == expected)
}
}
"A FizzBuzz" should "pass tests from a file" in {
val tests = CsvTable.fromFile[Int, String]("src/test/resources/fizzbuzz.csv")
forAll (tests) { (n: Int, expected: String) =>
assert(FizzBuzz(n) == expected)
}
}
"A FizzBuzz" should "pass tests from a resource file" in {
val tests = CsvTable.fromResource[Int, String]("fizzbuzz.csv") // from `src/test/resouces`
forAll (tests) { (n: Int, expected: String) =>
assert(FizzBuzz(n) == expected)
}
}
}
import com.github.akiomik.scalatest._
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.prop.TableDrivenPropertyChecks._
import kantan.csv._
case class Foo(i: Int, s: String, b: Boolean)
class FooSpec extends AnyFlatSpec {
implicit val decoder = RowDecoder.decoder(0, 1, 2)(Foo.apply _) //
"A Foo" should "pass tests from a string" in {
val csv =
"""i,s,b
|1,a,true
|2,b,true
|3,f,false
|4,e,false
|5,e,true""".stripMargin
val tests = CsvTable.fromString[Foo](csv)
forAll (tests) { (foo: Foo) =>
// ...
}
}
}