used in multiple [TestFixture]s

 ̄綄美尐妖づ 提交于 2019-12-23 03:19:05

问题


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]?


回答1:


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.




回答2:


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.




回答3:


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);
    }
}
}



回答4:


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);
    }
}


来源:https://stackoverflow.com/questions/3557904/used-in-multiple-testfixtures

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!