Skip to content

Commit

Permalink
Merge pull request #786 from ramonsmits/OneTimeSetup-run-only-once-gu…
Browse files Browse the repository at this point in the history
…idance

Use `SetUpFixture` or static contructor to have code only run once 3bd0d9d
  • Loading branch information
OsirisTerje committed Sep 1, 2023
1 parent 63ca210 commit 109f1a8
Show file tree
Hide file tree
Showing 3 changed files with 353 additions and 351 deletions.
2 changes: 2 additions & 0 deletions articles/nunit/writing-tests/attributes/onetimesetup.html
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ <h2 id="notes">Notes</h2>
</li>
<li><p>When using <a class="xref" href="fixturelifecycle.html"><code>FixtureLifeCycle</code></a> with <code>LifeCycle.InstancePerTestCase</code>, the <a class="xref" href="onetimesetup.html"><code>OneTimeSetUp</code></a> method must be static and is only called once. This is required so that the setup method does not access instance fields or properties that are reset for every test.</p>
</li>
<li><p>When set on a base class the method is invoked for each fixture that inherits from this base class and its invoked for the base class too if its not abstract. Use <a class="xref" href="setupfixture.html"><code>SetUpFixture</code></a> if this only needs to be run once or put the code in a static constructor.</p>
</li>
</ul>
<h2 id="see-also">See Also</h2>
<ul>
Expand Down
2 changes: 1 addition & 1 deletion index.json
Original file line number Diff line number Diff line change
Expand Up @@ -1197,7 +1197,7 @@
"articles/nunit/writing-tests/attributes/onetimesetup.html": {
"href": "articles/nunit/writing-tests/attributes/onetimesetup.html",
"title": "OneTimeSetUp | NUnit Docs",
"keywords": "OneTimeSetUp This attribute is to identify methods that are called once prior to executing any of the tests in a fixture. It may appear on methods of a TestFixture or a SetUpFixture. OneTimeSetUp methods may be either static or instance methods and you may define more than one of them in a fixture. Normally, multiple OneTimeSetUp methods are only defined at different levels of an inheritance hierarchy, as explained below. If a OneTimeSetUp method fails or throws an exception, none of the tests in the fixture are executed and a failure or error is reported. Example namespace NUnit.Tests { using System; using NUnit.Framework; [TestFixture] public class SuccessTests { [OneTimeSetUp] public void Init() { /* ... */ } [OneTimeTearDown] public void Cleanup() { /* ... */ } [Test] public void Add() { /* ... */ } } } Inheritance The OneTimeSetUp attribute is inherited from any base class. Therefore, if a base class has defined a OneTimeSetUp method, that method will be called before any methods in the derived class. You may define a OneTimeSetUp method in the base class and another in the derived class. NUnit will call base class OneTimeSetUp methods before those in the derived classes. Warning If a base class OneTimeSetUp method is overridden in the derived class, NUnit will not call the base class OneTimeSetUp method; NUnit does not anticipate usage that includes hiding the base method. Note that you may have a different name for each method; as long as both have the [OneTimeSetUp] attribute present, each will be called in the correct order. Notes Although it is possible to define multiple OneTimeSetUp methods in the same class, you should rarely do so. Unlike methods defined in separate classes in the inheritance hierarchy, the order in which they are executed is not guaranteed. OneTimeSetUp methods may be async if running under .NET 4.0 or higher. OneTimeSetUp methods run in the context of the TestFixture or SetUpFixture, which is separate from the context of any individual test cases. It's important to keep this in mind when using TestContext methods and properties within the method. When using FixtureLifeCycle with LifeCycle.InstancePerTestCase, the OneTimeSetUp method must be static and is only called once. This is required so that the setup method does not access instance fields or properties that are reset for every test. See Also SetUp Attribute TearDown Attribute OneTimeTearDown Attribute FixtureLifeCycle Attribute"
"keywords": "OneTimeSetUp This attribute is to identify methods that are called once prior to executing any of the tests in a fixture. It may appear on methods of a TestFixture or a SetUpFixture. OneTimeSetUp methods may be either static or instance methods and you may define more than one of them in a fixture. Normally, multiple OneTimeSetUp methods are only defined at different levels of an inheritance hierarchy, as explained below. If a OneTimeSetUp method fails or throws an exception, none of the tests in the fixture are executed and a failure or error is reported. Example namespace NUnit.Tests { using System; using NUnit.Framework; [TestFixture] public class SuccessTests { [OneTimeSetUp] public void Init() { /* ... */ } [OneTimeTearDown] public void Cleanup() { /* ... */ } [Test] public void Add() { /* ... */ } } } Inheritance The OneTimeSetUp attribute is inherited from any base class. Therefore, if a base class has defined a OneTimeSetUp method, that method will be called before any methods in the derived class. You may define a OneTimeSetUp method in the base class and another in the derived class. NUnit will call base class OneTimeSetUp methods before those in the derived classes. Warning If a base class OneTimeSetUp method is overridden in the derived class, NUnit will not call the base class OneTimeSetUp method; NUnit does not anticipate usage that includes hiding the base method. Note that you may have a different name for each method; as long as both have the [OneTimeSetUp] attribute present, each will be called in the correct order. Notes Although it is possible to define multiple OneTimeSetUp methods in the same class, you should rarely do so. Unlike methods defined in separate classes in the inheritance hierarchy, the order in which they are executed is not guaranteed. OneTimeSetUp methods may be async if running under .NET 4.0 or higher. OneTimeSetUp methods run in the context of the TestFixture or SetUpFixture, which is separate from the context of any individual test cases. It's important to keep this in mind when using TestContext methods and properties within the method. When using FixtureLifeCycle with LifeCycle.InstancePerTestCase, the OneTimeSetUp method must be static and is only called once. This is required so that the setup method does not access instance fields or properties that are reset for every test. When set on a base class the method is invoked for each fixture that inherits from this base class and its invoked for the base class too if its not abstract. Use SetUpFixture if this only needs to be run once or put the code in a static constructor. See Also SetUp Attribute TearDown Attribute OneTimeTearDown Attribute FixtureLifeCycle Attribute"
},
"articles/nunit/writing-tests/attributes/onetimeteardown.html": {
"href": "articles/nunit/writing-tests/attributes/onetimeteardown.html",
Expand Down
Loading

0 comments on commit 109f1a8

Please sign in to comment.