-1

我有以下代码:

File1.cs 包含:

namespace TestsNM
{
    [SetUpFixture]
    public class MySetUpClass
    {
        [SetUp]
        public void RunBeforeAnyTests()
        {
            //....
        }

        [TearDown]
        public void RunAfterAnyTests()
        {
            //...
        }
    }
}

File2.cs 包含:

namespace TestsNM
{
    [TestFixture]
    public class LoginTest : MySetUpClass
    {
        [Test]
        public void LoginInValid()
        {
            //...
        }

        [Test]
        public void LoginValid()
        {
            //...
        }
    }
}

File3.cs 包含:

namespace TestsNM
{
    [TestFixture]
    public class AddTest : MySetUpClass
    {
        [Test]
        public void Execute1()
        {
            //...
        }

        [Test]
        public void Execute2()
        {
            //...
        }
    }
}

File4.cs 包含:

namespace TestsNM
{
    public class Tests
    {
        [Suite]
        public static IEnumerable Suite
        {
            get
            {
                return GetSuite();
            }
        }

        static ArrayList GetSuite()
        {
            ArrayList suite = new ArrayList();

            suite.Add(new LoginTest());
            suite.Add(new AddTest());

            return suite;
        }
    }
}

在 Main() 中:

string[] my_args = { "/fixture=" + typeof(Tests), Assembly.GetExecutingAssembly().Location };
int returnCode = NUnit.ConsoleRunner.Runner.Main(my_args);

当我运行它时,MySetUpClass 会在每次测试之前和之后执行。我怎样才能让该类在所有 TestFixtures 之前和之后只运行一次?

4

2 回答 2

1

您必须将标签更改[SetUp][TestFixtureSetUp]和。[TearDown][TestFixtureTearDown]

于 2015-11-04T11:51:44.287 回答
0

那没有帮助。我想要的是以下输出:#run RunBeforeAnyTests() #run all Tests in TestFixture class-LoginTest。#运行TestFixture 类-AddTest 中的所有测试。#runAfterAnyTests() 我通过在类测试中放置 [TestFixtureSetUp] 和 [TestFixtureTearDown] 来实现这一点。公共类测试 { [TestFixtureSetUp] 公共无效设置();[TestFixtureTearDown] public void Teardown(); [套件] 公共静态 IEnumerable 套件;}

还取出了基类-MySetUpClass。

于 2015-11-04T12:37:44.120 回答