ShaneKm
ShaneKm

Reputation: 21328

xUnit assert multiple properties

In xUnit is there a way to assert only on specific properties?

for a given utility class method

public static CopyStuff(Oder o1, Order o2)
{
   o1.Name = o2.Name;
   o1.Age = o2.Age;
   ...
}

Is there a way to Assert only on those two or more properties? instead of writing multiple Asserts?

I would like to

// Something like this
Assert(result, o2, [Name, Age, Blah..]) 

Upvotes: 1

Views: 7861

Answers (4)

Cristian Zitelli
Cristian Zitelli

Reputation: 17

Nope, you must have a test by property (one assert per test), that is the way to figure out what is going on if something is going bad. For intance, if you have a test named When_CopyStuff_Is_Called_Then_It_Must_Sets_Right_Name() is very clear to understand the error instead all properties

This is very useful when you need to test an Api Model.

Upvotes: 0

xmorera
xmorera

Reputation: 1961

Also I started recently using XBehave which uses Gherkin notation: Given... When... Then...

Incredibly useful as it extends XUnit and allows Should assertions, for example Name.ShouldEqual().

Very readable.

Several asserts should help with readability, don't try to optimize on details that do not provide a clear gain.

Upvotes: 1

Jon Hanna
Jon Hanna

Reputation: 113242

If the class in question either implemented a custom Equals() which acted appropriately, or if it was a struct (Equals() does a field-by-field comparison by default) or anonymous type (Equals() does a property-by-property comparison), then you could assert that they were equal in a single assert.

This would be a bad idea though; there could be a related bug in both the copy and equality operations that meant both failed in such a way that one seemed to confirm that the other had worked.

It's much better to have multiple asserts if not multiple tests.

While in actual code the convenience of "check a bunch of different things are each equal" could be handy, in tests you don't want a "check a bunch of different things are equal"; you want a clear "confirm Name is equal" followed by a clear "confirm Age is equal" and so on, serving not just as a test, but as a clear indication of what has been tested and also by implication what you expect to work (sometimes unit tests are better than written documentation to a new developer coming to a project, in what they say about how something should work).

If you find that you are doing this same check across multiple properties in many tests, then add an AssertAllPropertiesEqual() method into the test themselves, but have the multiple asserts within that all the more obvious and blatant there.

Upvotes: 3

Doug Wilson
Doug Wilson

Reputation: 4260

Nope, not without using reflection. Even with reflection I wouldn't recommend this approach. Unit tests should favor readability over being concise. Just use multiple Asserts.

Also, take a look at the xUnit tests to see how they implement things.

Upvotes: 1

Related Questions