Skip to content

Instantly share code, notes, and snippets.

@leachdaniel
Created May 23, 2019 02:15
Show Gist options
  • Save leachdaniel/def1abee97add7fae0791460af8a51e2 to your computer and use it in GitHub Desktop.
Save leachdaniel/def1abee97add7fae0791460af8a51e2 to your computer and use it in GitHub Desktop.
MSTest Configure RunSettings

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment