1

I'm facing some difficulties with unit testing in C#.

Let's say I have

class Dummy{
    TypeA Foo {get; set;}
    TypeB Bar {get; set;}
}

and the test method

[TestMethod]
public void TestStuff()
{
    Type type = typeof(Dummy);
    PropertyInfo[] properties = type.GetProperties();

    foreach(PropertyInfo property in properties)
    {
        string result= MyStaticClass.ProcessProperty(property.Name);
        Assert.IsFalse(string.IsNullOrWhiteSpace(result));
    }
}

The test runs fine, but when it fails I have no clue about which property causes the problem.

In other test methods I've used the [DataTestMethod] and [DataRow(stuff)] in order to provide single inputs and know what caused the test to fail.

Is there a way to do something like this using reflection?

Am I thinking of a wrong unit test?

I'd like to use this approach to check consistency, is it wrong at all?

9
  • 1
    Can you not just add a message to your assert saying what property has failed, etc.? Commented Nov 20, 2018 at 9:39
  • 1
    Oh... so silly of me... It was what I needed, thanks! Commented Nov 20, 2018 at 9:41
  • Interesting test :) Commented Nov 20, 2018 at 9:44
  • @Chris probably post that as answer? Commented Nov 20, 2018 at 9:44
  • 1
    @magicleon: I've not really got anything to add that isn't in Carlos's answer so not going to post my own. I'm happy for him to get the upvotes and the accept. :) Commented Nov 20, 2018 at 9:55

1 Answer 1

4

Assert has many interesting properties params!

You can do something like:

Assert.IsFalse(string.IsNullOrWhiteSpace(result), $"{property.Name} is null");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.