Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Workitem support in TRX logger #2666

Merged
2 commits merged into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ internal interface ITestElement
TestExecId ParentExecutionId { get; set; }
TestListCategoryId CategoryId { get; set; }
TestCategoryItemCollection TestCategories { get; }
WorkitemCollection Workitems { get; set; }
Haplois marked this conversation as resolved.
Show resolved Hide resolved
TestType TestType { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ internal abstract class TestElement : ITestElement, IXmlTestStore
protected TestExecId executionId;
protected TestExecId parentExecutionId;
protected TestCategoryItemCollection testCategories;
protected WorkitemCollection workitems;
Haplois marked this conversation as resolved.
Show resolved Hide resolved
protected TestListCategoryId catId;

public TestElement(Guid id, string name, string adapter)
Expand Down Expand Up @@ -161,6 +162,20 @@ public TestCategoryItemCollection TestCategories
}
}

/// <summary>
/// Gets or sets the work items.
/// </summary>
public WorkitemCollection Workitems
Haplois marked this conversation as resolved.
Show resolved Hide resolved
{
get { return this.workitems; }

set
{
EqtAssert.ParameterNotNull(value, "value");
this.workitems = value;
}
}

/// <summary>
/// Gets the adapter name.
/// </summary>
Expand Down Expand Up @@ -230,6 +245,8 @@ public virtual void Save(System.Xml.XmlElement element, XmlTestStoreParameters p
if (this.parentExecutionId != null)
h.SaveGuid(element, "Execution/@parentId", this.parentExecutionId.Id);

h.SaveObject(this.workitems, element, "Workitems", parameters);

XmlTestStoreParameters testIdParameters = XmlTestStoreParameters.GetParameters();
testIdParameters[TestId.IdLocationKey] = "@id";
h.SaveObject(this.id, element, testIdParameters);
Expand All @@ -245,6 +262,7 @@ private void Initialize()
this.executionId = TestExecId.Empty;
this.parentExecutionId = TestExecId.Empty;
this.testCategories = new TestCategoryItemCollection();
this.workitems = new WorkitemCollection();
this.isRunnable = true;
this.catId = TestListCategoryId.Uncategorized;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.TestPlatform.Extensions.TrxLogger.ObjectModel
{
using System;
using System.Globalization;
using System.Text;

using Microsoft.TestPlatform.Extensions.TrxLogger.Utility;
using Microsoft.TestPlatform.Extensions.TrxLogger.XML;

#region Workitem
/// <summary>
/// Stores an int which represents a workitem
/// </summary>
internal sealed class Workitem : IXmlTestStore
{
#region Fields
[StoreXmlField(Location = ".")]
private int id = 0;

#endregion

#region Constructors
/// <summary>
/// Create a new item with the workitem set
/// </summary>
/// <param name="workitemId">The workitem.</param>
public Workitem(int workitemId)
{
this.id = workitemId;
}

#endregion

#region Properties/Methods
/// <summary>
/// Gets the id for this Workitem
/// </summary>
public int Id
{
get
{
return this.id;
}
}

#endregion

#region Methods - overrides
/// <summary>
/// Compare the values of the items
/// </summary>
/// <param name="other">Value being compared to.</param>
/// <returns>True if the values are the same and false otherwise.</returns>
public override bool Equals(object other)
{
Workitem otherItem = other as Workitem;
if (otherItem == null)
{
return false;
}
return this.id == otherItem.id;
}

/// <summary>
/// Convert the workitem to a hashcode
/// </summary>
/// <returns>Hashcode of the workitem.</returns>
public override int GetHashCode()
{
return this.id.GetHashCode();
}

/// <summary>
/// Convert the workitem to a string
/// </summary>
/// <returns>The workitem.</returns>
public override string ToString()
{
return this.id.ToString(CultureInfo.InvariantCulture);
}
#endregion

#region IXmlTestStore Members

/// <summary>
/// Saves the class under the XmlElement.
/// </summary>
/// <param name="element"> XmlElement element </param>
/// <param name="parameters"> XmlTestStoreParameters parameters</param>
public void Save(System.Xml.XmlElement element, XmlTestStoreParameters parameters)
{
new XmlPersistence().SaveSingleFields(element, this, parameters);
}

#endregion
}
#endregion

#region WorkitemCollection
/// <summary>
/// A collection of ints represent the workitems
/// </summary>
internal sealed class WorkitemCollection : EqtBaseCollection<Workitem>
{
#region Constructors
/// <summary>
/// Creates an empty WorkitemCollection.
/// </summary>
public WorkitemCollection()
{
}

/// <summary>
/// Create a new WorkitemCollection based on the int array.
/// </summary>
/// <param name="items">Add these items to the collection.</param>
public WorkitemCollection(int[] items)
{
EqtAssert.ParameterNotNull(items, "items");
foreach (int i in items)
{
this.Add(new Workitem(i));
}
}

#endregion

#region Methods
/// <summary>
/// Adds the workitem.
/// </summary>
/// <param name="item">Workitem to be added.</param>
public void Add(int item)
{
this.Add(new Workitem(item));
}

/// <summary>
/// Adds the workitem.
/// </summary>
/// <param name="item">Workitem to be added.</param>
public override void Add(Workitem item)
{
EqtAssert.ParameterNotNull(item, "item");
base.Add(item);
}

/// <summary>
/// Convert the WorkitemCollection to a string.
/// each item is separated by a comma (,)
/// </summary>
/// <returns></returns>
public override string ToString()
{
StringBuilder returnString = new StringBuilder();
if (this.Count > 0)
{
returnString.Append(",");
foreach (Workitem item in this)
{
returnString.Append(item);
returnString.Append(",");
}
}

return returnString.ToString();
}

/// <summary>
/// Convert the WorkitemCollection to an array of ints.
/// </summary>
/// <returns>Array of ints containing the workitems.</returns>
public int[] ToArray()
{
int[] result = new int[this.Count];

int i = 0;
foreach (Workitem item in this)
{
result[i++] = item.Id;
}

return result;
}

/// <summary>
/// Compare the collection items
/// </summary>
/// <param name="obj">other collection</param>
/// <returns>true if the collections contain the same items</returns>
public override bool Equals(object obj)
{
WorkitemCollection other = obj as WorkitemCollection;
bool result = false;

if (other == null)
{
result = false;
}
else if (object.ReferenceEquals(this, other))
{
result = true;
}
else if (this.Count != other.Count)
{
result = false;
}
else
{
foreach (Workitem item in this)
{
if (!other.Contains(item))
{
result = false;
break;
}
}
}

return result;
}

/// <summary>
/// Return the hash code of this collection
/// </summary>
/// <returns>The hashcode.</returns>
public override int GetHashCode()
{
return base.GetHashCode();
}
#endregion
}
#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ public ITestElement ToTestElement(
testElement.TestCategories.Add(testCategory);
}

var workItems = GetCustomPropertyValueFromTestCase(rockSteadyTestCase, "WorkItemIds")
.Select(workItem => int.Parse(workItem));
foreach (int workItem in workItems)
{
testElement.Workitems.Add(workItem);
}

return testElement;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -736,13 +736,11 @@ public void CustomTrxFileNameShouldConstructFromLogFileParameter()
Assert.AreEqual(Path.Combine(TrxLoggerTests.DefaultTestRunDirectory, TrxLoggerTests.DefaultLogFileNameParameterValue), this.testableTrxLogger.trxFile, "Wrong Trx file name");
}



/// <summary>
/// Unit test for reading TestCategories from the TestCase which is part of test result.
/// </summary>
[TestMethod]
public void GetCustomPropertyValueFromTestCaseShouldReadCategoyrAttributesFromTestCase()
public void GetCustomPropertyValueFromTestCaseShouldReadCategoryAttributesFromTestCase()
{
ObjectModel.TestCase testCase1 = CreateTestCase("TestCase1");
TestProperty testProperty = TestProperty.Register("MSTestDiscoverer.TestCategory", "String array property", string.Empty, string.Empty, typeof(string[]), null, TestPropertyAttributes.Hidden, typeof(TestObject));
Expand All @@ -759,6 +757,24 @@ public void GetCustomPropertyValueFromTestCaseShouldReadCategoyrAttributesFromTe
CollectionAssert.AreEqual(listCategoriesExpected, listCategoriesActual);
}

[TestMethod]
public void GetCustomPropertyValueFromTestCaseShouldReadWorkitemAttributesFromTestCase()
{
ObjectModel.TestCase testCase1 = CreateTestCase("TestCase1");
TestProperty testProperty = TestProperty.Register("WorkItemIds", "String array property", string.Empty, string.Empty, typeof(string[]), null, TestPropertyAttributes.Hidden, typeof(TestObject));

testCase1.SetPropertyValue(testProperty, new[] { "99999", "0" });

var converter = new Converter(new Mock<IFileHelper>().Object, new TrxFileHelper());
List<string> listWorkitemsActual = converter.GetCustomPropertyValueFromTestCase(testCase1, "WorkItemIds");

List<string> listWorkitemsExpected = new List<string>();
listWorkitemsExpected.Add("99999");
listWorkitemsExpected.Add("0");

CollectionAssert.AreEqual(listWorkitemsExpected, listWorkitemsActual);
}

[TestMethod]
public void CRLFCharactersShouldGetRetainedInTrx()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,22 @@ public void ToTestElementShouldAssignTestCategoryOfUnitTestElement()
CollectionAssert.AreEqual(expected, unitTestElement.TestCategories.ToArray().OrderByDescending(x => x.ToString()).ToArray());
}

[TestMethod]
public void ToTestElementShouldAssignWorkitemOfUnitTestElement()
{
TestPlatformObjectModel.TestCase testCase = CreateTestCase("TestCase1");
TestPlatformObjectModel.TestResult result = new TestPlatformObjectModel.TestResult(testCase);
TestProperty testProperty = TestProperty.Register("WorkItemIds", "String array property", string.Empty, string.Empty, typeof(string[]), null, TestPropertyAttributes.Hidden, typeof(TestObject));

testCase.SetPropertyValue(testProperty, new[] { "3", "99999", "0" });

var unitTestElement = this.converter.ToTestElement(testCase.Id, Guid.Empty, Guid.Empty, testCase.DisplayName, TrxLoggerConstants.UnitTestType, testCase);

int[] expected = new[] { 0, 3, 99999 };

CollectionAssert.AreEquivalent(expected, unitTestElement.Workitems.ToArray());
}

/// <summary>
/// Unit test for regression when there's no test categories.
/// </summary>
Expand Down