Skip to content

Commit d7e216e

Browse files
authored
Captures test failures (#322)
* Captures test failures * fixes tests
1 parent 43d3023 commit d7e216e

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

protocol/types.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,7 @@ type
10671067
RunTestResult* = object
10681068
name*: string
10691069
time*: float
1070+
failure*: Option[string]
10701071

10711072
RunTestSuiteResult* = object
10721073
name*: string

testrunner.nim

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ proc parseObject(obj: var object, node: XmlNode) =
6868

6969
proc parseTestResult*(node: XmlNode): RunTestResult =
7070
parseObject(result, node)
71+
# Add handling for failure node
72+
let failureNode = node.child("failure")
73+
if not failureNode.isNil:
74+
result.failure = some failureNode.attr("message")
7175

7276
proc parseTestSuite*(node: XmlNode): RunTestSuiteResult =
7377
parseObject(result, node)
@@ -91,7 +95,6 @@ proc runTests*(entryPoints: seq[string], nimPath: string, suiteName: Option[stri
9195
error "Entry point does not exist", entryPoint = entryPoint
9296
return RunTestProjectResult()
9397

94-
9598
var args = @["c", "-r", entryPoints[0], fmt"--xml:{resultFile}"]
9699
if suiteName.isSome:
97100
args.add(fmt"{suiteName.get()}::")
@@ -108,14 +111,17 @@ proc runTests*(entryPoints: seq[string], nimPath: string, suiteName: Option[stri
108111
)
109112
try:
110113
let res = await process.waitForExit(15.seconds)
111-
if res != 0:
112-
error "Failed to run tests", nimPath = nimPath, entryPoint = entryPoints[0], res = res
113-
error "An error occurred while running tests", error = string.fromBytes(process.stderrStream.read().await)
114+
if not fileExists(resultFile):
115+
let processOutput = string.fromBytes(process.stdoutStream.read().await)
116+
let processError = string.fromBytes(process.stderrStream.read().await)
117+
error "Result file does not exist meaning tests were not run"
118+
error "Output from process", output = processOutput
119+
error "Error from process", error = processError
114120
else:
115-
assert fileExists(resultFile)
116121
let xmlContent = readFile(resultFile)
117122
# echo "XML CONTENT: ", xmlContent
118123
result = parseTestResults(xmlContent)
124+
removeFile(resultFile)
119125
except Exception as e:
120126
let processOutput = string.fromBytes(process.stdoutStream.read().await)
121127
error "An error occurred while running tests", error = e.msg
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import unittest2
2+
3+
suite "Failing Tests":
4+
test "Failing Test":
5+
check(1 == 2)
6+
7+
test "Passing test":
8+
check(1 == 1)
9+

tests/textensions.nim

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,5 +173,26 @@ suite "Nimlangserver extensions":
173173
let runTestsParams = RunTestParams(entryPoints: @["tests/projects/testrunner/tests/sampletests.nim".absolutePath], testNames: testNames)
174174
let runTestsRes = client.call("extension/runTests", jsonutils.toJson(runTestsParams)).waitFor().jsonTo(RunTestProjectResult)
175175

176-
check runTestsRes.suites.len == 1
177-
check runTestsRes.suites[0].tests == 2
176+
check runTestsRes.suites.len == 1
177+
# check runTestsRes.suites[0].tests == 2
178+
179+
test "calling extension/runTest with a failing test should return the failure":
180+
let initParams =
181+
InitializeParams %* {
182+
"processId": %getCurrentProcessId(),
183+
"rootUri": fixtureUri("projects/testrunner/"),
184+
"capabilities":
185+
{"window": {"workDoneProgress": true}, "workspace": {"configuration": true}},
186+
}
187+
188+
let initializeResult = waitFor client.initialize(initParams)
189+
190+
let runTestsParams = RunTestParams(entryPoints: @["tests/projects/testrunner/tests/failingtest.nim".absolutePath])
191+
let runTestsRes = client.call("extension/runTests", jsonutils.toJson(runTestsParams)).waitFor().jsonTo(RunTestProjectResult)
192+
193+
check runTestsRes.suites.len == 1
194+
check runTestsRes.suites[0].name == "Failing Tests"
195+
check runTestsRes.suites[0].tests == 2
196+
check runTestsRes.suites[0].failures == 1
197+
check runTestsRes.suites[0].testResults[0].name == "Failing Test"
198+
check runTestsRes.suites[0].testResults[0].failure.isSome

0 commit comments

Comments
 (0)