2

Although my current context is SharePoint related this question applies to testing in general where I would like to run the same set of tests but with different parameters.

I have existing tests that test my Sharepoint code (that use the SharePoint Client Object Model) against SharePoint 2010. This is all working fine. A helper method in the base class provides the appropriate url for the SharePoint 2010 instance.

I now want to run these tests against SharePoint 2013 also. I was considering creating another test configuration for SharePoint 2013 and based on the configuration that is being executed I can use the appropriate url. In the method decorated with the 'ClassInitialize' attribute I do not get any information about the testsettings file being used and hence no way to know which configuration is being executed.

How can I achieve what I want - run the same tests against SharePoint 2010 and 2013?

alwayslearning
  • 4,493
  • 6
  • 35
  • 47

1 Answers1

0

Setting Up an MSTest Project to work with Multiple Configurations

Create Some Run Settings Files

Add a file to the solution called config1.RunSettings. Add Parameter(s) to the file to change the configuration.

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
  <TestRunParameters>
    <Parameter name="config" value="config1" />
  </TestRunParameters>
</RunSettings>

Add Some Initialization Code

AssemblyInit will run once before the tests start. Use the to make any configuration changes.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UnitTestProject1
{
    [TestClass]
    public class SetupAssemblyInitializer
    {
        [AssemblyInitialize]
        public static void AssemblyInit(TestContext context)
        {
            // Initalization code goes here
            // https://stackoverflow.com/questions/2382552/is-it-possible-to-execute-code-once-before-all-tests-run

            // set load configuration from runsettings here
            context.Properties["config"];
        }
    }
}

Select Test Settings File Before Each Run

https://github.com/MicrosoftDocs/visualstudio-docs/blob/master/docs/test/media/select-test-settings-file.png

Daniel Leach
  • 5,517
  • 4
  • 18
  • 32