Test helper for a single method

I’ve build during years a test helper infrastructure that permits me to decorate a test fixture with some custom attributes that are capable to execute code before and after fixture setup/teardown or test setup/teardown. Now my dear friend Ugo needs for Dexter a modified version of this framework to be able to apply attributes directly to tests.

Basically Ugo needs to change the principal during each test, and he want to be able to write code like this one.

   1: [PrincipalForTest("Alkampfer", new [] {"Administrator", "User"})]

   2: [Test]

   3: public void VerifyPrincipalForTestSingleMethod()

   4: {

   5:     Assert.That(Thread.CurrentPrincipal.IsInRole("Administrator"));

   6:     Assert.That(Thread.CurrentPrincipal.IsInRole("User"));

   7: }

   8:  

   9: [PrincipalForTest("Alkampfer", new[] { "Group" })]

  10: [Test]

  11: public void VerifyPrincipalForTestSingleMethodSingleGroup()

  12: {

  13:     Assert.That(Thread.CurrentPrincipal.IsInRole("Group"));

  14: }

Now the PrincipalForTestAttribute is a standard ITestHelperAttribute defined in this way.

   1: public class PrincipalForTestAttribute : Attribute, ITestHelperAttribute

   2: {

   3:     public int Order

   4:     {

   5:         get { return 1; }

   6:     }

   7:  

   8:     public ITestHelper Create()

   9:     {

  10:         return new PrincipalForTestHelper(UserName, Roles);

  11:     }

  12:  

  13:     public String UserName { get; set; }

  14:  

  15:     public String[] Roles { get; set; }

  16:  

  17:     public PrincipalForTestAttribute(string userName, string[] roles)

  18:     {

  19:         UserName = userName;

  20:         Roles = roles;

  21:     }

  22: }

All the dirty work is made by the PrincipalForTestHelper.

   1: public class PrincipalForTestHelper : ITestHelper

   2: {

   3:    public String UserName { get; set; }

   4:  

   5:    public String[] Roles { get; set; }

   6:  

   7:    public void FixtureSetUp(BaseTestFixture fixture)

   8:    {

   9:       

  10:    }

  11:    public void SetUp(BaseTestFixture fixture)

  12:    {

  13:        IPrincipal currentPrincipal = Thread.CurrentPrincipal;

  14:        GenericIdentity identity = new GenericIdentity(UserName);

  15:        GenericPrincipal newPrincipal = new GenericPrincipal(identity, Roles);

  16:        Thread.CurrentPrincipal = newPrincipal;

  17:        fixture.ExecuteAtTheEndOfTest(() => Thread.CurrentPrincipal = currentPrincipal);

  18:    }

Now my problem is how to be able to execute code of this helper only for the test where this attribute is applied to, and the solution is available only with Nunit 2.5.7 or above, because the concept of TestContext permits me to know test method name from the SetUp method.

First of all in the constructor of the test class I need to scan all methods looking for ITestHelperAttribute

   1: foreach (MethodInfo methodInfo in type.GetMethods())

   2: {

   3:     var customMethodAttributes = methodInfo.GetCustomAttributes(true);

   4:     IOrderedEnumerable<ITestHelperAttribute> testHelperAttributes =

   5:         customMethodAttributes.OfType<ITestHelperAttribute>()

   6:         .OrderBy(th => th.Order);

   7:     if (testHelperAttributes.Count() > 0)

   8:     {

   9:         List<ITestHelper>  helpers = new List<ITestHelper>();

  10:         MethodHelpers.Add(methodInfo.Name, helpers);

  11:         foreach (ITestHelperAttribute attribute in testHelperAttributes)

  12:         {

  13:             helpers.Add(attribute.Create());

  14:         }

  15:     }

  16: }

Basically I’m creating all the test helpers for each method, now during test setup I’m able to verify the name of the test that is going to be executed, and call the appropriate helpers if they are present.

   1: protected override void OnSetUp ( )

   2: {

   3:     foreach ( ITestHelper helper in Helpers ) {

   4:         helper.SetUp ( this );

   5:     }

   6:     if (MethodHelpers.ContainsKey(TestContext.CurrentContext.Test.Name))

   7:     {

   8:         foreach (ITestHelper helper in MethodHelpers[TestContext.CurrentContext.Test.Name])

   9:         {

  10:             helper.SetUp(this);

  11:         }

  12:     }

  13:     base.OnSetUp ( );

  14: }

In the first foreach I’m calling SetUp() method for every helper associated with the fixture (it should be executed for each test) and then I verify if the MethodHelpers dictionary contains some test helpers for the current test. The key here is that I’m able to know the test method name from TestContext.CurrentContext.Test.Name thanks to new Nunit’s functionality.

In order for this to work with TestDriven.Net you need to copy latest nunit testrunner in the appropriate Testdriven.net Folder (C:\Program Files (x86)\TestDriven.NET 3\NUnit\2.5)

Alk.

Published by

Ricci Gian Maria

.Net programmer, User group and community enthusiast, programmer - aspiring architect - and guitar player :). Visual Studio ALM MVP

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.