什么是 xUnit 运行设置等价物? [英] What's an xUnit runsettings equivalent?

查看:34
本文介绍了什么是 xUnit 运行设置等价物?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们使用 MSTest 时,我们有几个环境有自己的运行设置.由于 Microsoft 正在放弃 MSTest,我们将转向 xUnit.无论是通过运行设置还是命令行属性,我都需要一种在 xUnit 测试中指定 TestRunParameters 的方法.xUnit 是否有像 MSTest 这样的本地方式来做到这一点,还是我需要提出自己的解决方案?

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?

推荐答案

虽然在使用 xUnit 时您仍然可以使用 RunSettings 来控制 vstest.console 的某些方面,但当前版本没有本地方式来传递参数.我相信 v3 会有某种参数传递.

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.

我使用一个基类,它读入包含该测试集设置的 TestSettings.json 文件.使用以下代码,我可以传入新类型并让基类 json 读取器读取它们.

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; }
    }

您也可以使用 Class 或 AssemblyFixture 进行相同类型的测试.

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 ...
}

  • https://xunit.net/docs/shared-context
  • 这篇关于什么是 xUnit 运行设置等价物?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆