Share via


GeneratedTestClassBase Class

Provides an abstract base class for test classes generated by Spec Explorer.

Namespace: Microsoft.SpecExplorer.Runtime.Testing
Assembly: Microsoft.SpecExplorer.Runtime (in Microsoft.SpecExplorer.Runtime.dll)

Usage

'Usage

Syntax

'Declaration
public abstract class GeneratedTestClassBase : IBasicTestSite, IConfigurableGeneratedTestClass, IGeneratedTestClass

Example

The following Cord script and C# example shows an implementation of a custom test class, NUnitTestClassBase, that supports the NUnit 2.5 test environment.

In the Cord script, the Main configuration block sets some of the test generation switches, in particular, the TestClassBase switch, which indicates which base test class Spec Explorer should use in the generated test code; and the six test class and test method attribute switches, which indicate which attributes Spec Explorer should apply to generated test class and test case methods.

In the C# code, the NUnitTestClassBase class extends the GeneratedTestClassBase class and overrides the abstract methods of the GeneratedTestClassBase class: the BeginTest, EndTest, Assert, Assume, Checkpoint, and Comment methods.

config Main 
{
    switch GeneratedTestPath = "..\\NUnitEnvironment.TestSuite";
    switch GeneratedTestNamespace = "NUnitEnvironment.TestSuite";

    switch TestClassBase = "NUnitEnvironment.TestSuite.NUnitTestClassBase";
    switch TestMethodReturnType = none;

    switch TestClassAttribute =       "NUnit.Framework.TestFixtureAttribute";
    switch ClassInitializeAttribute = "NUnit.Framework.TestFixtureSetUpAttribute";
    switch ClassCleanupAttribute =    "NUnit.Framework.TestFixtureTearDownAttribute";
    switch TestMethodAttribute =      "NUnit.Framework.TestAttribute";
    switch TestInitializeAttribute =  "NUnit.Framework.SetUpAttribute";
    switch TestCleanupAttribute =     "NUnit.Framework.TearDownAttribute";

    // Define actions and set other switches here.
}
using System;
using System.IO;
using Microsoft.SpecExplorer.Runtime.Testing;

namespace NUnitEnvironment.TestSuite
{
    public class NUnitTestClassBase : GeneratedTestClassBase, IDisposable
    {
        private StreamWriter logWriter;
        private bool disposed = false;

        public NUnitTestClassBase()
        {
            // Set the LogToFile property as appropriate.
        }

        public override void Assert(bool condition, string description)
        {
            NUnit.Framework.Assert.IsTrue(condition, description);
        }

        public override void Assume(bool condition, string description)
        {
            NUnit.Framework.Assume.That(condition, description);
        }

        public override void BeginTest(string name)
        {
            string message = "Begin executing test " + name;
            Console.WriteLine(message);
            if (LogToFile)
            {
                OpenLogFile(name, message);
            }
        }

        public override void Checkpoint(string description)
        {
            string message = "Reaching checkpoint: " + description;
            Console.WriteLine(message);
            if (LogToFile && logWriter != null)
            {
                logWriter.WriteLine(message);
            }
        }

        public override void Comment(string description)
        {
            Console.WriteLine(description);
            if (LogToFile && logWriter != null)
            {
                logWriter.WriteLine(description);
            }
        }

        public override void EndTest()
        {
            string message = "End executing test";
            Console.WriteLine(message);
            if (LogToFile && logWriter != null)
            {
                logWriter.WriteLine(message);
                CloseLogFile();
            }
        }

        private void OpenLogFile(string name, string message)
        {
            string fullPath =
                Path.Combine(Directory.GetCurrentDirectory(), name) + ".log";
            try
            {
                logWriter = new StreamWriter(fullPath, false);
                logWriter.AutoFlush = true;
                logWriter.WriteLine(message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("{0} opening or writing to log file {1}.",
                    ex.GetType().Name, fullPath);
                Console.WriteLine(message);
                CloseLogFile();
            }
        }

        private void CloseLogFile()
        {
            if (logWriter != null)
            {
                try
                {
                    logWriter.Close();
                }
                finally
                {
                    logWriter = null;
                }
            }
        }

        public void Dispose()
        {
            disposed = true;
            CloseLogFile();
        }
    }
}

Remarks

During test code generation and on-the-fly testing, Spec Explorer extends the test class specified by the TestClassBase switch. The VsTestClassBase class provides a base test class that is suitable for the Visual Studio testing tools environment. In general, to create a base test class for a testing environment outside Visual Studio, define a custom base test class that extends the GeneratedTestClassBase class.

To create a base test class for a test environment that requires the test class to extend a specific base class, write the custom base test class such that it extends the required base class and implements the IGeneratedTestClass and IConfigurableGeneratedTestClass interfaces. Optionally, the custom base test class can implement the IBasicTestSite interface.

Notes to Inheritors: When you inherit from the GeneratedTestClassBase class, you must override the following members: BeginTest, EndTest, Assert, Assume, Checkpoint, and Comment.

Inheritance Hierarchy

System.Object
  Microsoft.SpecExplorer.Runtime.Testing.GeneratedTestClassBase
     Microsoft.SpecExplorer.Runtime.Testing.VsTestClassBase

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Platforms

Development Platforms

Microsoft Windows 7, Microsoft Windows Vista, Microsoft Windows XP SP2 or later, Microsoft Windows Server 2008, Microsoft Windows Server 2003

Change History

See Also

Reference

GeneratedTestClassBase Members
Microsoft.SpecExplorer.Runtime.Testing Namespace
VsTestClassBase
IGeneratedTestClass
IConfigurableGeneratedTestClass
IBasicTestSite