Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OOP Structure Help 1

Status
Not open for further replies.

beefeater267

Programmer
Apr 6, 2005
79
Hello,

I have some questions regarding how to design this in a proper OO manner. So, I'm designing a win app to perform various tests.... so, i have a baseclass "LoadTest"... and have subclasses : "TestA", "TestB", etc....

So, each test performs the same tasks with variation in the middle as such:

EXAMPLE of procedure:

for (int i=0 ; i<=SOMETHING; i++)
{
some common operations here for all Test objects

operations dependant on subclass type (TestA, TestB, etc)

some common operations here for all Test objects
}

So, how do I structure my methods? I'm confused. I want to put my operations into a method called "RunTest".

Can anyone help me out on how to structure this? I started out by creating :

public virtual void Run() in the LoadTest class and also:
public override void Run() in the TestA class

So, I have a reference to a TestA object... I know I can put all the code of the procedure in EACH subclass , but, i know this is repitiious and there has to be an OO way.

ANY HELP? THANKS 4 SAVING ME!
 
Have you looked at NUnit? It does everything you need, without the need for inheritance.


You create a class to be used for testing your other code, and affix the [TestFixture] attribute to it. For each method within the class which contains a test, you affix a [Test] attribute to it (you can also have non-test methods). It has the ability to run a method prior to all tests (the [TestFixtureSetUp] attribute), before each test (the [SetUp] attribute), as well as cleanup after all or each test ([TestFixtureTearDown] and [TearDown], respectively).

It comes with a GUI to run your tests, as well as a console version, for when you want to run your tests in an automated fashion (like inside a NAnt build script).

In order to prevent excessive clutter of .cs files, I put each class's test in the same .cs file, so it looks like this:
Code:
namespace FooSpace
{
  using System;

  public class Foo
  {
    // Foo stuff here
  }
}

//====================================

namespace FooSpaceTest
{
  using System;
  using NUnit.Framework;
  using FooSpace;
  
  [TestFixture]
  public class FooTest
  {
    [Test]
    public void TestConstructor()
    {
       Foo f = new Foo();
       Assert.IsNotNull(f);
    }
  }
}
Note that it's OK to have multiple namespaces per source file, and it's OK to have your using statements inside the namespace.

Chip H.

____________________________________________________________________
Donate to Katrina relief:
If you want to get the best response to a question, please read FAQ222-2244 first
 
chiph said:
Note that it's OK to have multiple namespaces per source file, and it's OK to have your using statements inside the namespace.

well blow me down. have a star.



mr s. <;)

 
This works because the C# language spec says:
msdn said:
A compilation-unit defines the overall structure of a source file. A compilation unit consists of zero or more using-directives followed by zero or more global-attributes followed by zero or more namespace-member-declarations.

compilation-unit:
using-directives(opt) global-attributes(opt) namespace-member-declarations(opt)
This says a source file can contain an optional using block, optional global attributes block (I've never messed with this one), or multiple namespace directives. Looking at the definition of a namespace directive:
msdn said:
A namespace-declaration consists of the keyword namespace, followed by a namespace name and body, optionally followed by a semicolon.

namespace-declaration:
namespace qualified-identifier namespace-body ;(opt)

qualified-identifier:
identifier
qualified-identifier . identifier

namespace-body:
{ using-directives(opt) namespace-member-declarations(opt) }
The body of a namespace can thus have multiple using directives, or additional namespace declarations (allows you to nest your namespaces within the same source file).

Chip H.

____________________________________________________________________
Donate to Katrina relief:
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top