Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access classes in another assembly for unit-testing purposes?

Tags:

I'm jumping into unit-testing the Visual-Studio 2008 way, and I'm wondering what's the best way to accomplish cross-assembly class access for testing purposes.

Basically, I have two projects in one solution:

  1. MyProject (C#)
  2. MyProjectTests (C# Test Project)

Everything in MyProject currently has default accessibility, which if I recall correctly means everything is effectively internal. I'm mostly looking to test at the class level, but there are a few delegates involved.

There will probably be an external API sometime in the future, but I'm about 20% of the way to feature complete (at least on paper) and I'm getting pretty leery of layering more code on top of this untested core. Accordingly I'd like to get some testing done now, before the app is complete enough for traditional (read: bad and/or lazy) functional testing and definitely before the version n+1 external API is up.

In addition to a straight answer, an example of the solution would be greatly appreciated.

like image 376
Kevin Montrose Avatar asked Jul 31 '09 09:07

Kevin Montrose


People also ask

How do you unit test classes which create new objects?

The first step is to import Mockito dependencies into your code. Now let us see an example of how to test the class. Let us assume the below is the class that we want to test. Below is the sample class of the object that is instantiated in ClassToBeTested.

Can unit testing be performed parallel to coding?

Unit Testing is the type of software testing level in which each individual component of the software is tested. Unit Testing is generally performed by the developer. Unit Testing can't be used for those systems which have a lot of interdependence between different modules. It does not allow for parallel testing.

Should unit tests be in the same package?

Unit tests can be in any package. In essence they are just separate classes used to test the behaviour of the class being tested.

Are unit tests run in parallel?

unittest-parallel is a parallel unit test runner for Python with coverage support. By default, unittest-parallel runs unit tests on all CPU cores available. To run your unit tests with coverage, add either the "--coverage" option (for line coverage) or the "--coverage-branch" for line and branch coverage.


2 Answers

You can use assembly-level attribute InternalsVisibleToAttribute to achieve this.

Add

[assembly:InternalsVisibleTo("MyProjectTests")] 

to AssemblyInfo.cs in your MyProject assembly.

like image 173
Arnold Zokas Avatar answered Oct 07 '22 21:10

Arnold Zokas


You need to add

[assembly:InternalsVisibleTo("Unit.Tests.Assembly")]  

to AssemblyInfo.cs of your "MyProject (C#)". That then allows your tests to access the internal methods for testing.

like image 31
AutomatedTester Avatar answered Oct 07 '22 21:10

AutomatedTester