Project class:
namespace ClassLibrary1
{
public class Project
{
/// <summary>
/// Everyone can access
/// </summary>
public void Test01()
{
}
/// <summary>
/// Cannot be easily tested without assembly
/// </summary>
internal void Test02()
{
}
/// <summary>
/// Do not expose if not required. Will not be accessed from test, we use instead testing accessor using Test03Testing.
/// </summary>
private void Test03()
{
}
/// <summary>
/// Create accessor for testing as internal.
/// </summary>
internal void Test03Testing()
{
Test03();
}
}
}
Test class
Now we create new test project and we want to create test for this implementation.
using ClassLibrary1;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTestProject1
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void Test01()
{
var project = new Project();
project.Test01();
}
[TestMethod]
public void Test02()
{
var project = new Project();
project.Test02();
}
[TestMethod]
public void Test03()
{
var project = new Project();
project.Test03Testing();
}
}
}
However the compilation will end with messages and you will not get intellisense when typing the internal methods.
Now we will add "magic" line to AssemblyInfo.cs of the project that we want to test.
Default project looks like this:
Inside of the file add line:
// Expose internal methods to test class
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Project.Tests")]
This will expose all internal items to test project.
If your rebuild the project again and all is working.
See test application here:
http://dl.dropbox.com/u/84298264/CodeExamples/TestingApplication.zip
With thanks to Bryan Avery for finding out the solution :)
No comments:
Post a Comment