简体   繁体   English

NUnit TestFixtures不能与RestSharp一起使用

[英]NUnit TestFixtures do not work with RestSharp

I'm trying to use NUnit TestAttributes to create and delete a RestSharp RestClient 我正在尝试使用NUnit TestAttributes来创建和删除RestSharp RestClient

https://github.com/nunit/docs/wiki/TestFixture-Attribute 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;
        }
    }
}

But, I get the error Object reference not set to an instance of an object. 但是,我得到错误Object reference not set to an instance of an object. when trying to use this in another class ie with my automated steps. 当试图在另一个类中使用它时,即使用我的自动步骤。

I don't understand this, as I thought code that's in the [SetUp] [Teardown] attributes are called at the beginning and end of the test respectively. 我不明白这一点,因为我认为[SetUp] [Teardown]属性中的代码分别在测试的开始和结束时被调用。

You created a TestFixture , which is a class that contains tests. 您创建了一个TestFixture ,它是一个包含测试的类。 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. 如果夹具有任何测试,那么NUnit将运行它们,并且还将在每次测试之前运行设置并在每次测试之后进行拆卸。 Since you have no tests, that isn't happening. 由于您没有测试,因此没有发生。 NUnit recognizes the fixture but finds nothing to run there. NUnit识别夹具,但没有发现任何东西在那里运行。

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. 相反,它们由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? 有没有理由你不希望它成为夹具?

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

相关问题 从NUnit中的多个类文件运行TestFixtures - Running TestFixtures from multiple class files in NUnit 将visual studio项目testfixtures转换为nunit dll - Turn visual studio project testfixtures into nunit dll 如何使用多个浏览器对C#NUnit TestFixtures进行参数化 - How to parameterized C# NUnit TestFixtures with multiple browsers 如何让“RedirectStandardOutput”在 NUnit 中工作? - How do I get 'RedirectStandardOutput' to work in NUnit? NUnit是否在每个测试之间重新实例化标记为TestFixtures的类? - Does NUnit re-instantiate classes marked as TestFixtures between each test? 我如何在没有NUNIT的情况下运行此硒应用程序? - How do I get this selenium app to work without NUNIT? RestSharp单元测试NUnit Moq RestResponse空引用异常 - RestSharp Unit Test NUnit Moq RestResponse null reference exception 如何将 RestSharp AddFile 转换为处理二进制数据? - How to convert RestSharp AddFile to work on Binary Data? 编写了一个控制台应用程序以使用RestSharp推送数据,但是当我尝试创建Windows Service时它不起作用 - Wrote a console app to push data using RestSharp, but when I try to do create a Windows Service it doesn't work 带有JWT身份验证的RestSharp不起作用 - RestSharp with JWT-authentication doesn't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM