简体   繁体   English

在Java或Scala中动态测试生成

[英]Dynamically test generating in Java or Scala

I have a big json file with list of tests. 我有一个带有测试列表的大json文件。 Json file contains several filenames, which contains names of test classes, which have some setup stuff and a list of tests. Json文件包含几个文件名,这些文件名包含测试类的名称,其中包含一些设置内容和测试列表。 Here is an example of such json file: 这是此类json文件的示例:

{
   "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. 而且我需要以某种方式用Java或Scala编写的几个类来执行此测试。 So there should be 1-3 classes written in Java or/and Scala which will perform all tests from json file. 因此,应该有1-3个用Java或/和Scala编写的类,它们将从json文件执行所有测试。 Is it possible? 可能吗?

It is possible using specs2 . 可以使用specs2 Let's say you can deserialize your Json file to 假设您可以将Json文件反序列化为

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. 如果您对此有任何疑问,请创建一个小型的Github项目,我可以从那里帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM