Search code examples
jsonscalajunitscalatestspecs2

Dynamically test generating in Java or Scala


I have a big json file with list of tests. Json file contains several filenames, which contains names of test classes, which have some setup stuff and a list of tests. Here is an example of such json file:

{
   "filename1.py": {
     "ClassName": [
       "setupSection": [
         here will be list of sqls which should be performed before tests
        ],
   "listOfTests": {
     "test1Name": [
        { here will be query }, {here will be expected result}
     ],
     "test1Name": [
        { here will be query }, {here will be expected result}
     ]
    }
   },
  "filename2.py": {
     "ClassName": [
       "setupSection": [
         here will be list of sqls which should be performed before tests
        ],
   "listOfTests": {
     "test1Name": [
        { here will be query }, {here will be expected result}
     ],
     "test1Name": [
        { here will be query }, {here will be expected result}
     ]
    }
   }
}

And I need somehow perform this tests with a few classes written in Java or Scala. So there should be 1-3 classes written in Java or/and Scala which will perform all tests from json file. Is it possible?


Solution

  • It is possible using specs2. Let's say you can deserialize your Json file to

    case class Query(query: Query)
    case class Test(name: String, query: Query, result: String)
    case class TestFile(setup: Query, tests: List[Test])
    

    Then you can create the following specification

    import org.specs2._
    
    class JsonSpec(path: String) extends Specification {
       lazy val files: List[TestFile] = readFilesFromJson(path)
    
       def createTests(tests: List[Test]): Fragments = 
         Fragments.foreach(tests) { test =>
           s2"""|
                |${test.name ! executeQuery(test.query) must_== test.result}""".stripMargin
         }
    
       def is = 
         Fragments.foreach(files) { file =>
         s2"""|
              |${step(executeQuery(file.setup))}
              |${createTests(file.tests) 
         """.stripMargin
       }
    
       // load the Json file
       def readFilesFromJson(path: String): List[TestFile] =
         ???
    
       // execute the query and return the result
       // as a String
       def executeQuery(query: Query): String = 
         ???
    }
    

    If you have any issue with that please create a small Github project and I can help you from there.