gpt4 book ai didi

c# - 如何扩展 .NET TestClass

转载 作者:太空狗 更新时间:2023-10-29 23:31:45 24 4
gpt4 key购买 nike

我正在尝试扩展 .NET TestClass。我发现我需要扩展以下抽象类:TestClassExtensionAttributeTestExtensionExecutionTestExtensionExecution 还需要我实现一个 ITestMethodInvoker。我已经完成了以下操作,但是当我运行一个测试方法时,我的断点都没有被命中(无论是在测试中还是在扩展中),这意味着测试类永远不会到达我的扩展并且显然在更早的时候失败了。 有人可以指出我遗漏的内容或有关如何扩展 TestClass 的工作示例吗?

扩展名:

class CoreTestClass : TestClassExtensionAttribute
{
public override Uri ExtensionId
{
get { throw new NotImplementedException(); }
}

public override TestExtensionExecution GetExecution()
{
return new TestCore();
}
}

class TestCore: TestExtensionExecution
{
public override ITestMethodInvoker CreateTestMethodInvoker(TestMethodInvokerContext context)
{
return new AnalysisTestMethodInvoker();
}

public override void Dispose()
{

}

public override void Initialize(TestExecution execution)
{
execution.OnTestStopping += execution_OnTestStopping;
}

void execution_OnTestStopping(object sender, OnTestStoppingEventArgs e)
{
throw new NotImplementedException();
}
}

class AnalysisTestMethodInvoker : ITestMethodInvoker
{
public TestMethodInvokerResult Invoke(params object[] parameters)
{
throw new NotImplementedException();
}
}

测试:

[CoreTestClass]
public class HomeControllerTest
{
[TestMethod]
public void Index()
{
// Arrange
HomeController controller = new HomeController();

// Act
ViewResult result = controller.Index() as ViewResult;

// Assert
Assert.AreEqual("Modify this template to jump-start your ASP.NET MVC application.", result.ViewBag.Message);
}
}

最佳答案

这篇 MSDN 文章描述了如何实现 TestClassExtensionAttribute:Extending the Visual Studio unit test type

相关问题:Is defining TestMethod's in test base classes not supported by MsTest?

根据您要完成的任务,您可以使用标有[TestClass] 的标准抽象类。 TestClassAttribute 将被派生类型继承,但是派生类型也必须用 [TestClass] 标记以用于 [TestInitialize] [测试清理]

参见 Using a base class for your unit testing classes .

关于c# - 如何扩展 .NET TestClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19544503/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com