测试失败后如何执行代码

时间:2022-08-24 23:05:54

I am Creating Unit Tests for a Library. This Library Connects to a Datasource and then I am doing some testing Stuff afterwards the Datasource will be disconnected.

我正在为图书馆创建单元测试。这个库连接到数据源然后我正在做一些测试Stuff之后数据源将被断开连接。

If one of the Tests fails, the Method is Terminated and I dont get to execute the Disconnection Function.

如果其中一个测试失败,则方法终止,我无法执行断开连接功能。

Here's a Sample to understande the above description:

这是一个样本,以说明以上描述:

[TestMethod]
public void Test()
{
    var datasourceObject = new DatasourceObject("location-string");
    datasourceObject.Connect();

    // Do some Stuff with Asserts

    datasourceObject.Disconnect(); // must be executed
}

Is There any Bestpractice to achieve that?

1 个解决方案

#1


If you use resource in other tests, then move it to class fields and use [TestInitialize] and [TestCleanup] to get and free that resource:

如果您在其他测试中使用资源,则将其移至类字段并使用[TestInitialize]和[TestCleanup]获取并释放该资源:

private Foo datasourceObject;

[TestInitialize]
public void TestInitialize()
{
    this.datasourceObject = new DatasourceObject("location-string");
    this.datasourceObject.Connect();
}

[TestMethod]
public void Test()
{
    // Do some Stuff with Asserts
}

[TestCleanup]
public void TestCleanup()
{
    this.datasourceObject.Disconnect();
}

If you use resource in this test only, then use either try..finally

如果仅在此测试中使用资源,则使用try..finally

[TestMethod]
public void Test()
{
    try
    {
        var datasourceObject = new DatasourceObject("location-string");
        datasourceObject.Connect();
        // Do some Stuff with Asserts
    }
    finally
    {
        datasourceObject.Disconnect(); // must be executed
    }
}

Or using statement if resource is disposable:

如果资源是一次性的,请使用声明:

[TestMethod]
public void Test()
{
    using(var datasourceObject = new DatasourceObject("location-string"))
    {
        datasourceObject.Connect();
        // Do some Stuff with Asserts
    }
}

#1


If you use resource in other tests, then move it to class fields and use [TestInitialize] and [TestCleanup] to get and free that resource:

如果您在其他测试中使用资源,则将其移至类字段并使用[TestInitialize]和[TestCleanup]获取并释放该资源:

private Foo datasourceObject;

[TestInitialize]
public void TestInitialize()
{
    this.datasourceObject = new DatasourceObject("location-string");
    this.datasourceObject.Connect();
}

[TestMethod]
public void Test()
{
    // Do some Stuff with Asserts
}

[TestCleanup]
public void TestCleanup()
{
    this.datasourceObject.Disconnect();
}

If you use resource in this test only, then use either try..finally

如果仅在此测试中使用资源,则使用try..finally

[TestMethod]
public void Test()
{
    try
    {
        var datasourceObject = new DatasourceObject("location-string");
        datasourceObject.Connect();
        // Do some Stuff with Asserts
    }
    finally
    {
        datasourceObject.Disconnect(); // must be executed
    }
}

Or using statement if resource is disposable:

如果资源是一次性的,请使用声明:

[TestMethod]
public void Test()
{
    using(var datasourceObject = new DatasourceObject("location-string"))
    {
        datasourceObject.Connect();
        // Do some Stuff with Asserts
    }
}