在 TestCleanup mstest c# 中获取 TestResult
Get TestResult in TestCleanup mstest c#
我想在 TestCleanup() 中使用 TestResult 来获取有关测试的一些信息。
但我不知道如何初始化 TestResult 对象并获取它。
我想要与 TestContext 对象相同的行为。
谢谢
private static TestContext _testContext;
[ClassInitialize]
public static void SetupTests(TestContext testContext)
{
_testContext = testContext;
}
编辑:
因此,如果我无法在 TestCleanup 中访问 TestResult,我如何在所有测试完成后将所有测试结果写入 csv 文件?
TestMethod 属性将 TestResult 对象的数组返回给适配器。这不是您可以访问的内容。您需要从 TestContext 获取信息。
您无法访问 TestCleanup
中的 TestResult
对象,因为它在此阶段尚不存在。在测试执行期间,花费在 TestCleanup
和 TestInitialize
上的时间合并为 TestResult.Duration
属性。您可以通过输入以下内容轻松测试它:
[TestCleanup]
public void TestCleanup()
{
Thread.Sleep(1000);
}
在你的快速执行中TestMethod
。或者您可以检查 TestMethodInfo
上的 Invoke
方法:https://github.com/microsoft/testfx/blob/167533cadfd2839641fc238d39ad2e86b4262be1/src/Adapter/MSTest.CoreAdapter/Execution/TestMethodInfo.cs#L127
此方法将 运行 您的测试。可以看到 watch.Start()
和 watch.Stop()
放在哪里,ExecuteInternal
方法在哪里执行。此方法将 运行 RunTestInitializeMethod
和 RunTestCleanupMethod
介于 Start
和 Stop
之间。
您唯一的解决方案是合并测试中的所有 TestResults class,然后在您的 ClassCleanup 方法中访问它们。
您可以通过实现自己的 TestMethodAttribute
并覆盖 Execute
方法来实现。然后,您可以将所有结果保存在静态 属性 - Results
in TestResultCollection
class - 并在 TestCleanup
方法中访问它。这是一个小例子:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
namespace UnitTestProject
{
[TestClass]
public class UnitTest
{
[ClassCleanup]
public static void ClassCleanUp()
{
// Save TestResultCollection.Results in csv file
}
[MyTestMethod]
public void TestMethod()
{
Assert.IsTrue(true);
}
}
public static class TestResultCollection
{
public static Dictionary<ITestMethod, TestResult[]> Results { get; set; } = new Dictionary<ITestMethod, TestResult[]>();
}
public class MyTestMethodAttribute : TestMethodAttribute
{
public override TestResult[] Execute(ITestMethod testMethod)
{
TestResult[] results = base.Execute(testMethod);
TestResultCollection.Results.Add(testMethod, results);
return results;
}
}
}
请记住,这更像是一种破解,而不是正确的解决方案。最好的选择是实现您自己的 csv 记录器,并使用 vstest.console.exe
和 csv
开关 运行 它。
我想在 TestCleanup() 中使用 TestResult 来获取有关测试的一些信息。 但我不知道如何初始化 TestResult 对象并获取它。 我想要与 TestContext 对象相同的行为。
谢谢
private static TestContext _testContext;
[ClassInitialize]
public static void SetupTests(TestContext testContext)
{
_testContext = testContext;
}
编辑: 因此,如果我无法在 TestCleanup 中访问 TestResult,我如何在所有测试完成后将所有测试结果写入 csv 文件?
TestMethod 属性将 TestResult 对象的数组返回给适配器。这不是您可以访问的内容。您需要从 TestContext 获取信息。
您无法访问 TestCleanup
中的 TestResult
对象,因为它在此阶段尚不存在。在测试执行期间,花费在 TestCleanup
和 TestInitialize
上的时间合并为 TestResult.Duration
属性。您可以通过输入以下内容轻松测试它:
[TestCleanup]
public void TestCleanup()
{
Thread.Sleep(1000);
}
在你的快速执行中TestMethod
。或者您可以检查 TestMethodInfo
上的 Invoke
方法:https://github.com/microsoft/testfx/blob/167533cadfd2839641fc238d39ad2e86b4262be1/src/Adapter/MSTest.CoreAdapter/Execution/TestMethodInfo.cs#L127
此方法将 运行 您的测试。可以看到 watch.Start()
和 watch.Stop()
放在哪里,ExecuteInternal
方法在哪里执行。此方法将 运行 RunTestInitializeMethod
和 RunTestCleanupMethod
介于 Start
和 Stop
之间。
您唯一的解决方案是合并测试中的所有 TestResults class,然后在您的 ClassCleanup 方法中访问它们。
您可以通过实现自己的 TestMethodAttribute
并覆盖 Execute
方法来实现。然后,您可以将所有结果保存在静态 属性 - Results
in TestResultCollection
class - 并在 TestCleanup
方法中访问它。这是一个小例子:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
namespace UnitTestProject
{
[TestClass]
public class UnitTest
{
[ClassCleanup]
public static void ClassCleanUp()
{
// Save TestResultCollection.Results in csv file
}
[MyTestMethod]
public void TestMethod()
{
Assert.IsTrue(true);
}
}
public static class TestResultCollection
{
public static Dictionary<ITestMethod, TestResult[]> Results { get; set; } = new Dictionary<ITestMethod, TestResult[]>();
}
public class MyTestMethodAttribute : TestMethodAttribute
{
public override TestResult[] Execute(ITestMethod testMethod)
{
TestResult[] results = base.Execute(testMethod);
TestResultCollection.Results.Add(testMethod, results);
return results;
}
}
}
请记住,这更像是一种破解,而不是正确的解决方案。最好的选择是实现您自己的 csv 记录器,并使用 vstest.console.exe
和 csv
开关 运行 它。