首页 > 解决方案 > NUnit TestFixtures 不适用于 RestSharp

问题描述

我正在尝试使用NUnit TestAttributes创建和删除一个RestSharp RestClient

https://github.com/nunit/docs/wiki/TestFixture-Attribute

using NUnit.Framework;
using RestSharp;

namespace Sanitized.Sanitized.Steps
{
    [TestFixture]
    public class SetupAndTeardown
    {
        public RestClient restClient;

        [SetUp]
        public void Init()
        {

            restClient = new RestClient();
        }

        [TearDown]
        public void Cleanup()
        {

            restClient = null;
        }
    }
}

Object reference not set to an instance of an object.但是,当我尝试在另一个类中使用它时,即使用我的自动步骤时,我得到了错误。

我不明白这一点,因为我认为[SetUp] [Teardown]属性中的代码分别在测试的开始和结束时被调用。

标签: c#automated-testsnunithookrestsharp

解决方案


You created a TestFixture, which is a class that contains tests. If the fixture had any tests, then NUnit would run them and would also run the setup before each test and the teardown after each test. Since you have no tests, that isn't happening. NUnit recognizes the fixture but finds nothing to run there.

You say that you have a problem when you "use" this fixture in another class. Test fixtures are not intended to be "used" by other code. Rather, they are run by NUnit.

For a better answer about how to do what you are trying to do, we first need to know what you are trying to do. When do you want the "setup" and "teardown" to run? How often should they run? Depending on those things, I can update this answer.

Replying further to your comment... If your tests are in another class, then that class is your test fixture. Is there a reason you don't want it to be the fixture?


推荐阅读