Montag, 18. Oktober 2010

Unittest: Testing non-public methods

Sometimes you want to write unit tests against methods that aren't publicly exposed. Rather than make these private methods public, which is a bad idea, you can specify friendly assemblies to which your methods will be exposed.

All you have to do is edit the AssemblyInfo.cs of the assembly you want to expose to your tests and add the following line ("SampleTestProject" is the name of the test assembly):

[assembly: InternalsVisibleTo("SampleTestProject")]
After this change in the assembly.cs and a rebuild, your “internal” signed methods will be exposed.

1 Kommentar:

  1. Hello Patric,
    so you don't have to make private methods public but you still have to make the internal in order to get this running.
    We had several discussions over at http://dotnet-forum.de/forums/ about this subject and one important thing has crystalized out of it: If you have to test private methods you should reconsider the project's architecture.
    Greetings
    Rainer Hilmer

    AntwortenLöschen