22// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33
44using System ;
5+ using System . Collections . Concurrent ;
56using System . Collections . Generic ;
67using System . IO ;
78using System . Linq ;
89using System . Text ;
910using System . Text . RegularExpressions ;
10- using System . Threading . Tasks ;
11- using Microsoft . Win32 ;
1211using Microsoft . VisualStudio . TestPlatform . ObjectModel ;
1312using Microsoft . VisualStudio . TestPlatform . ObjectModel . Client ;
1413using Microsoft . VisualStudio . TestPlatform . ObjectModel . Logging ;
@@ -29,9 +28,11 @@ public class HtmlTestLogger : ITestLoggerWithParameters
2928 private DateTimeOffset testRunEndTime = DateTimeOffset . MinValue . ToLocalTime ( ) ; //The end time of the test run
3029 private TxtToJSON txtToJSON = new TxtToJSON ( ) ;
3130
31+ private ConcurrentDictionary < string , DataType . TestCaseDetail > results = new ConcurrentDictionary < string , DataType . TestCaseDetail > ( ) ; //All the test results
32+
33+ private const string categoryPropertyKey = "MSTestDiscoverer.TestCategory" ;
3234 private const string scriptNode = "<script language=\" javascript\" type=\" text/javascript\" >" ;
3335 private const string jsFileName_Functions = "functions.js" ;
34- private const string jsFileName_Jquery = "jquery-1.11.0.min.js" ;
3536 private const string indexHtmlName = "index.html" ;
3637 private const string txtResultFolderName = "Txt" ;
3738 private const string htmlResultFolderName = "Html" ;
@@ -123,6 +124,7 @@ private void TestResultHandler(object sender, TestResultEventArgs e)
123124 }
124125
125126 DataType . TestCaseDetail caseDetail = ConvertToTestCase ( e . Result ) ;
127+ results . TryAdd ( caseDetail . Name , caseDetail ) ;
126128
127129 // Generate txt log file
128130 string txtFileName = Path . Combine (
@@ -196,10 +198,13 @@ private void CreateReportFolder()
196198 private DataType . TestCaseDetail ConvertToTestCase ( TestResult result )
197199 {
198200 var eolSeparators = new char [ ] { '\r ' , '\n ' } ;
199- string caseName = ! string . IsNullOrEmpty ( result . DisplayName ) ? result . DisplayName : result . TestCase . FullyQualifiedName . Split ( '.' ) . Last ( ) ;
201+
202+ string caseName = ! string . IsNullOrEmpty ( result . TestCase . DisplayName ) ? result . TestCase . DisplayName : result . TestCase . FullyQualifiedName . Split ( '.' ) . Last ( ) ;
200203 string outcome = result . Outcome == TestOutcome . Skipped ? "Inconclusive" : result . Outcome . ToString ( ) ;
204+ string classType = result . TestCase . FullyQualifiedName . Split ( '.' ) . Reverse ( ) . ElementAtOrDefault ( 1 ) ;
205+ List < string > testCategories = GetCustomPropertyValueFromTestCase ( result . TestCase , categoryPropertyKey ) ;
201206
202- var ret = new DataType . TestCaseDetail ( caseName , result . StartTime , result . EndTime , outcome , result . TestCase . Source ) ;
207+ var ret = new DataType . TestCaseDetail ( caseName , result . StartTime , result . EndTime , outcome , result . TestCase . Source , classType , testCategories ) ;
203208
204209 if ( ! String . IsNullOrEmpty ( result . ErrorStackTrace ) )
205210 {
@@ -257,6 +262,32 @@ private DataType.TestCaseDetail ConvertToTestCase(TestResult result)
257262 return ret ;
258263 }
259264
265+ /// <summary>
266+ /// Get Custom property values from test cases.
267+ /// </summary>
268+ /// <param name="testCase">TestCase object extracted from the TestResult</param>
269+ /// <param name="categoryID">Property Name from the list of properties in TestCase</param>
270+ /// <returns> list of properties</returns>
271+ public List < string > GetCustomPropertyValueFromTestCase ( TestCase testCase , string categoryID )
272+ {
273+ var customProperty = testCase . Properties . FirstOrDefault ( t => t . Id . Equals ( categoryID ) ) ;
274+
275+ if ( customProperty != null )
276+ {
277+ var cateogryValues = ( string [ ] ) testCase . GetPropertyValue ( customProperty ) ;
278+ if ( cateogryValues != null )
279+ {
280+ return cateogryValues . ToList ( ) ;
281+ }
282+ else
283+ {
284+ return Enumerable . Empty < String > ( ) . ToList ( ) ;
285+ }
286+ }
287+
288+ return Enumerable . Empty < String > ( ) . ToList ( ) ;
289+ }
290+
260291 /// <summary>
261292 /// Inserts the corresponding script to the template html and generates the [testcase].html
262293 /// </summary>
@@ -314,10 +345,8 @@ private string ConstructListAndSummaryObj(TestRunCompleteEventArgs e)
314345 {
315346 StringBuilder sb = new StringBuilder ( ) ;
316347 sb . AppendLine ( ) ;
317- sb . AppendLine ( "var listObj = " + txtToJSON . TestCasesString ( txtResultFolderPath , captureFolderPath ) + ";" ) ;
348+ sb . AppendLine ( "var listObj = " + txtToJSON . TestCasesString ( txtResultFolderPath , captureFolderPath , results ) + ";" ) ;
318349
319- // Clean the temp file
320- File . Delete ( txtToJSON . CaseCategoryFile ) ;
321350 return sb . ToString ( ) ;
322351 }
323352
0 commit comments