Skip to content

Problems with null in DataRow #471

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

Closed
jmagaram opened this issue Feb 12, 2017 · 3 comments
Closed

Problems with null in DataRow #471

jmagaram opened this issue Feb 12, 2017 · 3 comments
Labels

Comments

@jmagaram
Copy link

Description

Occasionally my unit tests fail to run and display a very cryptic message in the output. I've narrowed the problem down to using null values inside a DataRow. The error message is completely unhelpful and the fix is unexpected. I can't believe this is by-design.

[DataTestMethod]
[DataRow(false, "abc")]
[DataRow(true, "")]
[DataRow(true, "   ")]
[DataRow(true, null)]
public void IsStringNullOrWhitespace(bool expectedResult, string s)
{
    bool result = String.IsNullOrWhiteSpace(s);
    Assert.AreEqual(expectedResult, result);
}

The error message is Error in executing test. No result returned by extension. If using extension of TestMethodAttribute then please contact vendor.

The "fix" is to replace the last row with...

[DataRow(true, (string)null)]

When I do this, Visual Studio recommends removing the unnecessary cast. I've tried labeling the datarow parameters but can't seem to make this work.

Steps to reproduce

Try running the unit test above in the RC version of Visual Studio 2017.

Expected behavior

I expect the test to run and give me correct results. Or a much more helpful error message.

@jmagaram
Copy link
Author

jmagaram commented Feb 23, 2017

I just ran into this problem again today working on a unit test where a DataRow contains a int? column and I wanted to try null. I can't be the only one bumping into this. In this case, I can't type (int?)null as a value in a DataRow - I get "An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type". So now I probably need to change the type of value in the test method signature to "object" and cast it to (int?) - ugly. And then I still must use (object)null in the DataRow to avoid the "Error in executing test" message.

@AbhitejJohn
Copy link
Contributor

@jmagaram Here is a more convenient workaround until this gets fixed:
Add another dummy argument to your test methods where you can pass in a non-null value. So if you have something like:

[DataRow(true, null)]
public void IsStringNullOrWhitespace(bool expectedResult, string s)
{
    bool result = String.IsNullOrWhiteSpace(s);
    Assert.AreEqual(expectedResult, result);
}

Make that

[DataRow(true, "", null)]
public void IsStringNullOrWhitespace(bool expectedResult, string dummy, string s)
{
    bool result = String.IsNullOrWhiteSpace(s);
    Assert.AreEqual(expectedResult, result);
}

Hope that gets rid of most of the inconvenience. This should be fixed in an up-coming update.

@AbhitejJohn
Copy link
Contributor

This has been fixed in MSTest.TestFramework (1.1.14) and MSTest.TestAdapter (1.1.14). Release notes are here. Thanks for reporting this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants