TestContext 名称与给定代码有何关联?
How does the TestContext name is relevant in given code?
我正在使用 MicrosoftTestTools。我有一个 class A 包含 [TestMethod] 和 [ClassInitialize]。
[ClassInitialize]
public static void ClassInitialize(TestContext context)
{
Setup(context);
}
设置的定义存在于 Class B(class A 的基础class)中。
public static void Setup(TestContext context)
{
something
}
[TestCleanUp] 存在于 Class B 中。
[TestCleanup]
public void TestCleanUp()
{
if (TestContext.CurrentTestOutcome == UnitTestOutcome.Failed){}
}
为了在 TestCleanUp 方法中获取 TestContext,我创建了一个 属性,如下面的 class B.
public TestContext TestContext { get; set; }
它运行良好。但是当我改变声明时,
public `TestContext CurrentContext{ get; set; }`
所以功能类似于
[TestCleanup]
public void TestCleanUp()
{
if (CurrentContext.CurrentTestOutcome == UnitTestOutcome.Failed){}
}
CurrentContext 为空。 属性 在这里如何工作?这仅在名称为 TestContext 时有效。我对 C# 编程很天真。
您可能已经注意到您没有自己分配 TestContext
属性,但不知何故它获得了它的价值。它如何获得价值?测试框架分配它。它是如何找到 属性 的?它查找类型为 TestContext
的名为 TestContext
的 属性。因此,当您更改名称时 - 它无法再找到它并且无法分配值,因此它仍然为空。
您可以在 documentation 中阅读有关此 class 的更多信息。这个问题的相关部分是:
When you run a unit test, you are automatically provided with a
concrete instance of the TestContext type, if the test class that
contains your unit test method has a TestContext property defined.
我正在使用 MicrosoftTestTools。我有一个 class A 包含 [TestMethod] 和 [ClassInitialize]。
[ClassInitialize]
public static void ClassInitialize(TestContext context)
{
Setup(context);
}
设置的定义存在于 Class B(class A 的基础class)中。
public static void Setup(TestContext context)
{
something
}
[TestCleanUp] 存在于 Class B 中。
[TestCleanup]
public void TestCleanUp()
{
if (TestContext.CurrentTestOutcome == UnitTestOutcome.Failed){}
}
为了在 TestCleanUp 方法中获取 TestContext,我创建了一个 属性,如下面的 class B.
public TestContext TestContext { get; set; }
它运行良好。但是当我改变声明时,
public `TestContext CurrentContext{ get; set; }`
所以功能类似于
[TestCleanup]
public void TestCleanUp()
{
if (CurrentContext.CurrentTestOutcome == UnitTestOutcome.Failed){}
}
CurrentContext 为空。 属性 在这里如何工作?这仅在名称为 TestContext 时有效。我对 C# 编程很天真。
您可能已经注意到您没有自己分配 TestContext
属性,但不知何故它获得了它的价值。它如何获得价值?测试框架分配它。它是如何找到 属性 的?它查找类型为 TestContext
的名为 TestContext
的 属性。因此,当您更改名称时 - 它无法再找到它并且无法分配值,因此它仍然为空。
您可以在 documentation 中阅读有关此 class 的更多信息。这个问题的相关部分是:
When you run a unit test, you are automatically provided with a concrete instance of the TestContext type, if the test class that contains your unit test method has a TestContext property defined.