Being new to unit testing I would like to know if I am supposed to test the values of objects returned from methods when doing unit testing. As an example, consider the following classes:

public class Person
    public int Id { get; set }
    public int Age { get; set; }
    public string Name { get; set; }
    public Pet Pet { get; set; }

public class Pet
    public int Id { get; set; }
    public int Age { get; set; }
    public Breed Breed { get; set; }

And a method with the following declaration:

public Person GetPerson(int id) { ... }

I am now curious if I am supposed to create a test that checks to see if the various properties of the Person are correct (GetPerson_PersonFound_PersonHasCorrectName() and so on)? I suspect that I, under normal circumstances, should? All depends on the requirements?

If those properties should be tested, am I then supposed to dig into Pet and check those properties as well? What about going even further, down into Breed? Can add up to quite a few tests quite quickly.

  • Absolutely yes. Pet is as much of the returned object as Age, so you should test the values in the same circunstancies (that is, the contract of the method forces it to be set to an specific value).
    – SJuan76
    Jul 25, 2014 at 6:36

2 Answers 2


Of course you must test the values of an object returned by the tested method - at least those that are necessary to fulfill that method's contract. What else are you supposed to test? Its class? That's already guaranteed by the type system. (And if your type system doesn't enforce strict types, all the more reason to test that a property of the returned object (1) exists and (2) has the expected value.

If you mean whether properties in the stricter sense, i.e. accessor methods, should be tested, in principle they should - but if they are all trivial or even auto-generated, obviously the potential for error is rather small, so those are not the most important tests you should write.


I would agree to test a lot. In TDD you normaly write a Test first and THEN Implement. So if you don't write a Test which checks if a Person has an Age, then you don't implement it. TDD workflow:

  1. Write a Test which is not running (Red).
  2. Implement only that this Test and the others are running (Green).
  3. Refactor.

Although I must say that I don't Test Properties. Only if they contain logic. The reason is that Properties don't fail. What you can test is user Input, so that Age is formatted correctly and so on.

  • Yah, I've read about not testing properties. However, isn't testing automatic properties still a good idea in an attempt to prevent possible errors if logic is added in the future? Jul 25, 2014 at 8:16
  • you will test it through the logic. As Example:you have a Birthday property. When you add a Property for Age, You need to implement logic, therefor you write FIRST a Test. Through this all is tested. If you don't use the property in your logic you obviously don't need it.
    – Iron
    Jul 25, 2014 at 8:22
  • Well if you're testing the GetPerson method, making sure the Person you get has the expected property values is important.
    – Andy
    Sep 3, 2015 at 23:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.