Are there non-static equivalents to MSTest's [ClassCleanup] & [ClassInitialize]?
Asked Answered
H

1

14

Are there non-static equivalents to MSTest's [ClassCleanup] & [ClassInitialize]?

I am using MSTest for some system/integration level tests, and I don't want to have to worry about cleaning & initializing the connection in the tests.

Example Code:

[TestClass]
public class DefectCreatorTest
{
    private long _cookie;
    private soapcgi _soap;

    [ClassInitialize]
    public void Initialize()
    {
        _soap = new soapcgi {Url = "http://localhost:80/scripts/soapcgi.exe"};
        _cookie = Transaction.Login(_soap);
    }

    [ClassCleanup]
    public void TearDown()
    {
        Transaction.Logout(_cookie, _soap);
    }

    [TestMethod]
    public void CreateDefectTest()
    {
        var result = _soap.Foo();
        Assert.AreEqual("bar", result);
    }
}
Hedwighedwiga answered 19/1, 2012 at 19:51 Comment(3)
What do you mean by non-static? what's wrong with your example code?Counterfeit
The example code compiles, but will not run. The method decorated with ClassInitialize needs to be a static method.Hedwighedwiga
The test would not run because the Initialize method needed to have the TestContext passed into the setup method, even if it's not used.Hedwighedwiga
C
7

Answering your question, as far as i know - No, there isn't a non-static equivalent in MSTest.

But:

  1. looking at your code, there is no reason why _cookie and _soap shouldn't be made static, along with Initialize and TearDown. Doing so will let you forget the configuration worries during test writing...
  2. If you are willing to "jump ship", The NUnit equivalents for [ClassCleanup] & [ClassInitialize] can be applied to static & instance methods.
Counterfeit answered 23/1, 2012 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.