static void WriteJUnitSystemOut(XmlWriter w, SuiteOrTest test) { if (test.Skipped) { w.WriteStartElement("skipped"); w.WriteEndElement(); } else if (test.Failure) { test.Failures.ForEach(fail => { w.WriteStartElement("failure"); w.WriteAttributeString("message", fail.Message + "\n" + string.Join("\n", fail.Stack)); w.WriteEndElement(); }); } if (test.Logs == null || test.Logs.Count == 0) { return; } w.WriteStartElement("system-out"); w.WriteAttributeString("xml", "space", null, "preserve"); test.Logs.ForEach(m => { w.WriteString(m.Message + "\n " + string.Join("\n ", m.Stack) + "\n"); }); w.WriteEndElement(); }
static void WriteTestCases(XmlWriter w, SuiteOrTest suite) { suite.Nested.ForEach(test => { if (test.IsSuite) { return; } w.WriteStartElement("testcase"); w.WriteAttributeString("name", test.Name); w.WriteAttributeString("time", (test.Duration * 0.001).ToString("F4", CultureInfo.InvariantCulture)); if (test.Skipped) { w.WriteStartElement("skipped"); w.WriteEndElement(); } else if (test.Failure) { test.Failures.ForEach(fail => { w.WriteStartElement("failure"); w.WriteAttributeString("message", fail.Message + "\n" + string.Join("\n", fail.Stack)); w.WriteEndElement(); }); } WriteJUnitSystemOut(w, test); w.WriteEndElement(); }); }
static void WriteTestCases(XmlWriter w, SuiteOrTest suite) { suite.Nested.ForEach(test => { if (test.IsSuite) { return; } w.WriteStartElement("testcase"); w.WriteAttributeString("name", test.Name); w.WriteAttributeString("time", (test.Duration * 0.001).ToString("F4", CultureInfo.InvariantCulture)); WriteJUnitSystemOut(w, test); w.WriteEndElement(); }); }
static void RecursiveWriteJUnit(XmlWriter w, SuiteOrTest suite, string name, bool isWritingFlatTestSuites) { var duration = 0d; var testCaseCount = 0; var flat = true; if (suite.Nested != null) { suite.Nested.ForEach(n => { if (n.IsSuite) { flat = false; return; } testCaseCount++; duration += n.Duration; }); } if (flat || !isWritingFlatTestSuites) { duration = suite.Duration; } var isRecursiveTestCaseToWrite = !isWritingFlatTestSuites && !string.IsNullOrEmpty(name); if (testCaseCount > 0 || isRecursiveTestCaseToWrite) { w.WriteStartElement("testsuite"); w.WriteAttributeString("name", string.IsNullOrEmpty(name) ? "root" : name); w.WriteAttributeString("time", (duration * 0.001).ToString("F4", CultureInfo.InvariantCulture)); WriteTestCases(w, suite); if (!flat && !isWritingFlatTestSuites) { if (suite.Nested != null) { suite.Nested.ForEach(n => { if (n.IsSuite) { RecursiveWriteJUnit(w, n, n.Name, false); } }); } } WriteJUnitSystemOut(w, suite); w.WriteEndElement(); } if (!flat && (isWritingFlatTestSuites || !isRecursiveTestCaseToWrite)) { if (suite.Nested != null) { suite.Nested.ForEach(n => { if (n.IsSuite) { RecursiveWriteJUnit(w, n, (!string.IsNullOrEmpty(name) ? name + "." : "") + n.Name, isWritingFlatTestSuites); } }); } } }
static void RecursiveWriteJUnit(XmlWriter w, SuiteOrTest suite, string name) { var duration = 0d; var count = 0; var flat = true; suite.Nested.ForEach(n => { if (n.IsSuite) { flat = false; return; } count++; duration += n.Duration; }); if (flat) { duration = suite.Duration; } if (count > 0) { w.WriteStartElement("testsuite"); w.WriteAttributeString("name", string.IsNullOrEmpty(name) ? "root" : name); w.WriteAttributeString("time", (duration * 0.001).ToString("F4", CultureInfo.InvariantCulture)); suite.Nested.ForEach(test => { if (test.IsSuite) { return; } w.WriteStartElement("testcase"); w.WriteAttributeString("name", test.Name); w.WriteAttributeString("time", (test.Duration * 0.001).ToString("F4", CultureInfo.InvariantCulture)); if (test.Skipped) { w.WriteStartElement("skipped"); w.WriteEndElement(); } else if (test.Failure) { test.Failures.ForEach(fail => { w.WriteStartElement("failure"); w.WriteAttributeString("message", fail.Message + "\n" + string.Join("\n", fail.Stack)); w.WriteEndElement(); }); } WriteJUnitSystemOut(w, test); w.WriteEndElement(); }); WriteJUnitSystemOut(w, suite); w.WriteEndElement(); } if (!flat) { suite.Nested.ForEach(n => { if (n.IsSuite) { RecursiveWriteJUnit(w, n, (!string.IsNullOrEmpty(name) ? name + "." : "") + n.Name); } }); } }
public void OnMessage(ILongPollingConnection connection, string message, JToken data) { try { switch (message) { case "newClient": { if (_verbose) { Console.WriteLine("New Test Client: " + data.Value <string>("userAgent")); } var client = UAParser.Parser.GetDefault().Parse(data.Value <string>("userAgent")); lock (_lock) { _userAgent = client.ToString(); if (_url != null) { DoStart(); } else { _connection.Send("wait", null); } } _testServer.NotifySomeChange(); break; } case "wholeStart": { lock (_lock) { if (_curResults == null) { break; } _curResults.TotalTests = (int)data; _suiteId = 0; _suiteStack = new Stack <SuiteOrTest>(); _suiteStack.Push(_curResults); } _testServer.NotifyTestingStarted(); _testServer.NotifySomeChange(); break; } case "wholeDone": { lock (_lock) { if (_curResults == null) { break; } if (_suiteStack == null) { break; } _curResults.Duration = (double)data; _curResults.Running = false; _oldResults = _curResults; _curResults = null; _suiteStack = null; } _testServer.NotifyFinishedResults(_oldResults); _testServer.NotifySomeChange(); break; } case "suiteStart": { lock (_lock) { if (_curResults == null) { break; } if (_suiteStack == null) { break; } var suite = new SuiteOrTest { Id = ++_suiteId, ParentId = _suiteStack.Peek().Id, Name = (string)data, Nested = new List <SuiteOrTest>(), Duration = 0, Failure = false, IsSuite = true, Failures = new List <MessageAndStack>(), Skipped = false, Logs = new List <MessageAndStack>() }; _suiteStack.Peek().Nested.Add(suite); _suiteStack.Push(suite); } _testServer.NotifySomeChange(); break; } case "suiteDone": { lock (_lock) { if (_curResults == null) { break; } if (_suiteStack == null) { break; } var suite = _suiteStack.Pop(); suite.Duration = data.Value <double>("duration"); suite.Failures.AddRange(ConvertFailures(data.Value <JArray>("failures"))); if (suite.Failures.Count > 0) { suite.Failure = true; foreach (var s in _suiteStack) { s.Failure = true; } } } _testServer.NotifySomeChange(); break; } case "testStart": { lock (_lock) { if (_curResults == null) { break; } if (_suiteStack == null) { break; } var test = new SuiteOrTest { Id = ++_suiteId, ParentId = _suiteStack.Peek().Id, Name = data.Value <string>("name"), Stack = ConvertMessageAndStack("", data.Value <string>("stack")).Stack.Where(f => f.FileName != "testbundle.js").ToList(), Nested = null, Duration = 0, Failure = false, IsSuite = false, Failures = new List <MessageAndStack>(), Skipped = false, Logs = new List <MessageAndStack>() }; _suiteStack.Peek().Nested.Add(test); _suiteStack.Push(test); } _testServer.NotifySomeChange(); break; } case "testDone": { lock (_lock) { if (_curResults == null) { break; } if (_suiteStack == null) { break; } var test = _suiteStack.Pop(); test.Duration = data.Value <double>("duration"); test.Failures.AddRange(ConvertFailures(data.Value <JArray>("failures"))); _curResults.TestsFinished++; var status = data.Value <string>("status"); if (status == "passed") { } else if (status == "skipped" || status == "pending" || status == "disabled") { _curResults.TestsSkipped++; test.Skipped = true; } else { _curResults.TestsFailed++; test.Failure = true; foreach (var s in _suiteStack) { s.Failure = true; } } } _testServer.NotifySomeChange(); break; } case "consoleLog": { lock (_lock) { if (_curResults == null) { break; } if (_suiteStack == null) { break; } var test = _suiteStack.Peek(); test.Logs.Add(ConvertMessageAndStack(data.Value <string>("message"), data.Value <string>("stack"))); } _testServer.NotifySomeChange(); break; } } } catch (Exception ex) { Console.WriteLine("Exception in TestServerConnectionHandler message: " + message); Console.WriteLine(ex.ToString()); } }
public void OnMessage(ILongPollingConnection connection, string message, JToken data) { try { switch (message) { case "newClient": { if (_verbose) { _logger.Info($"New Test Client: {data.Value<string>("userAgent")}"); } var client = UAParser.Parser.GetDefault().Parse(data.Value <string>("userAgent")); lock (_lock) { _userAgent = client.ToString(); if (_url != null) { DoStart(); } else { _connection.Send("wait", null); } } _testServer.NotifySomeChange(); break; } case "wholeStart": { if (_verbose) { _logger.Info($"wholeStart tests:{(int)data}"); } lock (_lock) { if (_curResults == null) { break; } _curResults.TotalTests = (int)data; _suiteId = 0; if (_suiteStack == null) { _suiteStack = new Stack <SuiteOrTest>(); _suiteStack.Push(_curResults); } } _testServer.NotifyTestingStarted(); _testServer.NotifySomeChange(); break; } case "wholeDone": { if (_verbose) { _logger.Info($"wholeDone duration:{((double)data):f2}"); } lock (_lock) { if (_curResults == null) { break; } if (_suiteStack == null) { break; } _curResults.Duration = (double)data; _curResults.Running = false; _oldResults = _curResults; _curResults = null; _suiteStack = null; } _testServer.NotifyFinishedResults(_oldResults); _testServer.NotifySomeChange(); break; } case "suiteStart": { if (_verbose) { _logger.Info($"suiteStart {(string)data}"); } lock (_lock) { if (_curResults == null) { break; } if (_suiteStack == null) { break; } var suite = new SuiteOrTest { Id = ++_suiteId, ParentId = _suiteStack.Peek().Id, Name = (string)data, Nested = new List <SuiteOrTest>(), Duration = 0, Failure = false, IsSuite = true, Failures = new List <MessageAndStack>(), Skipped = false, Logs = new List <MessageAndStack>() }; _suiteStack.Peek().Nested.Add(suite); _suiteStack.Push(suite); } _testServer.NotifySomeChange(); break; } case "suiteDone": { lock (_lock) { if (_curResults == null) { break; } if (_suiteStack == null) { break; } var suite = _suiteStack.Pop(); suite.Duration = data.Value <double>("duration"); if (_verbose) { _logger.Info($"suiteDone {suite.Name} {suite.Duration:f2}"); } suite.Failures.AddRange(ConvertFailures(data.Value <JArray>("failures"))); if (suite.Failures.Count > 0) { _curResults.SuitesFailed += suite.Failures.Count; suite.Failure = true; _logger.Error( $"suite {suite.Name} in between test failures\n{string.Join('\n', suite.Failures.Select(f => f.Message + "\n " + string.Join("\n ", f.Stack)))}"); foreach (var s in _suiteStack) { s.Failure = true; } } } _testServer.NotifySomeChange(); break; } case "testStart": { if (_verbose) { _logger.Info("testStart " + data.Value <string>("name")); } lock (_lock) { if (_curResults == null) { break; } if (_suiteStack == null) { break; } var test = new SuiteOrTest { Id = ++_suiteId, ParentId = _suiteStack.Peek().Id, Name = data.Value <string>("name"), Stack = ConvertMessageAndStack("", data.Value <string>("stack")).Stack .Where(f => f.FileName != "bundle.js").ToList(), Nested = null, Duration = 0, Failure = false, IsSuite = false, Failures = new List <MessageAndStack>(), Skipped = false, Logs = new List <MessageAndStack>() }; _suiteStack.Peek().Nested.Add(test); _suiteStack.Push(test); } _testServer.NotifySomeChange(); break; } case "testDone": { lock (_lock) { if (_curResults == null) { break; } if (_suiteStack == null) { break; } var test = _suiteStack.Pop(); test.Duration = data.Value <double>("duration"); test.Failures.AddRange(ConvertFailures(data.Value <JArray>("failures"))); _curResults.TestsFinished++; var status = data.Value <string>("status"); if (_verbose) { _logger.Info("testDone " + test.Name + " " + status); } if (status == "passed") { } else if (status == "skipped" || status == "pending" || status == "disabled" || status == "excluded") { _curResults.TestsSkipped++; test.Skipped = true; } else { _curResults.TestsFailed++; test.Failure = true; foreach (var s in _suiteStack) { s.Failure = true; } } } _testServer.NotifySomeChange(); break; } case "consoleLog": { lock (_lock) { if (_curResults == null) { break; } if (_suiteStack == null) { break; } var test = _suiteStack.Peek(); test.Logs.Add(ConvertMessageAndStack(data.Value <string>("message"), data.Value <string>("stack"))); } _testServer.NotifySomeChange(); break; } case "onerror": { if (_verbose) { _logger.Error("onerror " + data); } lock (_lock) { if (_curResults == null) { break; } _suiteId = 0; _suiteStack = new Stack <SuiteOrTest>(); _suiteStack.Push(_curResults); _curResults.Failures.Add(ConvertMessageAndStack(data.Value <string>("message"), data.Value <string>("stack"))); _logger.Error("Test onerror " + _curResults.Failures[^ 1].Message);