1

If I have a class with static Facts (test methods) and the class has a static constructor, is the constructor called for each Fact or only once for all Facts in a class? I guess it depends on how the runner loads/unloads test classes?

Suraj
  • 35,905
  • 47
  • 139
  • 250

2 Answers2

2

Out of experience, I know that it is only called once for the class. It is the same if you use a static class (i.e. settings class) in your non-static tests (facts). The static object constructor is only called once for the whole test class.

Teoman Soygul
  • 25,584
  • 6
  • 69
  • 80
  • thanks! while we're on the topic, any idea of what happens with non-static methods/constructors? Is the class instantiated once and then each method is called or are there separate instantiations per method? – Suraj Jun 03 '11 at 19:05
  • 2
    The class in which the tests reside is constructor then destructed once for each test. So if there are 10 facts in a class, it will be instantiated 10 times then destructed 10 times. Any non-static object will be recreated. Any static object will be re-used. – Teoman Soygul Jun 03 '11 at 19:30
0

If anything (eg. Facts) use static method or class first time that static constructor is called ones. If there are no references to the static class the static constructor is not called at all. If you are considering to run code before and after the tests (setup/dispose) ONES I recommend to use Fixtures (see https://xunit.net/docs/shared-context).

ADM-IT
  • 3,719
  • 1
  • 25
  • 26