c# - 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]
属性中的代码分别在测试的开始和结束时被调用。
解决方案
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?
推荐阅读
- ajax - 如何从ajax响应中打开pdf
- javascript - 在 vscode 中调试 JavaScript 时如何找到当前函数的闭包
- python - 有什么方法可以在函数调用参数中添加 if 语句?
- php - Doctrine2 - 添加或更新没有 Id
- python - 计算数据框中列表中文本的频率
- c# - 无法将所选项目保存在数组的第一个位置
- powershell - 带过滤器的 PowerShell 数组 ~ ParameterBindingException
- php - 具有两个模型的雄辩查询
- ios - retrieve chat message in firebase issue in ios swift
- postgresql - Foreign key issue while insert or delete