开发者

Reasons for/against using partial classes for testing?

what are pros and cons of using partial classes for writing N开发者_Python百科Unit tests?

I am going to start:

pro: private methods can be tested

con: TDD is not really possible anymore

What else?


Con: either you have to test a different build to the one you ship, or you end up with your unit test code (and members) in your shipping code.

Sounds like a really bad idea to me in general.

I usually go for separate production/test projects, with [InternalsVisibleTo] allowing testing of internal methods (which goes against the dogma of some folks, but seems pragmatic to me).


I would argue that being able to test private methods is a 'con', as it can encourage accretion of new code within an existing class. If the private logic is so complex that it needs dedicated tests (beyond what's publicly accessible), then there's a lot of value in pulling out that logic as a separate class with a publicly testable interface. For borderline cases, I cautiously agree with Jon's approach of using internal methods (just be careful how much the internals expose.)

That said, I've occasionally made a class unsealed specifically so that I could write tests that used virtual method calls for sensing of effects, which is loosely related to the partial class approach. This always feels a bit dirty to me and I sometimes wished there were a way to mark a class as "internal unsealed".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜