在 Java 或 Scala 中动态测试生成
Dynamically test generating in Java or Scala
我有一个包含测试列表的大 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 or/and Scala 中,它将执行 json 文件中的所有测试。可能吗?
可以使用 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 项目,我可以在那里为您提供帮助。
我有一个包含测试列表的大 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 or/and Scala 中,它将执行 json 文件中的所有测试。可能吗?
可以使用 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 项目,我可以在那里为您提供帮助。