Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests to the JUnit xml report generation #3184

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 40 additions & 32 deletions scalalib/src/mill/scalalib/TestModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ trait TestModule

object TestModule {
private val FailedTestReportCount = 5
private val ErrorStatus = Status.Error.name()
private val FailureStatus = Status.Failure.name()
private val SkippedStates =
Set(Status.Ignored.name(), Status.Skipped.name(), Status.Pending.name())

/**
* TestModule using TestNG Framework to run tests.
Expand Down Expand Up @@ -334,9 +338,12 @@ object TestModule {
testReportXml: Option[String],
props: Option[Map[String, String]] = None
): Result[(String, Seq[TestResult])] = {
testReportXml.foreach(fileName =>
genTestXmlReport(results, ctx.dest / fileName, props.getOrElse(Map.empty))
)
for {
fileName <- testReportXml
path = ctx.dest / fileName
xml <- genTestXmlReport(results, Instant.now(), props.getOrElse(Map.empty))
_ = scala.xml.XML.save(path.toString(), xml, xmlDecl = true)
} yield ()
handleResults(doneMsg, results, Some(ctx))
}

Expand All @@ -348,17 +355,11 @@ object TestModule {
def scalacOptions: T[Seq[String]] = Seq.empty[String]
}

private def genTestXmlReport(
private[scalalib] def genTestXmlReport(
results0: Seq[TestResult],
out: os.Path,
timestamp: Instant,
props: Map[String, String]
): Unit = {
val timestamp = DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(
LocalDateTime.ofInstant(
Instant.now.truncatedTo(ChronoUnit.SECONDS),
ZoneId.systemDefault()
)
)
): Option[Elem] = {
def durationAsString(value: Long) = (value / 1000d).toString
def testcaseName(testResult: TestResult) =
testResult.selector.replace(s"${testResult.fullyQualifiedName}.", "")
Expand All @@ -384,34 +385,41 @@ object TestModule {

<testsuite name={fqn}
tests={testResults.length.toString}
failures={testResults.count(_.status == Status.Failure.toString).toString}
errors={testResults.count(_.status == Status.Error.toString).toString}
skipped={testResults.count(_.status == Status.Skipped.toString).toString}
time={(testResults.map(_.duration).sum / 1000.0).toString}
timestamp={timestamp}>
failures={testResults.count(_.status == FailureStatus).toString}
errors={testResults.count(_.status == ErrorStatus).toString}
skipped={
testResults.count(testResult => SkippedStates.contains(testResult.status)).toString
}
time={durationAsString(testResults.map(_.duration).sum)}
timestamp={formatTimestamp(timestamp)}>
{properties}
{cases}
</testsuite>
}
// todo add the parent module name
val xml =
<testsuites tests={results0.size.toString}
failures={results0.count(_.status == Status.Failure.toString).toString}
errors={results0.count(_.status == Status.Error.toString).toString}
skipped={results0.count(_.status == Status.Skipped.toString).toString}
failures={results0.count(_.status == FailureStatus).toString}
errors={results0.count(_.status == ErrorStatus).toString}
skipped={
results0.count(testResult => SkippedStates.contains(testResult.status)).toString
}
time={durationAsString(results0.map(_.duration).sum)}>
{suites}
</testsuites>
if (results0.nonEmpty) scala.xml.XML.save(out.toString(), xml, xmlDecl = true)
if (results0.nonEmpty) Some(xml) else None
}

private def testCaseStatus(e: TestResult): Option[Elem] = {
val Error = Status.Error.toString
val Failure = Status.Failure.toString
val Ignored = Status.Ignored.toString
val Skipped = Status.Skipped.toString
val Pending = Status.Pending.toString
private def formatTimestamp(timestamp: Instant): String = {
DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(
LocalDateTime.ofInstant(
timestamp.truncatedTo(ChronoUnit.SECONDS),
ZoneId.of("UTC")
)
)
}

private def testCaseStatus(e: TestResult): Option[Elem] = {
val trace: String = e.exceptionTrace.map(stackTraceTrace =>
stackTraceTrace.map(t =>
s"${t.getClassName}.${t.getMethodName}(${t.getFileName}:${t.getLineNumber})"
Expand All @@ -423,17 +431,17 @@ object TestModule {
)
).getOrElse("")
e.status match {
case Error if (e.exceptionMsg.isDefined && e.exceptionName.isDefined) =>
case ErrorStatus if (e.exceptionMsg.isDefined && e.exceptionName.isDefined) =>
lefou marked this conversation as resolved.
Show resolved Hide resolved
Some(<error message={e.exceptionMsg.get} type={e.exceptionName.get}>
{trace}
</error>)
case Error => Some(<error message={"No Exception or message provided"}/>)
case Failure if (e.exceptionMsg.isDefined && e.exceptionName.isDefined) =>
case ErrorStatus => Some(<error message="No Exception or message provided"/>)
case FailureStatus if (e.exceptionMsg.isDefined && e.exceptionName.isDefined) =>
Some(<failure message={e.exceptionMsg.get} type={e.exceptionName.get}>
{trace}
</failure>)
case Failure => Some(<failure message={"No Exception or message provided"}/>)
case Ignored | Skipped | Pending => Some(<skipped/>)
case FailureStatus => Some(<failure message="No Exception or message provided"/>)
case s if SkippedStates.contains(s) => Some(<skipped/>)
case _ => None
}
}
Expand Down
Loading