1.多实例参数化测试
Xunit参数不匹配会报错,MSTest不会;
//MSTest
public class Solution103Tests
{
[TestMethod()]
[DataRow(new object[]{ 3, 9, 20, null, null, 15, 7 },2,"2" )]//这里测试参数不匹配无法调试测试,而Xunit参数不匹配会报错
public void ZigzagLevelOrderTest(object[] array)
{
//Arrange
var expected =
new List<IList<int>> {new List<int>() {3}, new List<int>() {20, 9}, new List<int>() {15, 7}};
//Act
var actual = new Solution103().ZigzagLevelOrder(array.CreateTree());
//Assert
for (int i = 0; i < actual.Count; i++)
{
Assert.IsTrue(expected[i].SequenceEqual(actual[i]));
}
Assert.That.SequenceEqual2(expected, actual, "You are wrong!!!");
Assert.AreEqual(expected.Count, expected.Count, "You are wrong!!!");
var typ = expected.GetType();
Assert.IsInstanceOfType(actual, typ);
}
}
2.MSTest是单例模式,Xunit构造函数protect修改
这导致MSTest.That有更好的扩展性;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace LeetCodeTest.CommunityToolKit
{
public static class AssertExtend
{
/// <summary>
/// list判断内容是否相等
/// </summary>
/// <remarks>
/// Examples
/// Assert.That.SequenceEqual(list1, list2, "You are wrong!!!");
/// </remarks>
/// <param name="assert">Assert.That单例</param>
/// <param name="list1">集合1</param>
/// <param name="list2">集合2</param>
/// <param name="msg">错误信息</param>
public static void SequenceEqual(this Assert assert, IList<object> list1, IList<object> list2, string msg)
{
if (list1.Count != list2.Count)
Assert.Fail(msg);
var ok = list1.SequenceEqual(list2);
Assert.IsTrue(ok, msg);
}
/// <summary>
/// 嵌套list判断内容是否相等
/// </summary>
/// <remarks>
/// Examples
/// Assert.That.SequenceEqual2(list1, list2, "You are wrong!!!");
/// </remarks>
/// <param name="assert">Assert.That单例</param>
/// <param name="list1">集合1</param>
/// <param name="list2">集合2</param>
/// <param name="msg">错误信息</param>
public static void SequenceEqual2(this Assert assert, IList<IList<int>>? list1, IList<IList<int>>? list2, string msg)
{
if (list1 == null || list2 == null)
Assert.Fail(msg);
if (list1.Count != list2.Count)
Assert.Fail(msg);
var ok = true;
for (int i = 0; i < list1.Count; i++)
{
ok = ok && list1[i].SequenceEqual(list2[i]);
}
Assert.IsTrue(ok, msg);
}
}
}