rrr*_*eee 7 c# mstest data-driven-tests .net-core
多年来我一直在寻找这个,我想我终于在"MSTest V2"中找到了一个真正的方法(意思是.netcore附带的那个,并且在Visual Studio 2017中才能真正正确处理).请参阅我的解决方案.
这对我来说解决的问题是我的输入数据不容易序列化,但是我的逻辑需要用许多这些输入进行测试.有很多原因可以解释为什么这样做更好,但那对我来说是个噱头; 我被迫进行了一次巨大的单元测试,并通过我的输入进行for循环.到现在.
小智 11
您现在可以使用DynamicDataAttribute:
[DynamicData("TestMethodInput")]
[DataTestMethod]
public void TestMethod(List<string> list)
{
Assert.AreEqual(2, list.Count);
}
public static IEnumerable<object[]> TestMethodInput
{
get
{
return new[]
{
new object[] { new List<string> { "one" } },
new object[] { new List<string> { "one", "two" } },
new object[] { new List<string> { "one", "two", "three" } }
};
}
}
Run Code Online (Sandbox Code Playgroud)
https://dev.to/frannsoft/mstest-v2---new-old-kid-on-the-block有一个很好的简介
https://blogs.msdn.microsoft.com/devops/2017/07/18/extending-mstest-v2/上有更多血淋淋的细节
所以新的 DataTestMethodAttribute 类是可覆盖的,它允许覆盖具有此签名的方法:
public override TestResult[] Execute(ITestMethod testMethod);
Run Code Online (Sandbox Code Playgroud)
一旦我发现了这一点,就很简单了:我只需推导、计算出我的输入,然后在我的 Execute 方法中循环遍历它们。不过,为了使其易于重复使用,我又走了几步。
因此,首先是一个覆盖该 Execute 方法的基类,并公开一个返回 IEnumerable 的抽象 GetTestInputs() 方法。您可以从中派生出可以实现该方法的任何类型。
public abstract class DataTestMethodWithProgrammaticTestInputs : DataTestMethodAttribute
{
protected Lazy<IEnumerable> _items;
public DataTestMethodWithProgrammaticTestInputs()
{
_items = new Lazy<IEnumerable>(GetTestInputs, true);
}
protected abstract IEnumerable GetTestInputs();
public override TestResult[] Execute(ITestMethod testMethod)
{
var results = new List<TestResult>();
foreach (var testInput in _items.Value)
{
var result = testMethod.Invoke(new object[] { testInput });
var overriddenDisplayName = GetDisplayNameForTestItem(testInput);
if (!string.IsNullOrEmpty(overriddenDisplayName))
result.DisplayName = overriddenDisplayName;
results.Add(result);
}
return results.ToArray();
}
public virtual string GetDisplayNameForTestItem(object testItem)
{
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
接下来,我创建了一个派生类型,它使用反射来实例化一个类型,然后在创建的实例上调用属性的 get 方法。这种类型可以直接用作属性,尽管从它派生、实现 GetDisplayNameForTestItem 方法并绑定到特定类型是一个好主意,尤其是当您有多个测试使用相同的数据时。
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class DataTestMethodWithTestInputsFromClassPropertyAttribute : DataTestMethodWithProgrammaticTestInputs
{
private Type _typeWithIEnumerableOfDataItems;
private string _nameOfPropertyWithData;
public DataTestMethodWithTestInputsFromClassPropertyAttribute(
Type typeWithIEnumerableOfDataItems,
string nameOfPropertyWithData)
: base()
{
_typeWithIEnumerableOfDataItems = typeWithIEnumerableOfDataItems;
_nameOfPropertyWithData = nameOfPropertyWithData;
}
protected override IEnumerable GetTestInputs()
{
object instance;
var defaultConstructor = _typeWithIEnumerableOfDataItems.GetConstructor(Type.EmptyTypes);
if (defaultConstructor != null)
instance = defaultConstructor.Invoke(null);
else
instance = FormatterServices.GetUninitializedObject(_typeWithIEnumerableOfDataItems);
var property = _typeWithIEnumerableOfDataItems.GetProperty(_nameOfPropertyWithData);
if (property == null)
throw new Exception($"Failed to find property named {_nameOfPropertyWithData} in type {_typeWithIEnumerableOfDataItems.Name} using reflection.");
var getMethod = property.GetGetMethod(true);
if (property == null)
throw new Exception($"Failed to find get method on property named {_nameOfPropertyWithData} in type {_typeWithIEnumerableOfDataItems.Name} using reflection.");
try
{
return getMethod.Invoke(instance, null) as IEnumerable;
}
catch (Exception ex)
{
throw new Exception($"Failed when invoking get method on property named {_nameOfPropertyWithData} in type {_typeWithIEnumerableOfDataItems.Name} using reflection. Exception was {ex.ToString()}");
}
}
}
Run Code Online (Sandbox Code Playgroud)
最后,这里有一个正在使用的派生属性类型的例子,它可以很容易地用于许多测试:
[TestClass]
public class MyTestClass
{
public class MyTestInputType{public string Key; public Func<string> F; }
public IEnumerable TestInputs
{
get
{
return new MyTestInputType[]
{
new MyTestInputType(){ Key = "1", F = () => "" },
new MyTestInputType() { Key = "2", F = () => "2" }
};
}
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class DataTestMethodWithTestInputsFromThisTestProjectAttribute : DataTestMethodWithTestInputsFromClassPropertyAttribute
{
public DataTestMethodWithTestInputsFromThisTestProjectAttribute()
: base(typeof(MyTestClass), nameof(MyTestClass.TestInputs)) { }
public override string GetDisplayNameForTestItem(object testItem)
{
var asTestInput = testItem as MyTestInputType;
if (asTestInput == null)
return null;
return asTestInput.Key;
}
}
[DataTestMethodWithTestInputsFromThisTestProject]
public void TestMethod1(MyTestInputType testInput)
{
Assert.IsTrue(testInput.Key == testInput.F());
}
[DataTestMethodWithTestInputsFromThisTestProject]
public void TestMethod2(MyTestInputType testInput)
{
Assert.IsTrue(string.IsNullOrEmpty(testInput.F()));
}
}
Run Code Online (Sandbox Code Playgroud)
就是这样。有人对 mstest 有更好的方法吗?