C# 在TestCleanup MSC中获取TestResult#

C# 在TestCleanup MSC中获取TestResult#,c#,selenium,mstest,test-results,C#,Selenium,Mstest,Test Results,我想在TestCleanup()中使用TestResult来获取一些关于测试的信息。 但我不知道如何初始化TestResult对象并获取它。 我想要与TestContext对象相同的行为 谢谢 private static TestContext _testContext; [ClassInitialize] public static void SetupTests(TestContext testContext) { _testContext

我想在TestCleanup()中使用TestResult来获取一些关于测试的信息。 但我不知道如何初始化TestResult对象并获取它。 我想要与TestContext对象相同的行为

谢谢

    private static TestContext _testContext;
    [ClassInitialize]
    public static void SetupTests(TestContext testContext)
    {
        _testContext = testContext;
    }
编辑:
因此,如果我无法在TestCleanup中访问TestResult,那么在所有测试完成后如何将所有TestResult写入csv文件?

TestMethod属性将TestResult对象的数组返回给适配器。这不是您可以访问的内容。您需要从TestContext获取信息


TestResult对象的数组由TestMethod属性返回给适配器。这不是您可以访问的内容。您需要从TestContext获取信息


您无法访问
TestCleanup
中的
TestResult
对象,因为此阶段尚不存在该对象。在测试执行期间,
TestCleanup
TestInitialize
中花费的时间被合并到
TestResult.Duration
属性中。您可以通过以下方式轻松测试:

[TestCleanup]
public void TestCleanup()
{
    Thread.Sleep(1000);
}
在快速执行的
TestMethod
中。或者您可以检查
TestMethodInfo
上的
Invoke
方法:

此方法将运行您的测试。您可以看到放置
watch.Start()
watch.Stop()
的位置,以及执行
ExecuteInternal
方法的位置。此方法将在
Start
Stop
之间运行
RunTestInitializeMethod
RunTestCleanupMethod

唯一的解决方案是组合测试类中的所有测试结果,然后在ClassCleanup方法中访问它们

您可以通过实现自己的
TestMethodAttribute
并重写
Execute
方法来实现。然后,您可以将所有结果保存在一个静态属性中-
results
TestResultCollection
类中,并在
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;
        }
    }
}
使用Microsoft.VisualStudio.TestTools.UnitTesting;
使用System.Collections.Generic;
名称空间UnitTestProject
{
[测试类]
公共类单元测试
{
[课堂清理]
公共静态void ClassCleanUp()
{
//将TestResultCollection.Results保存在csv文件中
}
[MyTestMethod]
公共void TestMethod()
{
Assert.IsTrue(真);
}
}
公共静态类TestResultCollection
{
公共静态词典结果{get;set;}=new Dictionary();
}
公共类MyTestMethodAttribute:TestMethodAttribute
{
公共重写TestResult[]执行(ITestMethod testMethod)
{
TestResult[]results=base.Execute(testMethod);
TestResultCollection.Results.Add(testMethod,Results);
返回结果;
}
}
}

请记住,这更像是一个黑客,而不是一个正确的解决方案。最好的选择是实现您自己的csv记录器,并使用
vstest.console.exe
csv
开关运行它。

您无法访问
TestCleanup
中的
TestResult
对象,因为它在此阶段还不存在。在测试执行期间,
TestCleanup
TestInitialize
中花费的时间被合并到
TestResult.Duration
属性中。您可以通过以下方式轻松测试:

[TestCleanup]
public void TestCleanup()
{
    Thread.Sleep(1000);
}
在快速执行的
TestMethod
中。或者您可以检查
TestMethodInfo
上的
Invoke
方法:

此方法将运行您的测试。您可以看到放置
watch.Start()
watch.Stop()
的位置,以及执行
ExecuteInternal
方法的位置。此方法将在
Start
Stop
之间运行
RunTestInitializeMethod
RunTestCleanupMethod

唯一的解决方案是组合测试类中的所有测试结果,然后在ClassCleanup方法中访问它们

您可以通过实现自己的
TestMethodAttribute
并重写
Execute
方法来实现。然后,您可以将所有结果保存在一个静态属性中-
results
TestResultCollection
类中,并在
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;
        }
    }
}
使用Microsoft.VisualStudio.TestTools.UnitTesting;
使用System.Collections.Generic;
名称空间UnitTestProject
{
[测试类]
公共类单元测试
{
[课堂清理]
公共静态void ClassCleanUp()
{
//将TestResultCollection.Results保存在csv文件中
}
[MyTestMethod]
公共void TestMethod()
{
Assert.IsTrue(真);
}
}
公共静态类TestResultCollection
{
公共静态词典结果{get;set;}=new Dictionary();
}
公共类MyTestMethodAttribute:TestMethodAttribute
{
公共重写TestResult[]执行(ITestMethod testMethod)
{
TestResult[]results=base.Execute(testMethod);
TestResultCollection.Results.Add(testMethod,Results);
返回结果;
}
}
}

请记住,这更像是一个黑客,而不是一个正确的解决方案。最好的选择是实现您自己的csv记录器,并使用
vstest.console.exe
csv
开关运行它。

我已经知道了,但无法从TestContext访问测试结果消息和持续时间。我已经知道了,但无法从TestContext。如果我不能在TestCleanup中访问TestResult,那么在所有测试完成后,我如何将所有测试结果写入csv文件?您不想从控制台运行测试并使用trx文件吗?比如:
vstest.console.exe your.test.dll/Logger:trx
,因为这正是我们拥有trx文件的原因;有p