locked
Should I create a unit test for a property? RRS feed

  • Question

  • When I select a class and click on create unit test it selects everything in the class including properties.

    Should I be or do I need to be testing properties? I would guess the answer is yes if there is some logic to test? Correct?


    chuckdawit
    Friday, August 5, 2011 9:35 PM

Answers

  • Hi,

    at least that is what we do. If there is no logic inside the properties, there is no need to write a test. But as soon as you have logic inside, you should write tests that test this logic.

    It makes no sense in my eyes:
    - To test that the property exist. (You should get compile errors if you try to access a property that is not there. maybe a special condition could be if you know that the objects are used through reflection so there might be unit tests wanted to just make sure, that some properties exist? But normaly I would consider this as bad design.)
    - The test, that a property without additional code works belongs to the .Net Framework and not to your code. The unit tests you write in your project target your project. It is not your duty to check that the .Net framework has no bugs.

    Just my simple view on it. Maybe others have better explanations.

    With kind regards,

    Konrad

    • Marked as answer by witdaj Saturday, August 6, 2011 7:12 PM
    Saturday, August 6, 2011 6:47 AM