多[的TestFixture] S使用 [英] used in multiple [TestFixture]s

查看:148
本文介绍了多[的TestFixture] S使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在NUnit的建立测试和有新手的问题的过程。

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

是否有可能有一个测试/秒,可以在使用多[的TestFixture] S?

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

所以,
[测试] ValidateString(字符串BOB)

So [Test]ValidateString(string bob)

莫非在一系列不同[的TestFixture]

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

推荐答案

这听起来不像一个测试给我。测试通常参(除非你使用 [TestCase的] S)和一个固定的范围内运行它通常就足够了 - 它要么通过一次,这是好它不,它是一个破碎的测试。

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.

如果你只是有,做一个字符串一些验证的方法,你可以将其设置为一个静态方法对某一类(如 TestHelpers ),并从任何测试(在多个测试设备)需要调用它。

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.

下面是另一个想法:继承。你可以拥有所有的测试,然后从它继承该设置任何你需要的变量夹具底座固定。该测试将每台灯上运行。我不熟悉使用Selenium RC,但你应该能够适应下面的代码来设置你的各种灯具需要的任何变量。

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.

这篇关于多[的TestFixture] S使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆