C# NUnit 在 NullPointer 中使用 TestCaseSource 结果
C# NUnit using TestCaseSource results in NullPointer
我有以下 class,它是一个 TestFixture,它有一个测试和一个生成我的 TestCases 的 TestCaseSource。我的测试用例实际上包含实际结果和预期结果,它们被包装在一个名为 TestCaseEntity
的对象中
namespace SomeNamespace
{
[TestFixture]
public class MyTest
{
static Client client;
static List<string> userIds = new List<string>();
[TestFixtureSetUp]
public void Init()
{
// Set up a client
client = new Client("server address"); // a HTTP client
// populate a List of userIds from a local text test file
userIds.Add(GetAllIdsFromTxtFile());
}
[Test, TestCaseSource(typeof(MyTestCaseEntityFactory), "MyTestCases")]
public void CheckExpectations(TestCaseEntity testCaseEntity)
{
if (!testCaseEntity.IsIdentical)
{
Log.Error("Log some shit");
Assert.Fail("fail assertions");
}
}
public class MyTestCaseEntityFactory
{
public static IEnumerable<TestCaseEntity> MyTestCases
{
get
{
foreach (string id in userIds)
{
// use the client and get the results, construct the new TestCaseEntity(...) and return;
yield return new TestCaseEntity("actualValue", "expectedValue resulting from the server call");
}
}
}
}
}
}
当我 运行 我的测试时,我收到以下错误,不幸的是这不是很有帮助!
System.NullReferenceException : Object reference not set to an instance of an object.
对我可能做错的地方有什么建议吗?
你 foreach 超过 allSalesDEDealIds
但你似乎没有将它包含在你的代码示例中。这可能是您代码中的 NullRefException
?
我有以下 class,它是一个 TestFixture,它有一个测试和一个生成我的 TestCases 的 TestCaseSource。我的测试用例实际上包含实际结果和预期结果,它们被包装在一个名为 TestCaseEntity
的对象中namespace SomeNamespace
{
[TestFixture]
public class MyTest
{
static Client client;
static List<string> userIds = new List<string>();
[TestFixtureSetUp]
public void Init()
{
// Set up a client
client = new Client("server address"); // a HTTP client
// populate a List of userIds from a local text test file
userIds.Add(GetAllIdsFromTxtFile());
}
[Test, TestCaseSource(typeof(MyTestCaseEntityFactory), "MyTestCases")]
public void CheckExpectations(TestCaseEntity testCaseEntity)
{
if (!testCaseEntity.IsIdentical)
{
Log.Error("Log some shit");
Assert.Fail("fail assertions");
}
}
public class MyTestCaseEntityFactory
{
public static IEnumerable<TestCaseEntity> MyTestCases
{
get
{
foreach (string id in userIds)
{
// use the client and get the results, construct the new TestCaseEntity(...) and return;
yield return new TestCaseEntity("actualValue", "expectedValue resulting from the server call");
}
}
}
}
}
}
当我 运行 我的测试时,我收到以下错误,不幸的是这不是很有帮助!
System.NullReferenceException : Object reference not set to an instance of an object.
对我可能做错的地方有什么建议吗?
你 foreach 超过 allSalesDEDealIds
但你似乎没有将它包含在你的代码示例中。这可能是您代码中的 NullRefException
?