5

It used to be the number one reason for us to choose MSTest from others that we could access and test private methods. Now that Private accessors are deprecated in Visual Studio 2012. Does anyone know why Microsoft make such decision? Is it because it's not a good practice to test private methods?

Also, if I still need to unit test my private methods, how could I do that in VS 2012 and later versions?

leppie
  • 115,091
  • 17
  • 196
  • 297
user3918598
  • 285
  • 1
  • 4
  • 12
  • The page you linked lists a few options. – Dirk Aug 18 '14 at 16:20
  • They've been deprecated for a while. The options on the right of the link you posted doesn't say why but it lists your choices. – Mike Cheel Aug 18 '14 at 16:21
  • 1
    Also, from that doc: "Create a reflection framework that would be able to reflect off your code to access internal or private APIs." This means 'get a mocking framework that can do this'. – Mike Cheel Aug 18 '14 at 16:22
  • 4
    You shouldn't be testing your private methods period. Test at a higher level and make sure that the overall outcome is what you expect. I recommend "The Art of Unit Testing" by Roy Osherove, it's an excellent book – reggaeguitar Aug 18 '14 at 16:44
  • how about using PrivateObject? – k.c. Mar 01 '16 at 10:14

1 Answers1

6

According to VS team article Generation of Private Accessors... this feature was deprecated in 2010 for following reasons:

  1. Lack of resources and time: The focus for this release has been to improve the experience for manual testers, so the priority for the code generation and publicize features has been lowered. There have also been other issues with the publicize functionality that we utilize that have not been addressed.

  2. New features by Language teams: As the language teams have made modifications to their project types and languages, we have been unable to respond to the changes they have made and have not been able to work with the new features they have introduced.

(More historical notes may be found via following search: mstest why private accessors depricated site:blogs.msdn.com ).

For your second part of the question - generally you should not need to unit-test private methods. If you really feel need to expose such methods for testing - consider if marking internal and using "friend" (InternalsVisibleToAttribute) would work for your case.

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • 1
    So, the reason that it is deprecated is just logistics (not enough resources, moved to other assemblies) rather than an anti-pattern reason or a-like. – user3918598 Aug 18 '14 at 17:09
  • 3
    @user3918598 - "just logistic" is probably too strong way of reading it - likely (purely my speculation, I work for Microsoft, but very far from VS team) it was because general usefulness/need for the feature is below cost of implementing/improving. Consider reading [Minus 100 points](http://blogs.msdn.com/b/ericgu/archive/2004/01/12/57985.aspx) article on one approach used in Microsoft to pick what features to implement/improve. – Alexei Levenkov Aug 18 '14 at 18:24