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 1 commit
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
62 changes: 35 additions & 27 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 SkippedStatus =
Copy link
Member

@lefou lefou May 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be plural?

Suggested change
private val SkippedStatus =
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 Down Expand Up @@ -386,9 +387,11 @@ object TestModule {
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}
skipped={
testResults.count(testResult => SkippedStatus.contains(testResult.status)).toString
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You sometimes use Status.toString and sometimes Status.name(). Is there a difference? Can we always use name ?

Copy link
Contributor Author

@romain-gilles-ultra romain-gilles-ultra May 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @lefou
You are right I should always use the Enum.name() function which is the right function to get the string representation of the enum name. I will fix it. BTW I introduced constants for those names it's my mistake to not use them here ;)

}
time={(testResults.map(_.duration).sum / 1000.0).toString}
timestamp={timestamp}>
timestamp={formatTimestamp(timestamp)}>
{properties}
{cases}
</testsuite>
Expand All @@ -398,20 +401,25 @@ object TestModule {
<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}
skipped={
results0.count(testResult => SkippedStatus.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 SkippedStatus.contains(s) => Some(<skipped/>)
case _ => None
}
}
Expand Down
Loading