Should you Unit Test simple properties?

前端 未结 8 1464
耶瑟儿~
耶瑟儿~ 2021-02-01 13:22

Should you Unit Test simple properties of a class, asserting that a value is set and retrieved? Or is that really just unit testing the language?

Example

8条回答
  •  我在风中等你
    2021-02-01 13:32

    Are you adhering to strict TDD practices or not?

    If yes then you absolutely should write tests on public getters and setters, otherwise how will you know if you've implemented them correctly?

    If no, you still probably should write the tests. Though the implementation is trivial today, it is not guaranteed to remain so, and without a test covering the functionality of a simple get/set operation, when a future change to implementation breaks an invariant of "setting property Foo with a value Bar results in the getter for property Foo returning value Bar" the unit tests will continue to pass. The test itself is also trivially implemented, yet guards against future change.

提交回复
热议问题