开发者

How to Make sure that a particular method in a class is always the first being called?

I'm not too sure whether this is doable or not.

Currently I need to set the license information before the start of my test code, because these tests use a third party component librar开发者_如何学运维y that requires me to initialize the license information. The license information needs to be set only once-- at the start of the running of the program.

But from what I understand, the philosophy of NUnit is that you can run a class, any class at any time, in any sequence, so although there is a setup method for each class, there isn't a setup method that runs at the start of the test for a test suite. But I may be wrong.

Is there anyway to make sure that a method is always the first being called during the whole test execution process?


Have a look at the SetupFixture attribute. You can define one setup function to run once for an entire namespace.


Generally I try to avoid setup and teardowns whenever possible. They tend to indicate (teardowns especially) that I am writing integration tests when I should be doing unit tests. However, I am known to your situation. When this is needed, I derive the testfixture from a class that handles the licensing.

public class LicencedTestFixture
{
    private LicenseComponent _licenseComponent;

    public LicencedTestFixture()
    {
        _licenseComponent = new LicenseComponent();
        _licenseComponent.Init();
    }

    ~LicencedTestFixture()
    {
        _licenseComponent.Shutdown();
    }

}  

The test fixture just inherrit from the class, and that is all it needs to know:

[TestFixture]
public sealed class SomeTestFixture: LicencedTestFixture 
{}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜