Skip to content

Commit 4002771

Browse files
hobovskykazk
authored andcommitted
Report error code from a test run
1 parent 68ed266 commit 4002771

File tree

1 file changed

+79
-52
lines changed

1 file changed

+79
-52
lines changed

workspace/Program.fs

Lines changed: 79 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
open System
2-
open NUnit.Engine
32
open System.Reflection
43
open System.Xml
54
open System.Globalization
65

6+
open NUnit.Engine
7+
8+
type TestResult = Unknown | Passed | Failed
9+
710
[<EntryPoint>]
811
let main argv =
9-
1012
let GetAttribute (node: XmlNode) (name: string): string option =
1113
match node with
1214
| null -> None
13-
| elem -> match node.Attributes.[name] with
15+
| elem -> match elem.Attributes.[name] with
1416
| null -> None
1517
| a -> Some(a.Value)
1618

@@ -27,7 +29,15 @@ let main argv =
2729

2830
let escapeLF(s: string): string = s.Replace(Environment.NewLine, "<:LF:>")
2931

30-
let OnTestCase(testCase: XmlNode) =
32+
let GetSuiteResult (testResults: TestResult seq) =
33+
let GetResultScore (result: TestResult): int =
34+
match result with
35+
| Passed -> 0
36+
| Unknown -> 1
37+
| Failed -> 2
38+
if Seq.isEmpty testResults then Unknown else Seq.maxBy GetResultScore testResults
39+
40+
let OnTestCase(testCase: XmlNode): TestResult =
3141

3242
GetDescription testCase
3343
|> Option.orElse (GetAttribute testCase "name")
@@ -38,77 +48,94 @@ let main argv =
3848
|> Option.ofObj
3949
|> Option.iter (fun node -> printfn "%s" node.InnerText)
4050

41-
match GetAttribute testCase "result" with
42-
| Some("Passed") -> printfn "\n<PASSED::>Test Passed"
43-
| Some("Failed") ->
44-
let label = GetAttribute testCase "label"
45-
let message = Option.ofObj <| testCase.SelectSingleNode "failure/message"
46-
match label with
47-
| Some("Error") ->
48-
message
49-
|> Option.map (fun m -> escapeLF(m.InnerText))
50-
|> Option.defaultValue "Unknown Error"
51-
|> printfn "\n<ERROR::>%s"
52-
53-
testCase.SelectSingleNode "failure/stack-trace"
54-
|> Option.ofObj
55-
|> Option.iter (fun node -> printfn "\n<LOG::-Stack Trace>%s" node.InnerText)
56-
| _ ->
57-
message
58-
|> Option.map (fun msg -> "<:LF:>" + escapeLF msg.InnerText)
59-
|> Option.defaultValue ""
60-
|> printfn "\n<FAILED::>Test Failed%s"
61-
| _ -> ()
51+
let testCaseResult =
52+
match GetAttribute testCase "result" with
53+
| Some("Passed") ->
54+
printfn "\n<PASSED::>Test Passed"
55+
Passed
56+
| Some("Failed") ->
57+
let label = GetAttribute testCase "label"
58+
let message = Option.ofObj <| testCase.SelectSingleNode "failure/message"
59+
match label with
60+
| Some("Error") ->
61+
message
62+
|> Option.map (fun m -> escapeLF(m.InnerText))
63+
|> Option.defaultValue "Unknown Error"
64+
|> printfn "\n<ERROR::>%s"
65+
66+
testCase.SelectSingleNode "failure/stack-trace"
67+
|> Option.ofObj
68+
|> Option.iter (fun node -> printfn "\n<LOG::-Stack Trace>%s" node.InnerText)
69+
| _ ->
70+
message
71+
|> Option.map (fun msg -> "<:LF:>" + escapeLF msg.InnerText)
72+
|> Option.defaultValue ""
73+
|> printfn "\n<FAILED::>Test Failed%s"
74+
Failed
75+
| _ -> Unknown
6276
WriteCompletedIn testCase
77+
testCaseResult
6378

64-
65-
let rec OnTestSuiteTestFixture(testFixture: XmlNode) =
66-
79+
let rec OnTestSuiteTestFixture(testFixture: XmlNode): TestResult =
6780
GetDescription testFixture
6881
|> Option.orElse (GetAttribute testFixture "name")
6982
|> Option.defaultValue ""
7083
|> printfn "\n<DESCRIBE::>%s"
7184

72-
for child in testFixture.ChildNodes do
73-
match child.Name with
74-
| "test-suite" ->
75-
match GetAttribute child "type" with
76-
| Some("ParameterizedMethod" | "GenericMethod") -> OnTestSuiteTestFixture(child)
77-
| _ -> ()
78-
| "test-case" -> OnTestCase(child)
79-
| _ -> ()
80-
WriteCompletedIn(testFixture);
81-
82-
let rec OnTestSuiteTestSuite(testSuite: XmlNode) =
85+
let suiteResult =
86+
testFixture.ChildNodes
87+
|> Seq.cast<XmlNode> |> Seq.toList
88+
|> List.map (fun child ->
89+
match child.Name with
90+
| "test-suite" ->
91+
match GetAttribute child "type" with
92+
| Some("ParameterizedMethod" | "GenericMethod") -> OnTestSuiteTestFixture(child)
93+
| _ -> Passed
94+
| "test-case" -> OnTestCase(child)
95+
| _ -> Passed )
96+
|> GetSuiteResult
97+
WriteCompletedIn(testFixture)
98+
suiteResult
99+
100+
let rec OnTestSuiteTestSuite(testSuite: XmlNode): TestResult =
83101

84102
GetDescription testSuite
85103
|> Option.orElse (GetAttribute testSuite "name")
86104
|> Option.defaultValue ""
87105
|> printfn "\n<DESCRIBE::>%s"
88106

89-
for child in testSuite.SelectNodes "test-suite" do
90-
match GetAttribute child "type" with
91-
| Some("TestFixture") -> OnTestSuiteTestFixture(child)
92-
| _ -> OnTestSuiteTestSuite(child)
107+
let suiteResult =
108+
testSuite.SelectNodes "test-suite"
109+
|> Seq.cast<XmlElement> |> Seq.toList
110+
|> List.map (fun child ->
111+
match GetAttribute child "type" with
112+
| Some("TestFixture") -> OnTestSuiteTestFixture(child)
113+
| _ -> OnTestSuiteTestSuite(child) )
114+
|> GetSuiteResult
115+
116+
WriteCompletedIn(testSuite)
93117

94-
WriteCompletedIn(testSuite);
118+
suiteResult
95119

96120

97-
let OnTestSuiteAssembly(testSuite: XmlNode) =
98-
for child in testSuite.SelectNodes "test-suite" do
121+
let OnTestSuiteAssembly(testSuite: XmlNode): TestResult =
122+
testSuite.SelectNodes "test-suite"
123+
|> Seq.cast<XmlElement> |> Seq.toList
124+
|> List.map (fun child ->
99125
match GetAttribute child "type" with
100126
| Some("TestFixture") -> OnTestSuiteTestFixture(child)
101-
| _ -> OnTestSuiteTestSuite(child)
127+
| _ -> OnTestSuiteTestSuite(child) )
128+
|> GetSuiteResult
129+
102130

103-
let reportRun (reportNode: XmlNode) =
131+
let reportRun (reportNode: XmlNode): TestResult =
104132
reportNode.SelectSingleNode "test-suite[@type = 'Assembly']"
105133
|> Option.ofObj
106-
|> Option.iter OnTestSuiteAssembly
134+
|> Option.map OnTestSuiteAssembly
135+
|> Option.defaultValue Unknown
107136

108137
let testpkg = new TestPackage (Assembly.GetExecutingAssembly().Location)
109138
let engine = new TestEngine()
110139
use runner = engine.GetRunner(testpkg)
111140
let reportNode = runner.Run(null, TestFilter.Empty)
112-
// TODO Exit code
113-
reportRun(reportNode)
114-
0
141+
if reportRun(reportNode) = Passed then 0 else 1

0 commit comments

Comments
 (0)