|
using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
namespace UTCore
{
public class TestFixture
{
private TestFixtureAttribute tfa=null; // one per TF
private SetUpAttribute sua=null; // one per TF
private TearDownAttribute tda=null; // one per TF
private ArrayList testList=null; // many per TF
public bool HasTestFixture
{
get
{
return tfa != null;
}
}
public Assembly Assembly
{
get
{
return tfa.TestClass.Assembly;
}
}
public string Namespace
{
get
{
return tfa.TestClass.Namespace;
}
}
public ArrayList Tests
{
get
{
return testList;
}
}
public int NumTests
{
get
{
return testList.Count;
}
}
public TestFixture()
{
testList=new ArrayList();
}
public void AddTestFixtureAttribute(TestFixtureAttribute tfa)
{
this.tfa=tfa;
}
public void AddSetUpAttribute(SetUpAttribute sua)
{
if (this.sua==null)
{
this.sua=sua;
}
else
{
Trace.WriteLine("Multiple setup methods are not allowed.");
}
}
public void AddTearDownAttribute(TearDownAttribute tda)
{
if (this.tda==null)
{
this.tda=tda;
}
else
{
Trace.WriteLine("Multiple teardown methods are not allowed.");
}
}
public void AddTestAttribute(TestAttribute ta)
{
testList.Add(ta);
}
// *****
public void RunTests(TestNotificationDelegate testNotificationEvent)
{
object instance=tfa.CreateClass();
foreach (TestAttribute ta in testList)
{
if (!ta.IgnoreTest())
{
try
{
if (sua != null) sua.Invoke(instance);
ta.Invoke(instance);
// If we get here, the test did not throw an exception.
// Was it supposed too?
if (ta.ExpectedExceptionType != null)
{
Trace.WriteLine("***Fail***: "+ta.TestMethod.ToString()+" Expected exception not encountered");
ta.State=TestAttribute.TestState.Fail;
}
else
{
Trace.WriteLine("***Pass***: "+ta.TestMethod.ToString());
ta.State=TestAttribute.TestState.Pass;
}
}
catch(UnitTest.AssertionException e)
{
Trace.WriteLine("***Fail***: "+ta.TestMethod.ToString()+" Exception="+e.Message);
ta.State=TestAttribute.TestState.Fail;
}
catch(Exception e)
{
if (e.GetType() != ta.ExpectedExceptionType)
{
Trace.WriteLine("***Fail***: "+ta.TestMethod.ToString()+" Exception="+e.Message);
ta.State=TestAttribute.TestState.Fail;
}
else
{
Trace.WriteLine("***Pass***: "+ta.TestMethod.ToString()+" Exception="+e.Message);
ta.State=TestAttribute.TestState.Pass;
}
}
finally
{
if (tda != null) tda.Invoke(instance);
}
}
else
{
Trace.WriteLine("***Ignore***: "+ta.TestMethod.ToString());
ta.State=TestAttribute.TestState.Ignore;
}
testNotificationEvent(ta);
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here