-
Notifications
You must be signed in to change notification settings - Fork 266
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
Support DataSourceAttribute in .Net Core. #233
Comments
@magol: Apologies for the delay. The .Net Core version of the adapter does not support DataSource yet. I'll update the title and mark this as a request for adding in that support. |
@AbhitejJohn Thanks for the answer. What is you time framme to add this feature? When can I expert to be able to use this? |
@magol: Don't have one yet. Put this temporarily in S123 where it would most likely be addressed. |
I hope MS still support DataRow on Dot Net Core |
@AbhitejJohn What is the status on this? The S123 milestone have past due by about 1 month. |
@magol : Sorry, we had a few other things that came up and this took the back seat. Tagging @sudiptadmsft and @pvlakshm to see when we can bring this in. |
I am waiting on this too.. @AbhitejJohn any ETA? |
Trying to move my Unit Tests to .NET Core 2.0. Of course, I have many row tests. |
this would be great to have working |
Any progress on this? |
Sorry, I haven't been working on the framework for a while now. Tagging @cltshivash @pvlakshm . |
I am also waiting on this. |
More than a year since the initial post and still waiting. This is unacceptable, as it makes it impossible to make use of data-driven unit tests in .NET Core. Please fix this! I'm gonna have to hack in a really sloppy workaround for my test project to work in the meantime.... |
As @AbhitejJohn already mentioned above DynamicData is way to go at this point. Here is an example for CSV reading 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);
} |
@cilerler It is NOT that easy. Is the DynamicDataAttribute available in other .NET platforms yet? We have a bunch of code that compiles to multiple .Net versions. The code and unit tests are shared between platforms. So if I change the unit tests to use DynamicData, I break them for .Net Framework. So basically, I cannot share unit test code. At best I could add a compiler directive to all my unit tests so in .NET Framework it uses the old code and in .NetCore it uses the new code. Actually, I already did this in my open source projects, only the unit tests are simply not run in .NetCore as it doesn't support the DataSourceAttribute. Not to mention that the GetData() method you have above 1) doesn't take into account Xml row tests or Excel row tests, and 2) is far too naive to be used by product code. We need to get this working for Csv, Xml, and Excel. I have a CsvParser, but I have to figure out what Xml parser to use or write my own, figure out what Excel parser to use or write my own, etc. Now I have to bring in either new code, which requires approval and sprint time, etc, or new open source projects, which means a security team involved to vet the open source project and paperwork and process. So again, if it were that easy, we would have done it already. |
@rhyous is spot-on. Relying on DynamicData instead of just implementing this (is there some pressing reason not to?) is not a viable long-term solution to this problem, in my opinion. |
Therefore please take it into the consideration that it could be very easy workaround where things are not complicated as it in your projects. |
Wow. Let's ignore that unhelpful response and talk about implementation of a single solution in the right place, which is in this project, instead having customers implement many one-off solutions. It would be better to implement this once than to have many users implement their own hacks. It looks like this feature is tagged with up-for-grabs so anyone can implement it. That is greateness of open source. So if DynamicDataAttribute is the future, why not implement DataSourceAttribute in this project as a child of DynamicDataAttribute. We could probably write an attribute that inherits from DynamicDataAttribute (except the class is sealed, but that is easy to change, it is an open source project after all) and simply have child class that acts exactly like the DynamicDataAttribute but has the signature of the DataSourceAttribute and we enhance the DataSourceAttribute to populate the GetData needs. Assuming the posters here decide to fork testfx, implement this ourselves, and do a pull request. We could use the source for DataSourceAttribute and associated classes. Can someone at Microsoft comment on if that code can be made available? |
I'm facing the same situation. I was trying to move my Tests to .Net Core but for me is strictly necessary to use DataRow as my information comes only from the DB. So, without this, I wouldn't migrate to .Net Core. |
+1 on the above. Unit testing does not look particularly well supported by Microsoft. |
|
@sebainones : I haven't been getting much time since I've moved off of working on this area. @cltshivash should be able to help answer if this is on the immediate backlog. What @cilerler has added seems to be good start to unblock folks. It does create an API diff between .NetCore and .NetFramework but the DynamicData schematics was what we were hoping we could converge on. |
I have opened links presented here in comments, and they are useless or such voodoo that I do not understand it. |
I found a solution I am happy with using the CsvHelper NuGet package and the My answer on StackOverflow to What is the replacement for TestContext.DataRow[“MyColumnName”] shows all the details. A brief summary is here. The CSV File (Example.csv)
Important: Make sure this CSV file is included in your test project and "Copy To Output Directory" is set to "Always" in the properties for the CSV file in Solution Explorer. Data Transfer Object Used By CsvHelper
The Test Class
After building your solution, you will see a single test named "AddingTwoNumbers" in Test Explorer. Running this single test runs all variants defined in your CSV file. |
If anyone is here, if you are using XML, this is what we have attempted on our side :
and we switched the attribute from
to
|
Did the attempt succeed?
…On Mon, Apr 18, 2022 at 6:37 AM Frederic Forjan ***@***.***> wrote:
If anyone is here, if you are using XML, this is what we have attempted on
our side :
[AttributeUsage(AttributeTargets.Method)]
public class CurrentDataRowAttribute : Attribute, ITestDataSource
{
private readonly string path;
public CurrentDataRowAttribute(string path)
{
this.path = path;
}
public IEnumerable<object[]> GetData(MethodInfo methodInfo)
{
var content = XElement.Load(path);
foreach(var line in content.Elements())
{
yield return new[] { new Data(line) };
}
}
public string GetDisplayName(MethodInfo methodInfo, object[] data)
{
return ((Data)data[0]).ToString();
}
}
public class Data
{
public Data(XElement element)
{
this.From = element.Element("From").Value;
this.To = element.Element("To").Value;
this.Tooltip = element.Element("Tooltip").Value;
this.BecauseMessage = element.Element("BecauseMessage").Value;
this.HasError = Convert.ToBoolean(element.Element("HasError").Value);
}
public string From { get; private set; }
public string To { get; private set; }
public bool HasError { get; private set; }
public string Tooltip { get; private set; }
public string BecauseMessage { get; private set; }
public override string ToString()
{
return string.Format(CultureInfo.InvariantCulture, "From '{0}' To '{1}'", this.From, this.To);
}
}
and we switched the attribute from
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", @"|DataDirectory|\TestData.xml", "TestData",
DataAccessMethod.Sequential)]
public void TestMethod()
to
[CurrentDataRow(@"TestData.xml")]
public void TestMethod(Data data)
—
Reply to this email directly, view it on GitHub
<#233 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAFTJZJE2TWZPJW6P24TDEDVFVQR5ANCNFSM4DVWQD5A>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
@fforjan That is cool. I haven't fixed all my Xml ones. I have a project, github.com/rhyous/unittesting (also there is a NuGet package) that does Json, I haven't implement XML yet. https://github.com/rhyous/UnitTesting I kept meaning to get Xml done, but I ended up converting my Xml to json instead. |
@rhyous just curiositry, we have lot of comment into our XML. How did you solve the comment in JSON ? |
Unlike Xml, Json doesn't support comments. They would be considered a syntax error.
However, you don't have to deserialize the comment. The object you serialize with can exclude them and they simply won't be serialized.
However, I don't usually do that. My only comment is usually a TestName and Message property the is deserialized. The TestName should show up as the test name in VS. The message I pass into the Assert.
|
Will move forward by closing this task as won't fix as exploratoration are showing there is no good abstraction level that would simplify a good chunk of complexity from users while still providing a good flexibility and strong typing. We will revisit our view if we get some compelling stories. |
Description
We have following code
with following xml file
Is
TestContext.DataRow
expected to come to MsTest v2, or is it any other way to do the same thing?Steps to reproduce
Expected behavior
The compile past
Actual behavior
I get following build error
Environment
.NET Core 2.0.0-preview2-006497
Visual Studio 15.3.0 preview 7.0
Windows 10
AB#2303200
The text was updated successfully, but these errors were encountered: