Skip to content

Commit

Permalink
Junit test reporter (#243)
Browse files Browse the repository at this point in the history
* Initial JUnitTestReporter

* Add dummy version for testing

* fix lint errors

* Add to file factory

---------

Co-authored-by: Drake McCabe <[email protected]>
  • Loading branch information
EdwardPhilo and drake-philo authored Jan 4, 2024
1 parent e199c5f commit 72979fc
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 2 deletions.
1 change: 1 addition & 0 deletions bsc-plugin/src/lib/rooibos/FileFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class FileFactory {
'TestGroup',
'BaseTestReporter',
'ConsoleTestReporter',
'JUnitTestReporter',
'TestResult',
'TestRunner',
'Utils'
Expand Down
1 change: 1 addition & 0 deletions bsc-plugin/src/lib/rooibos/RooibosConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface RooibosConfig {
tags?: string[];
catchCrashes?: boolean;
sendHomeOnFinish?: boolean;
reporter?: string;
keepAppOpen?: boolean;
testSceneName?: string;

Expand Down
1 change: 1 addition & 0 deletions bsc-plugin/src/lib/rooibos/RooibosSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export class RooibosSession {
method.func.body.statements.length,
new RawCodeStatement(undent`
return {
"reporter": "${this.config.reporter || ''}"
"failFast": ${this.config.failFast ? 'true' : 'false'}
"sendHomeOnFinish": ${this.config.sendHomeOnFinish ? 'true' : 'false'}
"logLevel": ${this.config.logLevel ?? 0}
Expand Down
98 changes: 98 additions & 0 deletions framework/src/source/JUnitTestReporter.bs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
namespace rooibos
class JUnitTestReporter extends rooibos.BaseTestReporter

function new(testRunner as dynamic)
super(testRunner)
end function

override function reportResults(allStats as dynamic)
root = createObject("roXMLElement")
root.SetName("testsuites")
properties = root.addElement("properties")

versionProperty = properties.AddElement("property")
versionProperty.AddAttribute("name", "Rooibos Version")
versionProperty.AddAttribute("value", m.testRunner.runtimeConfig.getVersionText())

for each testSuite in m.testRunner.testSuites
suite = root.AddElement("testsuite")
suite.AddAttribute("name", testSuite.name)
suite.AddAttribute("tests", Rooibos.Common.AsString(testSuite.stats.ranCount))
suite.AddAttribute("failures", Rooibos.Common.AsString(testSuite.stats.failedCount))
suite.AddAttribute("skipped", Rooibos.Common.AsString(testSuite.stats.ignoredCount))
suite.AddAttribute("time", Rooibos.Common.AsString(testSuite.stats.time))

for each testGroup in testSuite.groups
m.generateGroupXML(testGroup, suite)
end for
end for

'bs:disable-next-line
ignoredInfo = m.testRunner.runtimeConfig.getIgnoredTestInfo()
if ignoredInfo.count > 0
for each ignoredItemName in ignoredInfo.items
testCase = root.AddElement("testcase")
testCase.AddAttribute("name", ignoredItemName)
testCase.addAttribute("classname", "skipped")
testCase.AddElement("skipped")
end for
end if

? `<?xml version="1.0" encoding="UTF-8"?>` + root.GenXML(false)
end function

function generateGroupXML(testGroup, suite)
for each test in testGroup.tests
testCase = suite.AddElement("testcase")
testCase.AddAttribute("name", test.name)
testCase.AddAttribute("time", Rooibos.Common.AsString(test.result.time))
locationText = "file://" + test.testSuite.filePath.trim() + ":" + Rooibos.Common.AsString(test.lineNumber)
testCase.addAttribute("file", locationText)

' Gitlab displays classname in the suite field for some reason
testCase.addAttribute("classname", test.testSuite.name)

if test.result.isCrash or test.result.isFail
testFailure = testCase.AddElement(test.result.isCrash ? "error" : "failure")

testFailure.addAttribute("message", test.result.getMessage())
testFailure.addAttribute("type", Rooibos.Common.AsString(test.result.getStatusText()))

if test.isParamTest = true
if type(test.rawParams) = "roAssociativeArray"
rawParams = {}
for each key in test.rawParams
if type(test.rawParams[key]) <> "Function" and type(test.rawParams[key]) <> "roFunction"
rawParams[key] = test.rawParams[key]
end if
end for
else
rawParams = test.rawParams
end if
testFailure.addText(chr(10) + "Params: " + formatJson(rawParams) + chr(10))
end if

testFailure.addText("Failure/Error: " + test.result.getMessage() + chr(10))
if test.result.isCrash
testFailure.addText("Trace: " + m.getStackTrace(test.result.error) + chr(10))
end if
end if

end for
end function

function getStackTrace(error) as string
output = ""

for i = error.backTrace.count() - 1 to 0 step -1
e = error.backTrace[i]
if e.filename.instr("pkg:/source/rooibos") = -1
output = output + e.filename + "(" + stri(e.line_number).trim() + ")"
end if
end for

return output
end function

end class
end namespace
8 changes: 6 additions & 2 deletions framework/src/source/TestRunner.bs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ namespace rooibos
m.stats = new rooibos.Stats()
m.runtimeConfig = new rooibos.RuntimeConfig()
m.config = m.runtimeConfig.getRuntimeConfig()
'TODO - allow providing different reporters via config
m.testReporter = new rooibos.ConsoleTestReporter(m)

if m.config.reporter = "JUnitTestReporter"
m.testReporter = new Rooibos.JUnitTestReporter(m)
else
m.testReporter = new Rooibos.ConsoleTestReporter(m)
end if
end function

' /**
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "rooibos",
"version": "1.0.0",
"description": "simple, flexible, fun brightscript test framework for roku scenegraph apps",
"files": [
"dist/**/!(manifest)*"
Expand Down

0 comments on commit 72979fc

Please sign in to comment.