我的scalatest测试使用标记功能,如下所示:
"A test" should "test something" taggedAs (Smoke) in {
....
}
是否有机会将标签添加到使用scalatest运行器的-u选项生成的junit报告?
试图四处查看,但找不到任何答案,只是如何基于这些Tag对象禁用/启用测试。
答案 0 :(得分:1)
scalatest-custom-reporter-example是一个工作示例,展示了如何创建自定义Reporter
并将标签作为custom information传递
给记者。
JUnitXmlReporter
通过-u
argument配置后,将以JUnit的XML格式生成报告:
Test / testOptions += Tests.Argument("-u", "target")
给出以下测试:
class HelloSpec extends FlatSpec with Matchers {
object Slow extends Tag("Slow")
object Unreliable extends Tag("Unreliable")
"The Hello object" should "say hello" taggedAs (Slow) in {
assert(true)
}
it should "sing lullaby" taggedAs (Unreliable, Slow) in {
assert(true)
}
}
默认情况下, JUnitXmlReporter.xmlify 输出以下字符串:
...
<testcase
name="The Hello object should say hello" classname="example.HelloSpec" time="0.011">
</testcase>
<testcase
name="The Hello object should sing lullaby" classname="example.HelloSpec" time="0.001">
</testcase>
...
我们希望将测试的tags添加到报告中,如下所示:
...
<testcase
name="The Hello object should say hello" tag="Set(Slow)" classname="example.HelloSpec" time="0.011">
</testcase>
<testcase
name="The Hello object should sing lullaby" tag="Set(Unreliable, Slow)" classname="example.HelloSpec" time="0.001">
</testcase>
...
通过扩展JUnitXmlReporter
创建自定义报告程序:
package org.scalatest.tools
class JUnitReporterWithTags extends JUnitXmlReporter("target")
添加成员映射以按测试名称保存套件的tags
:
private var tags: Map[String, Set[String]] = Map.empty
覆盖xmlify
以将标签插入输出字符串:
override def xmlify(testsuite: Testsuite): String = {
var xml = super.xmlify(testsuite)
for (testcase <- testsuite.testcases) yield {
xml = xml.replace(s""""${testcase.name}"""", s""""${testcase.name}" tag="${tags(testcase.name)}"""" )
}
xml
}
BeforeAndAfterAll
特性:
class HelloSpec extends FlatSpec with Matchers with BeforeAndAfterAll {
Suite.tags
作为InfoProvided
事件的payload
自变量
通过Informer
传递给记者:
override def beforeAll(): Unit = {
info("", Some(tags))
}
覆盖JUnitXmlReporter.apply
以提取和存储标签有效载荷:
override def apply(event: Event): Unit = {
super.apply(event)
event match {
case e: InfoProvided =>
e.payload.foreach { providedTags =>
tags ++= providedTags.asInstanceOf[Map[String, Set[String]]]
}
case _ =>
}
}
JUnitReporterWithTags
的全限定名称提供给-C argument
:
Test / testOptions += Tests.Argument("-C", "org.scalatest.tools.JUnitReporterWithTags")
sbt test
target/TEST-example.HelloSpec.xml