Skip to content

Instantly share code, notes, and snippets.

@ryankelley
Created August 24, 2015 21:28
Show Gist options
  • Save ryankelley/ff555f11f53080756216 to your computer and use it in GitHub Desktop.
Save ryankelley/ff555f11f53080756216 to your computer and use it in GitHub Desktop.
ContextSpecification
using NUnit.Framework;
namespace Inofile.Kno2
{
[TestFixture]
public abstract class ContextSpecification
{
/// <summary>
/// Runs before every test
/// </summary>
[SetUp]
public void MainSetup()
{
SetContext();
Because();
if (_hasExecuted) return;
BecauseOnce();
_hasExecuted = true;
}
/// <summary>
/// Runs after every test
/// </summary>
[TearDown]
protected void MainTeardown()
{
CleanUp();
}
[TestFixtureSetUp]
public void FixtureSetup()
{
SetupFixtureContext();
}
private bool _hasExecuted;
/// <summary>
/// SetupFixtureContext executes one time, before<see cref="Because" />, <see cref="SetContext" />, and
/// <see
/// cref="BecauseOnce" />
/// </summary>
protected virtual void SetupFixtureContext()
{
}
/// <summary>
/// SetContext executes before every test, before <see cref="Because" />
/// </summary>
protected virtual void SetContext()
{
}
/// <summary>
/// Cleanup executes after every test
/// </summary>
protected virtual void CleanUp()
{
}
/// <summary>
/// Because executes before every test, after <see cref="SetContext" />
/// </summary>
protected virtual void Because()
{
}
/// <summary>
/// BecauseOnce executes once for all tests in a fixture, after <see cref="SetContext" />
/// </summary>
protected virtual void BecauseOnce()
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment