What's an xUnit runsettings equivalent?
Asked Answered
B

1

18

We have several environments that had their own run settings when we used MSTest. Since Microsoft is abandoning MSTest we are switching to xUnit. Whether it's through a runsettings or a command line property, I need a way to specify TestRunParameters in my xUnit test. Does xUnit have a native way to do that like MSTest or do I need to come up with my own solution?

Bootee answered 19/7, 2016 at 12:43 Comment(0)
H
0

While you can still use RunSettings to control some aspects of vstest.console while using xUnit the current version does not have a native way to pass in parameters. I believe v3 is going to have some kind of parameter passing.

For now you could use environment variables but if you are running multiple tests sets in parallel on the same system you would have conflicts.

I use a base class which reads in a TestSettings.json file with the settings for that test set. Using the following code I am able to pass in new types and have them read in by the base class json reader.

    /// <inheritdoc />
    /// <summary>
    /// Common TestBase which uses CommonSettingsModel. Use TestBase&lt;T&gt; to override with custom settings Type.
    /// </summary>
    public abstract class TestBase : TestBase<CommonSettingsModel>
    {

    }
    /// <inheritdoc />
    /// <summary>
    /// Common TestBase for loading settings.
    /// </summary>
    /// <typeparam name="T">Type to read TestSettings.json file</typeparam>
    public abstract class TestBase<T> where T : ICommonSettings, new()
    {
        /// <inheritdoc />
        /// <summary>
        ///     Constructor loads Settings T
        /// </summary>
        protected TestBase()
        {
            Settings = SettingsUtil.GetSettings<T>();           
        }

        /// <summary>
        /// Settings T loaded from TestSettings.json
        /// </summary>
        protected T Settings { get; }
    }

You could also do the same type of thing with a Class or AssemblyFixture for the tests.

public class DatabaseFixture : IDisposable
{
    public DatabaseFixture()
    {
        Db = new SqlConnection("MyConnectionString");

        // ... initialize data in the test database ...
    }

    public void Dispose()
    {
        // ... clean up test data from the database ...
    }

    public SqlConnection Db { get; private set; }
}

public class MyDatabaseTests : IClassFixture<DatabaseFixture>
{
    DatabaseFixture fixture;

    public MyDatabaseTests(DatabaseFixture fixture)
    {
        this.fixture = fixture;
    }

    // ... write tests, using fixture.Db to get access to the SQL Server ...
}
Healthful answered 19/5, 2020 at 13:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.