NUnit TestFixtures不能与RestSharp一起使用

问题描述 投票:0回答:1

我正在尝试使用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-tests nunit hook restsharp
1个回答
0
投票

您创建了一个TestFixture,它是一个包含测试的类。如果夹具有任何测试,那么NUnit将运行它们,并且还将在每次测试之前运行设置并在每次测试之后进行拆卸。由于您没有测试,因此没有发生。 NUnit识别夹具,但没有发现任何东西在那里运行。

当你在另一个类中“使用”这个装置时,你说你有问题。测试夹具不打算被其他代码“使用”。相反,它们由NUnit运行。

为了更好地回答如何做你想做的事情,我们首先需要知道你想要做什么。你什么时候想要“设置”和“拆解”运行?他们应该多久运行一次?根据这些事情,我可以更新这个答案。

回复你的评论......如果你的测试是在另一个类中,那么那个类就是你的测试夹具。有没有理由你不希望它成为夹具?

© www.soinside.com 2019 - 2024. All rights reserved.