简体   繁体   English

Nunit基类与常见测试

[英]Nunit base class with common tests

I am using nunit to do some tests for some classes. 我正在使用nunit为某些类做一些测试。

There are a couple of operations that are common for all the test classes but require different parameters to work. 所有测试类都有一些常见的操作,但需要使用不同的参数。

So I added the tests in a base class and virtual methods in the base class to provide the parameters. 所以我在基类中添加了测试,在基类中添加了虚方法来提供参数。

In the derived test classes I overriden the virtual methods to provide specific parameters for the tests in the base class. 在派生的测试类中,我重写了虚方法,以便为基类中的测试提供特定的参数。

Now my problem is that I want the tests in the base class to be executed only from the derived classes. 现在我的问题是我希望基类中的测试只能从派生类中执行。 I am currently using the ignore attribute on the base class to ignore the tests but this causes some warnings when the tests are ran and there is a policy that does not allow me to submit the changes to svn if there are a number of ignored tests. 我目前正在使用基类上的ignore属性来忽略测试但是这会在运行测试时导致一些警告,并且如果存在许多被忽略的测试,则存在不允许我将更改提交到svn的策略。

So how can I run the tests from the base class in the derived classes only without using the ignore attribute on the base class. 那么如何在不使用基类的ignore属性的情况下从派生类中的基类运行测试。

You should be able to mark your base class as abstract, this will stop nunit running the tests in that class - meaning you no longer need the ignore attribute. 您应该能够将基类标记为抽象,这将阻止nunit在该类中运行测试 - 这意味着您不再需要ignore属性。

namespace MyTests
{
    [TestFixture]
    public abstract class BaseTestClass
    {
        [Test]
        public void CommonTest()
        {

        }
    }

    [TestFixture]
    public class TestClass1 : BaseTestClass
    {
        [Test]
        public void OtherTest1()
        {

        }
    }

    [TestFixture]
    public class TestClass2 : BaseTestClass
    {
        [Test]
        public void OtherTest2()
        {

        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM