Make test result and test info avaiable to TestFixtureSetUp and TestFixtureTearDown methods

This commit is contained in:
Charlie Poole 2012-09-25 09:39:42 -07:00
Родитель 9d2f6eb762
Коммит 354e6fd61b
6 изменённых файлов: 268 добавлений и 37 удалений

Просмотреть файл

@ -11,7 +11,14 @@ namespace NUnit.Core
{
public class ContextDictionary : Hashtable
{
internal TestExecutionContext _ec;
internal TestExecutionContext _context;
public ContextDictionary() { }
public ContextDictionary(TestExecutionContext context)
{
_context = context;
}
public override object this[object key]
{
@ -22,18 +29,18 @@ namespace NUnit.Core
switch (key as string)
{
case "Test.Name":
return _ec.CurrentTest.TestName.Name;
return _context.CurrentTest.TestName.Name;
case "Test.FullName":
return _ec.CurrentTest.TestName.FullName;
return _context.CurrentTest.TestName.FullName;
case "Test.Properties":
return _ec.CurrentTest.Properties;
return _context.CurrentTest.Properties;
case "Result.State":
return (int)_ec.CurrentResult.ResultState;
return (int)_context.CurrentResult.ResultState;
case "TestDirectory":
return AssemblyHelper.GetDirectoryName(_ec.CurrentTest.FixtureType.Assembly);
return AssemblyHelper.GetDirectoryName(_context.CurrentTest.FixtureType.Assembly);
case "WorkDirectory":
return _ec.TestPackage.Settings.Contains("WorkDirectory")
? _ec.TestPackage.Settings["WorkDirectory"]
return _context.TestPackage.Settings.Contains("WorkDirectory")
? _context.TestPackage.Settings["WorkDirectory"]
: Environment.CurrentDirectory;
default:
return base[key];

Просмотреть файл

@ -9,6 +9,7 @@ using System.Configuration;
using System.IO;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.Remoting.Messaging;
using System.Security.Principal;
using System.Threading;
@ -111,6 +112,12 @@ namespace NUnit.Core
/// </summary>
public TestExecutionContext prior;
/// <summary>
/// Context dictionary used to provide test access
/// to this TestExecutionContext
/// </summary>
private ContextDictionary contextDictionary;
#endregion
#region Constructors
@ -133,6 +140,8 @@ namespace NUnit.Core
this.currentCulture = CultureInfo.CurrentCulture;
this.currentUICulture = CultureInfo.CurrentUICulture;
this.currentPrincipal = Thread.CurrentPrincipal;
this.contextDictionary = new ContextDictionary(this);
}
/// <summary>
@ -158,6 +167,8 @@ namespace NUnit.Core
this.currentCulture = CultureInfo.CurrentCulture;
this.currentUICulture = CultureInfo.CurrentUICulture;
this.currentPrincipal = Thread.CurrentPrincipal;
this.contextDictionary = new ContextDictionary(this);
}
#endregion
@ -386,7 +397,8 @@ namespace NUnit.Core
/// </summary>
public static void Save()
{
TestExecutionContext.current = new TestExecutionContext(current);
current = new TestExecutionContext(current);
CallContext.SetData("NUnit.Framework.TestContext", current.contextDictionary);
}
/// <summary>
@ -397,6 +409,7 @@ namespace NUnit.Core
{
current.ReverseChanges();
current = current.prior;
CallContext.SetData("NUnit.Framework.TestContext", current.contextDictionary);
}
#endregion

Просмотреть файл

@ -31,8 +31,6 @@ namespace NUnit.Core
{
static Logger log = InternalTrace.GetLogger(typeof(TestMethod));
static ContextDictionary context;
#region Fields
/// <summary>
/// The test method
@ -101,20 +99,6 @@ namespace NUnit.Core
}
#endregion
#region Static Properties
private static ContextDictionary Context
{
get
{
if (context==null)
{
context = new ContextDictionary();
}
return context;
}
}
#endregion
#region Properties
public override string TestType
@ -224,11 +208,6 @@ namespace NUnit.Core
TestExecutionContext.CurrentContext.CurrentTest = this;
ContextDictionary context = Context;
context._ec = TestExecutionContext.CurrentContext;
CallContext.SetData("NUnit.Framework.TestContext", context);
if (this.Parent != null)
{
this.Fixture = this.Parent.Fixture;

Просмотреть файл

@ -13,6 +13,7 @@ namespace NUnit.Core
using System.Collections;
using System.Reflection;
using NUnit.Core.Filters;
using System.Runtime.Remoting.Messaging;
#if CLR_2_0 || CLR_4_0
using System.Collections.Generic;
@ -107,7 +108,7 @@ namespace NUnit.Core
}
#endregion
#region Public Methods
#region Public Methods
public void Sort()
{
if (!maintainTestOrder)
@ -285,6 +286,8 @@ namespace NUnit.Core
{
TestExecutionContext.Save();
TestExecutionContext.CurrentContext.CurrentTest = this;
try
{
return ShouldRunOnOwnThread
@ -300,6 +303,7 @@ namespace NUnit.Core
public TestResult RunSuite(EventListener listener, ITestFilter filter)
{
TestResult suiteResult = new TestResult(this);
TestExecutionContext.CurrentContext.CurrentResult = suiteResult;
DoOneTimeSetUp(suiteResult);
#if CLR_2_0 || CLR_4_0

Просмотреть файл

@ -12,9 +12,142 @@ using NUnit.TestUtilities;
namespace NUnit.Core.Tests
{
[TestFixture]
[TestFixture][Property("Question", "Why?")]
public class TestContextTests
{
private TestContext fixtureContext;
private TestContext setupContext;
[TestFixtureSetUp]
public void FixtureSetUp()
{
fixtureContext = TestContext.CurrentContext;
}
[TestFixtureTearDown]
public void FixtureTearDown()
{
// TODO: We put some tests in one time teardown to verify that
// the context is still valid. It would be better if these tests
// were placed in a second-level test, invoked from this test class.
TestContext context = TestContext.CurrentContext;
Assert.That(context.Test.Name, Is.EqualTo("TestContextTests"));
Assert.That(context.Test.FullName,
Is.EqualTo("NUnit.Core.Tests.TestContextTests"));
Assert.That(context.Test.Properties["Question"], Is.EqualTo("Why?"));
// The following assert cannot fail, but we use it to make
// sure that the result can be accessed in TestFixtureTearDown.
Assert.NotNull(context.Result.State);
}
[SetUp]
public void SetUpMethod()
{
setupContext = TestContext.CurrentContext;
}
[TearDown]
public void TearDownMethod()
{
Assert.That(
TestContext.CurrentContext.Test.FullName,
Is.EqualTo(setupContext.Test.FullName),
"Context at TearDown failed to match that saved from SetUp");
// The following assert cannot fail, but we use it to make
// sure that the result can be accessed in TearDown.
Assert.NotNull(TestContext.CurrentContext.Result.State);
}
[Test]
public void FixtureSetUpCanAccessFixtureName()
{
Assert.That(fixtureContext.Test.Name, Is.EqualTo("TestContextTests"));
}
[Test]
public void FixtureSetUpCanAccessFixtureFullName()
{
Assert.That(fixtureContext.Test.FullName,
Is.EqualTo("NUnit.Core.Tests.TestContextTests"));
}
[Test]
public void FixtureSetUpCanAccessFixtureResultState()
{
Assert.That(fixtureContext.Result.State, Is.EqualTo(TestState.Success));
Assert.That(fixtureContext.Result.Status, Is.EqualTo(TestStatus.Passed));
}
[Test]
[Property("Answer", 37)]
public void FixtureSetUpCanAccessFixtureProperties()
{
Assert.That(fixtureContext.Test.Properties["Question"], Is.EqualTo("Why?"));
}
[Test]
public void FixtureSetUpCanAccessTestDirectory()
{
string testDirectory = fixtureContext.TestDirectory;
Assert.NotNull(testDirectory);
Assert.That(Directory.Exists(testDirectory));
Assert.That(File.Exists(Path.Combine(testDirectory, "nunit.core.tests.dll")));
}
[Test]
public void FixtureSetUpCanAccessWorkDirectory()
{
string workDirectory = fixtureContext.WorkDirectory;
Assert.NotNull(workDirectory);
Assert.That(Directory.Exists(workDirectory), string.Format("Directory {0} does not exist", workDirectory));
}
[Test]
public void SetUpCanAccessTestName()
{
Assert.That(setupContext.Test.Name, Is.EqualTo("SetUpCanAccessTestName"));
}
[Test]
public void SetUpCanAccessTestFullName()
{
Assert.That(setupContext.Test.FullName,
Is.EqualTo("NUnit.Core.Tests.TestContextTests.SetUpCanAccessTestFullName"));
}
[Test]
[Property("Answer", 99)]
public void SetUpCanAccessTestProperties()
{
Assert.That(setupContext.Test.Properties["Answer"], Is.EqualTo(99));
}
[Test]
public void SetUpCanAccessTestResultState()
{
Assert.That(setupContext.Result.State, Is.EqualTo(TestState.Inconclusive));
Assert.That(setupContext.Result.Status, Is.EqualTo(TestStatus.Inconclusive));
}
[Test]
public void SetUpCanAccessTestDirectory()
{
string testDirectory = setupContext.TestDirectory;
Assert.NotNull(testDirectory);
Assert.That(Directory.Exists(testDirectory));
Assert.That(File.Exists(Path.Combine(testDirectory, "nunit.core.tests.dll")));
}
[Test]
public void SetUpCanAccessWorkDirectory()
{
string workDirectory = setupContext.WorkDirectory;
Assert.NotNull(workDirectory);
Assert.That(Directory.Exists(workDirectory), string.Format("Directory {0} does not exist", workDirectory));
}
[Test]
public void TestCanAccessItsOwnName()
{
@ -35,6 +168,13 @@ namespace NUnit.Core.Tests
Assert.That(TestContext.CurrentContext.Test.Properties["Answer"], Is.EqualTo(42));
}
[Test]
public void TestCanAccessItsOwnResultState()
{
Assert.That(TestContext.CurrentContext.Result.State, Is.EqualTo(TestState.Inconclusive));
Assert.That(TestContext.CurrentContext.Result.Status, Is.EqualTo(TestStatus.Inconclusive));
}
[Test]
public void TestCanAccessTestDirectory()
{
@ -91,7 +231,7 @@ namespace NUnit.Core.Tests
Assert.That(fixture.statusList, Is.EqualTo("Inconclusive=>=>Skipped"));
}
[Test, RequiresThread]
[Test, RequiresThread]
public void CanAccessTestContextOnSeparateThread()
{
Assert.That(TestContext.CurrentContext.Test.Name, Is.EqualTo("CanAccessTestContextOnSeparateThread"));

Просмотреть файл

@ -15,21 +15,45 @@ namespace NUnit.Core.Tests
/// <summary>
/// Summary description for TestExecutionContextTests.
/// </summary>
[TestFixture]
[TestFixture][Property("Question", "Why?")]
public class TestExecutionContextTests
{
TestExecutionContext fixtureContext;
TestExecutionContext setupContext;
string currentDirectory;
CultureInfo currentCulture;
CultureInfo currentUICulture;
IPrincipal currentPrincipal;
/// <summary>
[TestFixtureSetUp]
public void OneTimeSetUp()
{
fixtureContext = TestExecutionContext.CurrentContext;
}
[TestFixtureTearDown]
public void OneTimeTearDown()
{
// TODO: We put some tests in one time teardown to verify that
// the context is still valid. It would be better if these tests
// were placed in a second-level test, invoked from this test class.
TestExecutionContext ec = TestExecutionContext.CurrentContext;
Assert.That(ec.CurrentTest.TestName.Name, Is.EqualTo("TestExecutionContextTests"));
Assert.That(ec.CurrentTest.TestName.FullName,
Is.EqualTo("NUnit.Core.Tests.TestExecutionContextTests"));
Assert.That(ec.CurrentTest.Properties["Question"], Is.EqualTo("Why?"));
}
/// <summary>
/// Since we are testing the mechanism that saves and
/// restores contexts, we save manually here
/// </summary>
[SetUp]
public void SaveContext()
{
setupContext = TestExecutionContext.CurrentContext;
currentDirectory = Environment.CurrentDirectory;
currentCulture = CultureInfo.CurrentCulture;
currentUICulture = CultureInfo.CurrentUICulture;
@ -43,9 +67,73 @@ namespace NUnit.Core.Tests
Thread.CurrentThread.CurrentCulture = currentCulture;
Thread.CurrentThread.CurrentUICulture = currentUICulture;
Thread.CurrentPrincipal = currentPrincipal;
}
[Test]
Assert.That(
TestExecutionContext.CurrentContext.CurrentTest.TestName.FullName,
Is.EqualTo(setupContext.CurrentTest.TestName.FullName),
"Context at TearDown failed to match that saved from SetUp");
}
[Test]
public void FixtureSetUpCanAccessFixtureName()
{
Assert.That(fixtureContext.CurrentTest.TestName.Name, Is.EqualTo("TestExecutionContextTests"));
}
[Test]
public void FixtureSetUpCanAccessFixtureFullName()
{
Assert.That(fixtureContext.CurrentTest.TestName.FullName,
Is.EqualTo("NUnit.Core.Tests.TestExecutionContextTests"));
}
[Test]
public void FixtureSetUpCanAccessFixtureProperties()
{
Assert.That(fixtureContext.CurrentTest.Properties["Question"], Is.EqualTo("Why?"));
}
[Test]
public void SetUpCanAccessTestName()
{
Assert.That(setupContext.CurrentTest.TestName.Name, Is.EqualTo("SetUpCanAccessTestName"));
}
[Test]
public void SetUpCanAccessTestFullName()
{
Assert.That(setupContext.CurrentTest.TestName.FullName,
Is.EqualTo("NUnit.Core.Tests.TestExecutionContextTests.SetUpCanAccessTestFullName"));
}
[Test]
[Property("Answer", 42)]
public void SetUpCanAccessTestProperties()
{
Assert.That(setupContext.CurrentTest.Properties["Answer"], Is.EqualTo(42));
}
[Test]
public void TestCanAccessItsOwnName()
{
Assert.That(TestExecutionContext.CurrentContext.CurrentTest.TestName.Name, Is.EqualTo("TestCanAccessItsOwnName"));
}
[Test]
public void TestCanAccessItsOwnFullName()
{
Assert.That(TestExecutionContext.CurrentContext.CurrentTest.TestName.FullName,
Is.EqualTo("NUnit.Core.Tests.TestExecutionContextTests.TestCanAccessItsOwnFullName"));
}
[Test]
[Property("Answer", 42)]
public void TestCanAccessItsOwnProperties()
{
Assert.That(TestExecutionContext.CurrentContext.CurrentTest.Properties["Answer"], Is.EqualTo(42));
}
[Test]
public void SetAndRestoreCurrentDirectory()
{
Assert.AreEqual(currentDirectory, TestExecutionContext.CurrentContext.CurrentDirectory, "Directory not in initial context");