tags:

views:

54

answers:

4

I'm in the process of setting up tests in NUnit and have a newbie question.

Is it possible to have a Test/s that could be used in multiple [TestFixture]s?

So [Test]ValidateString(string bob)

Could be called in a series of different [TestFixture]?

+5  A: 

That doesn't sound like a test to me. Tests are typically parameterless (unless you're using [TestCase]s) and running it within a context of a single fixture would typically be enough -- it either passes once and that's good or it doesn't and it's a broken test.

If you just have a method that does some validation on a string, you could set it up as a static method on some class (e.g. TestHelpers) and call it from whatever tests (in multiple test fixtures) need it.


Here's another idea: inheritance. You can have a base fixture that has all your tests, and then fixtures that inherit from it that set up whatever variables you need. The tests will run for each fixture. I'm not familiar with Selenium RC, but you should be able to adapt the code below to set up whatever variables you need in various fixtures.

[TestFixture]
public class BaseFixtureTests
{
    protected IMyClass _myClass;

    [TestFixtureSetUp]
    public void FixtureSetup() 
    {
        _myClass = ConfigureMyClass();
    }

    protected virtual IMyClass ConfigureMyClass() 
    {
        // fixtures that inherit from this will set up _myClass here as they see fit.
    }

    [Test]
    public void MyClassTest1() 
    {
        // test something about _myClass;
    }
}

[TestFixture]
public class MySpecificFixture1 : BaseFixtureTests
{
    protected override IMyClass ConfigureMyClass()
    {
        return new MySpecificMyClassImplementation();
    }   
}

public class MySpecificMyClassImplementation : IMyClass
{
    //some implementation
}

You can also add extra tests in each fixture as well that don't test common functionality and don't need to be reused across fixtures.

Anna Lear
What I am trying to achieve is a set of tests that can be called by a custom UI and also potentially the Nunit test runner. Basically I am using Selenium RC to do regression testing and I need to be able to parameterise which browser and website to use (dev/QA/live etc).So my idea was to build up the tests but have a different testfixture per browser I want to test (so the SetupTest can be configured as required).
DazManCat
Anna Lear
great thanks Anna. Like I say I'm an nUnit noob. So It's quite possible I'm not it the nUnit mindset yet.
DazManCat
No problem. Take a look at my latest edit. Hope it helps. :)
Anna Lear
OIC thats more like what I am after. So it runs a test for each case using the specified parameters.The only problem there is that when I am running tests individually via our test UI I may only want it to run once with a particular set of params.So what I was hoping is that the constituate tests could be setup and simply called from the which ever Fixtures required. (e.g. have one setup for our UI (which uses properties configurable from the UI) and other for the automated test fixtures.
DazManCat
If you want to run with a single set of params, you can run just one of your fixtures that inherit from the base class. So, if you have your Base and then Fixture1 and Fixture2, just run tests from Fixture1 and that should do what you want.
Anna Lear
Sorry to be a pain, I think I get what you mean but, could you give me an example?
DazManCat
I'm not familiar with your test UI, so I'm gonna use the nUnit runner as an example. When you load up your test assembly (the dll that has your tests in it), you'll see a list of fixtures that contain tests. You can either run all of them at once, in which case your base fixture's tests will run for every child fixture. Or you can pick one child fixture and just run its tests -- in this case the base fixture's tests will only execute once with the configuration set in your chosen child fixture.
Anna Lear
A: 

You can write a method to be called from multiple [Test] methods. But I don't think there is a way to have the same [Test] included in multiple [TestFixture]s.

[TestFixture]
public class TestsOne{
    [Test] public void TryOne(){
        Helpers.ValidateString("Work?");
    }
}

[TestFixture]
public class TestsTwo{
    [Test] public void TryTwo(){
        Helpers.ValidateString("Work?");
    }
}

public static class Helpers{
    public static void ValidateString(string s){
        Assert.IsNotNull(s);
    }
}
TheSean
Hmmm perhaps I could have the core of the test in a helper and called from whichever fixture required.
DazManCat
There definitely is - use inheritance. You can place the Test attribute on methods in the base class and the TestFixture attribute on child classes. If you don't want the tests to ever execute on instances of the base class, make it abstract. This is know as the abstract fixture pattern.
imoatama
+1  A: 

You might be able to achieve what you want with inheritance.

using NUnit.Framework;

namespace ClassLibrary1
{
[TestFixture]
public class TestFixtureBase
{
    [SetUp]
    public virtual void Setup()
    {
        // setup code here
    }

    [Test]
    public void CommonTest1()
    {
        Assert.True(true);
    }

    [Test]
    public void CommonTest2()
    {
        Assert.False(false);
    }
}

public class MyClassTests : TestFixtureBase
{
    [SetUp]
    public override void Setup()
    {
        base.Setup();

        // additional setup code
    }

    [Test]
    public void MyClassTest1()
    {
        Assert.True(true);
    }
}
}
Peter
Oops, just noticed Anna's latest edit. That looks good to me. :)
Peter
DazManCat
+1  A: 

The newer version of NUnit supports generics. This is a great fit if what you are testing doesn’t need to be configured (only created) from your test code. Here is an example copied from http://nunit.net/blogs/:

[TestFixture(typeof(ArrayList))]
[TestFixture(typeof(List<int>))]
public class IList_Tests<TList> where TList : IList, new()
{
    private IList list;

    [SetUp]
    public void CreateList()
    {
        this.list = new TList();
    }

    [Test]
    public void CanAddToList()
    {
        list.Add(1); list.Add(2); list.Add(3);
        Assert.AreEqual(3, list.Count);
    }
}

I’ve also used Anna’s approach of inheritance. One possible refinement to her example (depending on personal preference): Don’t mark the base class as a TestFixture, only the child classes. Each class that you mark as a TestFixture will be displayed as a set of tests in the NUnit client. You will probably never want to run the base class methods directly because the child is providing all of the setup code. If you remove TestFixture from the base class, running invalid tests won’t be an option in the UI. This allows you to run all the tests and see all green… always a nice feeling.

ErnieL
+1 for the point about putting (or not) [TestFixture] on the base class. I hadn't thought to try running without it.
Anna Lear