This is the online documentation for PostSharp 5.0.
Download PDF or CHM. Go to v4.3 or v5.0

Testing Build-Time Logic

Testing build-time logic of aspects has specific challenges:

  • Aspects can emit errors and warnings, which cannot be tested using a run-time testing framework. We need a mechanism to test error messages themselves.

  • When a project contains a large number of test cases (which are all compiled at the same time), it is difficult to isolate one specific case when the debugger is attached to the build process (see Debugging Build-Time Logic). We need a mechanism to run the build process on a single test case.

Therefore, we built a test framework specifically for the purpose of testing aspects.

This topic contains the following sections:

Creating an aspect unit test project

To create an aspect unit test project:

  1. Create a console project and add all required references to it.

  2. Add PostSharp to this project

  3. Edit the project file using a text editor. The project file must import PostSharp.BuildTests.targets before Microsoft.CSharp.targets (download). File PostSharp.targets also needs to be included (which is the case if the PostSharp NuGet package is added to the project).

  4. Implement each test case as a standalone file having its own Program class and Main method. To avoid naming conflicts, every file should have a distinct namespace.

A test is considered successful in the following situations:

  • the test compiles using the C# or VB compiler, and

  • the test compiles using PostSharp without any unexpected message (see below), and

  • the output exe is valid according to PEVERIFY, and

  • the output exe executes successfully and returns the exit code 0,

This default behavior can be altered by test directives, as described below.

Executing a single test

Execute the following line from the command prompt:

msbuild /t:TestOne /p:Source=MyFile.cs
Executing all tests from a directory

Execute the following line from the command prompt:

msbuild /t:Test /p:SourceDir=MyDirectory
Executing all tests in the project directory

Execute the following line from the command prompt:

msbuild /t:Test
Test that messages are emitted

If the test is expected to emit a message (error, warning, information), insert the text @ExpectedMessage(PS0001) in the test file as a comment line.

If this directive is present, the test will be valid if and only if all expected messages, and no other, have been emitted.

Allow unsafe code

To enable unsafe code and disable verification by PEVERIFY, insert the text @Unsafe in the test file as a comment line.

Creating a reference assembly

In case that a test requires a dependency assembly (typically, for tests that require two assemblies, for instance testing aspect inheritance that crosses assembly boundaries), you can create a second file named MyTest.Dependency.cs, if the first file is named MyTest.cs. This will create an assembly MyTest.Dependency.dll, and main test will have a reference to this assembly.