Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I unit test private functions from a separate project in VB .NET?

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?

like image 947
user78369 Avatar asked Mar 15 '09 21:03

user78369


Video Answer


2 Answers

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.

like image 178
Jon Skeet Avatar answered Oct 05 '22 03:10

Jon Skeet


You can use Reflection to invoke the private methods. There are plenty of samples out there to do this.

like image 45
Danny G Avatar answered Oct 05 '22 01:10

Danny G