Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

We need some global one time setup code in our test suite. We can do it more than once but it takes quite some time.

  • It's required by all fixtures so [TestFixtureSetUp] does not work. It has to run before all [TestFixtureSetUp] code.

  • Put it in Main() since we keep test assemblies as executables. However Main doesn't get executed under GUI client.

  • Creating a separate class with a static constructor for initialization only works when you reference the class which we do not favor doing in each and every class.

  • Inheriting all test fixtures from a base class and adding a static constructor to it causes multiple calls to the init code.

Now given the circumstances, I have two questions:

1) Is "global setup" a very bad idea that it's not supported by NUnit?

2) What's the least painful, most common way to achieve this?

question from:https://stackoverflow.com/questions/3619735/nunit-global-initialization-bad-idea

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
266 views
Welcome To Ask or Share your Answers For Others

1 Answer

[SetUpFixture]

This is the attribute that marks a class that contains the one-time setup or teardown methods for all the test fixtures under a given namespace.

The SetUp method in a SetUpFixture is executed once before any of the fixtures contained in its namespace. The TearDown method is executed once after all the fixtures have completed execution.

Assembly wide initialization. If you don't put the class in any namespace, it will apply to all tests in the assembly.

eg.

// using statements

[SetUpFixture]
public class GlobalSetup {
  [SetUp]
  public void ShowSomeTrace() {
    Trace.WriteLine("It works..."); // won't actually trace
  }
}

http://www.nunit.org/index.php?p=setupFixture&r=2.4


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...