Should we test the values of objects returned from the methods being unit tested?

  softwareengineering

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.

1

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.

3

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website Kho Theme wordpress Kho Theme WP Theme WP

Should we test the values of objects returned from the methods being unit tested?

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.

1

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.

3

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website Kho Theme wordpress Kho Theme WP Theme WP

LEAVE A COMMENT