xunit构造函数在每次测试之前运行

时间:2017-10-25 07:51:47

标签: c# unit-testing xunit

在测试类中,构造函数在每个测试之前启动,并且对于当前运行的测试无法访问基类的先前初始化,我只想创建一次基类。可以这样做吗?

[Category("Basics")]
[Collection("DD")]
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public class SecurityTests : TestUnitBase
{
    StartUpFixture fixture;
    public AuthenticationTests(StartUpFixture fixture)
       : base()
    {
        this.fixture = fixture;
    }

    [Fact(DisplayName = "Successful response Test1")]
    public void SuccessfulResponseTest1()
    {
        var users = base.Db.Users.FirstOrDefault(x => x.Name == "...");
        ...
    }

    [Fact(DisplayName = "Successful response Test2")]
    public void SuccessfulResponseTest2()
    {
        var users = base.Db.Users.FirstOrDefault(x => x.Name == "iranian1");
        ...
    }

提前致谢。

1 个答案:

答案 0 :(得分:19)

您可以使用Xunit Class灯具。当使用类fixture时,xUnit.net将确保在任何测试运行之前创建fixture实例,并且一旦所有测试完成,它将通过调用Dispose(如果存在)来清理fixture对象。例如:

//similar to base class
public class DatabaseFixture : IDisposable
{
    public SqlConnection Db { get; private set; }
    public DatabaseFixture()
    {
        Db = new SqlConnection("MyConnectionString");

        // initialize data in the test database
    }

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

//Class where you want to use shared class instance
public class MyDatabaseTests : IClassFixture<DatabaseFixture>
{
    DatabaseFixture dbFixture;

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

    // write tests, using dbFixture.Db to get access to the SQL Server
}

对于每个测试,它将创建MyDatabaseTests的新实例,并将DatabaseFixture的共享实例传递给构造函数。

Xunit文档here

希望它有所帮助。

编辑28/10 对于多个灯具,您可以创建一个类,其中封装其他两个灯具,如下所示,并创建启动夹具,以便在db fixture之前运行:

public class SecurityTestsFixture : IDisposable
{
    public DatabaseFixture Dbfixture { get; private set; }
    public StartupTestFixture Startupfixture { get; private set; }

    public SecurityTestsFixture()
    {
        Startupfixture = new StartupTestFixture();
        Dbfixture = new DatabaseFixture(Startupfixture);
    }

    public void Dispose()
    {
        // clean up code
        Dbfixture.Dispose();
    }

    public class StartupTestFixture
    {
        public string SqlConnString { get; private set; }
        public StartupTestFixture()
        {
            SqlConnString = ConfigurationManager.AppSettings["SqlConnectionString"];

            // other startup code
        }
    }

    public class DatabaseFixture : IDisposable
    {
        public SqlConnection Db { get; private set; }
        public DatabaseFixture(StartupTestFixture sFixture)
        {
            Db = new SqlConnection(sFixture.SqlConnString);

            // initialize data in the test database
        }

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

和测试类:

public class Xunittests : IClassFixture<SecurityTestsFixture>
{
    SecurityTestsFixture _securityFixture;
    public Xunittests(SecurityTestsFixture securityfixture)
    {
        _securityFixture = securityfixture;
    }

    [Fact(DisplayName = "Successful response Test1")]
    public void SuccessfulResponseTest1()
    {
        var db = _securityFixture.Dbfixture.Db;
        //var users = db.Users.FirstOrDefault(x => x.Name == "...");
        Assert.Equal("test", "test");
    }
}