[英]What`s .Net core alternative of DataSource attribute for mstest?
From the below, got to know that DataSource attribute is no more supported on .net core projects of MSTest.从下文中,了解到 MSTest 的 .net 核心项目不再支持 DataSource 属性。 So what`s the alternative to this?
那么有什么替代方法呢?
Link: https://docs.microsoft.com/en-us/visualstudio/test/how-to-create-a-data-driven-unit-test?view=vs-2019链接: https : //docs.microsoft.com/en-us/visualstudio/test/how-to-create-a-data-driven-unit-test?view=vs-2019
Info: .NET Core does not support the DataSource attribute.信息:.NET Core 不支持 DataSource 属性。 If you try to access test data in this way in a .NET Core or UWP unit test project, you'll see an error similar to "'TestContext' does not contain a definition for 'DataRow' and no accessible extension method 'DataRow' accepting a first argument of type 'TestContext' could be found (are you missing a using directive or an assembly reference?)".
如果您尝试在 .NET Core 或 UWP 单元测试项目中以这种方式访问测试数据,您将看到类似于“'TestContext'不包含'DataRow'的定义并且没有可访问的扩展方法'DataRow'的错误”可以找到接受类型为“TestContext”的第一个参数(您是否缺少 using 指令或程序集引用?)”。
There is an open issue issue-233 about this and no comments on when it can be fixed.有一个关于此的未解决问题issue-233 ,并且没有关于何时可以修复的评论。 There are some work-arounds:
有一些解决方法:
private static string[] SplitCsv(string input)
{
var csvSplit = new Regex("(?:^|,)(\"(?:[^\"]+|\"\")*\"|[^,]*)", RegexOptions.Compiled);
var list = new List<string>();
foreach (Match match in csvSplit.Matches(input))
{
string value = match.Value;
if (value.Length == 0)
{
list.Add(string.Empty);
}
list.Add(value.TrimStart(','));
}
return list.ToArray();
}
private static IEnumerable<string[]> GetData()
{
IEnumerable<string> rows = System.IO.File.ReadAllLines(@"Resources\NameAddressCityStateZip.csv").Skip(1);
foreach (string row in rows)
{
yield return SplitCsv(row);
}
}
[TestMethod]
[DynamicData(nameof(GetData), DynamicDataSourceType.Method)]
//x [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", @"Resources\NameAddressCityStateZip.csv", "NameAddressCityStateZip#csv", DataAccessMethod.Sequential)]
public void TestMethod1(string input, string expected)
{
// Arrange
//x string input = _testContext.Properties["Data"].ToString(); //x _testContext.DataRow["Data"].ToString();
//x string expected = _testContext.Properties["Expected"].ToString(); //x _testContext.DataRow["Expected"].ToString();
var parser = _serviceProvider.GetService<Parser>();
// Act
string actual = parser.MultiParser(input, ModeType.NameAddressCityStateZipCountry).ToString();
// Assert
Assert.AreEqual(expected, actual);
}
but there are drawbacks to it.但它也有缺点。
As exibited in the comment here正如这里的评论中所示
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.