As I develop code, I often want to unit test some of the building blocks of a class even if they are normally private. If my unit tests are inside the project, I can use "Friend" to accomplish this and still keep the functions private for normal use. But I would rather move my NUnit tests into their own separate project(s). How do I achieve the effect I'm looking for?
You can't (easily) test private methods from a different project, but it's quite common to test internal methods (Friend
in VB) from a test project using InternalsVisibleToAttribute
. This makes Friend
members accessible to another assembly.
Apparently this was new in VB 9 although it was available in C# 2... not quite sure why, but this blog post from Bart de Smet gives a quick example.
Note that if your production assembly is signed, your test assembly will need to be signed too, and you'll have to specify the public key in the InternalsVisibleToAttribute
arguments. See this Stack Overflow answer for more details.
You can use Reflection to invoke the private methods. There are plenty of samples out there to do this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With