An idea for making private code more easily testable #9523
Replies: 3 comments 23 replies
-
That attribute already exists for |
Beta Was this translation helpful? Give feedback.
-
What about using |
Beta Was this translation helpful? Give feedback.
-
To my mind, you missed the biggest downside to this idea: private functions are implementation details and therefore should never be tested directly. Therefore any idea that would make it easier to directly test a private functions is inherently bad practice in my view. If you have code that needs directly testing, then that code is part of the public API of that class/library as it has expected behaviour. Therefore mark it public. If you for some reason absolutely do not want it public but absolute do want to be able to test directly eg for debugging purposes, then make it internal and use |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Greetings, current accessibility levels for members is great for object-oriented design, but poor for testing. I propose a new attribute that keeps the advantage of the
private
accessibility level in terms of object-oriented design, but allows visibility for testing. And, this would mimicinternal
in terms of visibility for testing, but allows for the benefits ofprivate
thatinternal
does not allow.The naming is up for discussion, but for purposes of discussion I'll call the attribute
TestableAttribute
. The general way it would work is as follows:public
for API exposure,internal
for API exposure within an assembly, andprivate
for supporting methods.[Testable]
attribute to these methods.This provides the following benefits:
I can also imagine the following downsides, which I raise because obviously I don't think they come near to outweighing the benefits:
internal
with a different name. To which I say first a different name is a benefit here; second that depending on how this is implemented this has the possibility to have some other exciting differences frominternal
andprivate
.Thank you for your consideration, I look forward to the discussion.
Beta Was this translation helpful? Give feedback.
All reactions