1 #region Copyright (c) 2002-2003, James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole, Philip A. Craig
2 /************************************************************************************
4 ' Copyright © 2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
5 ' Copyright © 2000-2003 Philip A. Craig
7 ' This software is provided 'as-is', without any express or implied warranty. In no
8 ' event will the authors be held liable for any damages arising from the use of this
11 ' Permission is granted to anyone to use this software for any purpose, including
12 ' commercial applications, and to alter it and redistribute it freely, subject to the
13 ' following restrictions:
15 ' 1. The origin of this software must not be misrepresented; you must not claim that
16 ' you wrote the original software. If you use this software in a product, an
17 ' acknowledgment (see the following) in the product documentation is required.
19 ' Portions Copyright © 2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
20 ' or Copyright © 2000-2003 Philip A. Craig
22 ' 2. Altered source versions must be plainly marked as such, and must not be
23 ' misrepresented as being the original software.
25 ' 3. This notice may not be removed or altered from any source distribution.
27 '***********************************************************************************/
33 using System
.Globalization
;
39 /// Summary description for XmlResultVisitor.
41 public class XmlResultVisitor
: ResultVisitor
43 private XmlTextWriter xmlWriter
;
44 private TextWriter writer
;
45 private MemoryStream memoryStream
;
47 public XmlResultVisitor(string fileName
, TestResult result
)
49 xmlWriter
= new XmlTextWriter( new StreamWriter(fileName
, false, System
.Text
.Encoding
.UTF8
) );
53 public XmlResultVisitor( TextWriter writer
, TestResult result
)
55 this.memoryStream
= new MemoryStream();
57 this.xmlWriter
= new XmlTextWriter( new StreamWriter( memoryStream
, System
.Text
.Encoding
.UTF8
) );
61 private void Initialize(TestResult result
)
63 ResultSummarizer summaryResults
= new ResultSummarizer(result
);
65 xmlWriter
.Formatting
= Formatting
.Indented
;
66 xmlWriter
.WriteStartDocument(false);
67 xmlWriter
.WriteComment("This file represents the results of running a test suite");
69 xmlWriter
.WriteStartElement("test-results");
71 xmlWriter
.WriteAttributeString("name", summaryResults
.Name
);
72 xmlWriter
.WriteAttributeString("total", summaryResults
.ResultCount
.ToString());
73 xmlWriter
.WriteAttributeString("failures", summaryResults
.Failures
.ToString());
74 xmlWriter
.WriteAttributeString("not-run", summaryResults
.TestsNotRun
.ToString());
76 DateTime now
= DateTime
.Now
;
77 xmlWriter
.WriteAttributeString("date", now
.ToShortDateString());
78 xmlWriter
.WriteAttributeString("time", now
.ToShortTimeString());
81 public void Visit(TestCaseResult caseResult
)
83 xmlWriter
.WriteStartElement("test-case");
84 xmlWriter
.WriteAttributeString("name",caseResult
.Name
);
86 if(caseResult
.Description
!= null)
87 xmlWriter
.WriteAttributeString("description", caseResult
.Description
);
89 xmlWriter
.WriteAttributeString("executed", caseResult
.Executed
.ToString());
90 if(caseResult
.Executed
)
92 xmlWriter
.WriteAttributeString("success", caseResult
.IsSuccess
.ToString());
94 xmlWriter
.WriteAttributeString("time", caseResult
.Time
.ToString("#####0.000", NumberFormatInfo
.InvariantInfo
));
96 xmlWriter
.WriteAttributeString("asserts", caseResult
.AssertCount
.ToString() );
97 WriteCategories(caseResult
);
98 if(caseResult
.IsFailure
)
100 if(caseResult
.IsFailure
)
101 xmlWriter
.WriteStartElement("failure");
103 xmlWriter
.WriteStartElement("error");
105 xmlWriter
.WriteStartElement("message");
106 xmlWriter
.WriteCData( EncodeCData( caseResult
.Message
) );
107 xmlWriter
.WriteEndElement();
109 xmlWriter
.WriteStartElement("stack-trace");
110 if(caseResult
.StackTrace
!= null)
111 xmlWriter
.WriteCData( EncodeCData( StackTraceFilter
.Filter( caseResult
.StackTrace
) ) );
112 xmlWriter
.WriteEndElement();
114 xmlWriter
.WriteEndElement();
120 WriteCategories(caseResult
);
121 xmlWriter
.WriteStartElement("reason");
122 xmlWriter
.WriteStartElement("message");
123 xmlWriter
.WriteCData(caseResult
.Message
);
124 xmlWriter
.WriteEndElement();
125 xmlWriter
.WriteEndElement();
128 xmlWriter
.WriteEndElement();
131 private string EncodeCData( string text
)
133 if ( text
.IndexOf( "]]>" ) < 0 )
136 return text
.Replace( "]]>", "]]>" );
139 public void WriteCategories(TestResult result
)
141 if (result
.Test
.Categories
!= null && result
.Test
.Categories
.Count
> 0)
143 xmlWriter
.WriteStartElement("categories");
144 foreach (string category
in result
.Test
.Categories
)
146 xmlWriter
.WriteStartElement("category");
147 xmlWriter
.WriteAttributeString("name", category
);
148 xmlWriter
.WriteEndElement();
150 xmlWriter
.WriteEndElement();
154 public void Visit(TestSuiteResult suiteResult
)
156 xmlWriter
.WriteStartElement("test-suite");
157 xmlWriter
.WriteAttributeString("name",suiteResult
.Name
);
158 if(suiteResult
.Description
!= null)
159 xmlWriter
.WriteAttributeString("description", suiteResult
.Description
);
161 xmlWriter
.WriteAttributeString("success", suiteResult
.IsSuccess
.ToString());
162 xmlWriter
.WriteAttributeString("time", suiteResult
.Time
.ToString());
163 xmlWriter
.WriteAttributeString("asserts", suiteResult
.AssertCount
.ToString() );
165 WriteCategories(suiteResult
);
166 xmlWriter
.WriteStartElement("results");
167 foreach (TestResult result
in suiteResult
.Results
)
171 xmlWriter
.WriteEndElement();
173 xmlWriter
.WriteEndElement();
180 xmlWriter
.WriteEndElement();
181 xmlWriter
.WriteEndDocument();
184 if ( memoryStream
!= null && writer
!= null )
186 memoryStream
.Position
= 0;
187 using ( StreamReader rdr
= new StreamReader( memoryStream
) )
189 writer
.Write( rdr
.ReadToEnd() );