【问题标题】:Dynamically test generating in Java or Scala在 Java 或 Scala 中动态测试生成
【发布时间】:2016-06-22 03:07:36
【问题描述】:

我有一个包含测试列表的大 json 文件。 Json 文件包含几个文件名,其中包含测试类的名称,其中包含一些设置内容和测试列表。以下是此类 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}
     ]
    }
   }
}

而且我需要以某种方式使用一些用 Java 或 Scala 编写的类来执行这些测试。所以应该有 1-3 个用 Java 或/和 Scala 编写的类,它们将从 json 文件执行所有测试。有可能吗?

【问题讨论】:

    标签: json scala junit scalatest specs2


    【解决方案1】:

    可以使用specs2。假设您可以将您的 Json 文件反序列化为

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

    然后你可以创建以下规范

    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 = 
         ???
    }
    

    如果您对此有任何疑问,请创建一个小型 Github 项目,我可以从那里帮助您。

    【讨论】:

      猜你喜欢
      • 2018-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-10
      相关资源
      最近更新 更多