Switching from MS Test to NUnit Testing

So yesterday I decided to switch my test project to use NUnit as it just feels better with using TestDriven.Net and just feels like kludgey in general. I was pleasantly surprised to see that the level of integration between NHibernate and NUnit that any of the queries that NHibernate runs will be output in the GUI for NUnit that will be really useful until it is superseded by Ayende Rahien’s NProf is released.

Among my transition to MS Test I encountered a few issues, the first of which was not being able to get any of my tests to run. No matter which test or group of tests I attempted to run I would receive this error message:

TestCase ‘Repository.Test.RepositoryTest.EmployeeDataProviderTest.CanResolveProvider’
not executed: Invalid TestFixtureSetUp method signature

Which is what I would get in my output window from running the test through TD.NET eventually I was able to trace this back to MS Test declaring it’s TestFixtureSetUp method’s (the ClassInitialize for their attribute) as static. So remember to remove the static keyword from your methods as you convert your property names to NUnit.

The other issue I ran into was I used the MS Test specific [DeploymentItem] attribute to move config files from my config directories into my test bin folders. Since that was no longer an option I went around creating my own file copy helper. The code usage feels a little less clean compared to DeploymentItem but I feel it’s acceptable on it’s own. Feel free offer any input if you think there’s any cleaner way to do this.

public static class DeploymentItem
{
    /// <summary>
    /// Deploys the files from the deployment directory to the target directory        
    /// using the specified file filter after emptying the target directory.
    /// </summary>
    /// <param name="deploymentDir">The deployment dir.</param>
    /// <param name="targetDir">The target dir.</param>
    /// <param name="fileFilter">The file filter.</param>
    public static void DeployFiles(string deploymentDir, string targetDir, string fileFilter)
    {
        if (Directory.Exists(targetDir))
            Directory.Delete(targetDir, true);

        Directory.CreateDirectory(targetDir);

        foreach (string file in Directory.GetFiles(deploymentDir, fileFilter))
        {
            string newFile = Path.Combine(targetDir, Path.GetFileName(file));

            File.Copy(file, newFile);
            File.SetAttributes(newFile, FileAttributes.Normal);
        }
    }

    /// <summary>
    /// Deploys the config deployment directory to the target directory        
    /// after emptying the target directory.
    /// </summary>
    /// <param name="configDir">The config dir.</param>
    /// <param name="targetDir">The target dir.</param>
    public static void DeployConfigs(string configDir, string targetDir)
    {
        DeployFiles(configDir, targetDir, "*.config");
    }
}

And inside my test class this is what my TestFixtureSetup method looks like

[TestFixtureSetUp]
public void TestFixtureSetup()
{
    const string relativeDir = @"..\..\..\..\..\Deploy\Development\";
    string configDir = Path.Combine(Environment.CurrentDirectory, relativeDir);
    string testConfigDir = Path.Combine(Environment.CurrentDirectory, @"Config\");
    DeploymentItem.DeployConfigs(configDir, testConfigDir);
}

I’m not fan of how many times I need to backdir my relative location based off Enviroment.CurrentDirectory at runtime this is the part I dislike most. I’d much rather be able to just figure out where the solution root is at but I couldn’t seem to find anything online that showed a way to do it so if anyone knows a better way than CurrentDirectory + “../../../../etcetcetc” please comment.

BloggingContext.ApplicationInstance.CompleteRequest();

Advertisements

4 thoughts on “Switching from MS Test to NUnit Testing

  1. I can’t believe you had problems with MSTest and NUnit. I am able to jump seemlessly between the two frameworks in Team City and VS. They are the same exact tests. All I did was choose what syntax I wanted to go with and add some using statements at the top.

    #IF NUNIT
    using TestFixutreSetup = [mstest namespace].TestClassInitialize;
    #END IF

    and then add a new build in your environment. I’ll create an example if you need me to.

    In terms of files, maybe you could abstract that away to an interface and mock it up. that way you are not subject to the structure of the file system.

    Yeah Nunit doesn’t create folders, but MSTests does.

  2. Khalid when you switch from MSTest to NUnit the ClassInitalize methods can’t be static anymore. When they’re created by MS Test by default they’re marked static. I’m not sure if it matters to MS test if you remove static off it.

    I suppose you could do the precompiler directives to pick out which attributes syntax to use also.

    I’m not really sure how it’d be possible to mock the config files unless I actually put a layer on top of the configuration manager since this was about deploying like connectionstring.config and etc.

  3. The following member will get you the directory in which the Assembly of the passed in type is originally located

    private static string GetExecutionDirectory(Type typeWhoseAssemblyLivesInTheExecutionDirectory)
    {
    return System.IO.Path.GetDirectoryName(typeWhoseAssemblyLivesInTheExecutionDirectory.Assembly.CodeBase);
    }

    • Bill thanks for posting this, this might be helpful for some others but i don’t think it would help the original case which is deploying content files. All of my assemblies would be in the bin folder which would make this about the same as Environment.CurrentDirectory

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s