重复 mstest 测试运行多次

发布于 2024-07-20 16:49:05 字数 126 浏览 3 评论 0原文

我的一些 mstest 单元测试有助于检测多线程竞争条件,因此它们在连续运行多次时最有用,但我只想针对特定的测试运行执行此操作 - 而不是始终如此。

有没有办法配置 mstest(最好在测试列表编辑器中)多次运行测试?

Some of my mstest unit tests help detect multi-threading race conditions, and as such they are most useful when run many times in a row, but I only want to do this for specific test runs -- not all the time.

Is there a way to configure mstest (in the Test List Editor preferably) to run a test multiple times?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

抚笙 2024-07-27 16:49:06

我想答案是否定的。

I guess the answer is no.

遮云壑 2024-07-27 16:49:06

您可以创建生成指定次数的数据行的自定义属性。 结果测试将被执行多次。 想法取自 https://learn.microsoft.com/en-us/visualstudio/test/how-to-create-a-data-driven-unit-test?view=vs-2022
示例属性类:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace EndToEndTest.Infrastructure;

public class RepeatAttribute : Attribute, ITestDataSource {
private readonly int _count;

public RepeatAttribute(int count = 10) {
    this._count = count;
}

public IEnumerable<object[]> GetData(MethodInfo methodInfo) {
    return Enumerable.Range(1, _count).Select(x => new object[0]);
}

public string GetDisplayName(MethodInfo methodInfo, object[] data) {
    if (data != null) {
        return string.Format(CultureInfo.CurrentCulture, "{0} x{1}", methodInfo.Name, _count);
    }

    return null;
}
}

然后将此属性添加到测试方法中

[TestMethod]
[Repeat(5)]
public void TestAccountSummaryInitialData() {...}

You can create custom attribute that yields data rows specified number of times. As a result test will be executed several times. Idea is taken from https://learn.microsoft.com/en-us/visualstudio/test/how-to-create-a-data-driven-unit-test?view=vs-2022
Example attribute class:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace EndToEndTest.Infrastructure;

public class RepeatAttribute : Attribute, ITestDataSource {
private readonly int _count;

public RepeatAttribute(int count = 10) {
    this._count = count;
}

public IEnumerable<object[]> GetData(MethodInfo methodInfo) {
    return Enumerable.Range(1, _count).Select(x => new object[0]);
}

public string GetDisplayName(MethodInfo methodInfo, object[] data) {
    if (data != null) {
        return string.Format(CultureInfo.CurrentCulture, "{0} x{1}", methodInfo.Name, _count);
    }

    return null;
}
}

And then add this attribute to a test method

[TestMethod]
[Repeat(5)]
public void TestAccountSummaryInitialData() {...}
清风疏影 2024-07-27 16:49:06

我发现最简单的方法是使用“运行直至失败”(Ctr+R,U)。
该测试将循环运行,直到您停止它,同时显示已执行测试的状态。 它在菜单中的样子

The easiest way I found is to use Run Until Failure (Ctr+R,U).
This test will be run in loop until you stop it, meantime showing status of executed tests. how it looks like in menu

情泪▽动烟 2024-07-27 16:49:05

我需要做类似的事情,所以我想出了一个解决方案。

这并不简单,但是一旦所有内容都设置完毕,您就可以在项目中重复使用它。 我还在 GitHub 上下载了此代码 (https://github.com/johnkoerner/MSTestLooper) ,但以防万一这种情况在某个时候消失,我是这样做的。

首先,我们创建一个属性,将其应用于我们的类,以告诉它多次运行所有测试。 在单独的程序集中执行所有这些操作,因为 DLL 需要位于特殊位置。

[Serializable]
public class TestLooperAttribute :  TestClassExtensionAttribute
{
    private static readonly Uri thisGuy = new Uri("urn:TestLooperAttribute");

    private string _PropertyName;
    public string PropertyName
    {
        get
        { return _PropertyName; }
        set
        {
            _PropertyName = value;
        }
    }
    public override Uri ExtensionId
    {

        get {
            return thisGuy; }
    }


        public override TestExtensionExecution GetExecution()
    {

        return new TestLooperExecution(PropertyName);
    }
}

接下来我们必须创建一个自定义测试类执行类:

class TestLooperExecution : TestExtensionExecution
{
    private string PropertyName;

    public TestLooperExecution(string PropertyName)
    {
        this.PropertyName = PropertyName;
    }

    public override ITestMethodInvoker CreateTestMethodInvoker(TestMethodInvokerContext InvokerContext)
    {
        return new TestLooperInvoker(InvokerContext, PropertyName);
    }

    public override void Dispose()
    {
        //TODO: Free, release or reset native resources
    }

    public override void Initialize(TestExecution Execution)
    {
        //TODO: Wire up event handlers for test events if needed

    }
}

最后我们添加一个自定义调用程序,这是我们执行循环的地方:

class TestLooperInvoker : ITestMethodInvoker
{
    private TestMethodInvokerContext m_invokerContext;
    private string PropertyName;

    public TestLooperInvoker(TestMethodInvokerContext InvokerContext, string PropertyName)
    {
        m_invokerContext = InvokerContext;
        this.PropertyName = PropertyName;
    }

    public TestMethodInvokerResult Invoke(params object[] args)
    {

        // Our helper results class to aggregate our test results
        HelperTestResults results = new HelperTestResults();

        IEnumerable<object> objects = m_invokerContext.TestContext.Properties[PropertyName] as IEnumerable<object>;

        foreach (var d in objects)
            results.AddTestResult(m_invokerContext.InnerInvoker.Invoke(d), new object[1] { d.GetType().ToString()});

        var output = results.GetAllResults();
        m_invokerContext.TestContext.WriteLine(output.ExtensionResult.ToString());

        return output;
    }
}

HelperTestResults 类只是构建输出字符串,您可以按照您想要的方式处理它,但我不希望包含该代码,因为它只会使这篇文章变得更长。

将其编译到 DLL 中,然后需要将其复制到

C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\PublicAssemblies

您还必须为该类创建一个注册表项:

Windows Registry Editor Version 5.00 
[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\11.0\EnterpriseTools\QualityTools\TestTypes\{13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b}\TestTypeExtensions\TestLooperAttribute]
"AttributeProvider"="TestLooper.TestLooperAttribute, TestLooper"

现在您已经完成了所有这些操作,您终于可以使用该类了:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestLooper;
using System.Collections.Generic;
namespace UnitTestSamples
{
    [TestLooper(PropertyName="strings")]
    public class UnitTest1
    {
        public static List<String> strings = new List<String>();
        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }
        [ClassInitialize()]
        public static void Init(TestContext x)
        {
            strings.Add("A");
            strings.Add("B");
            strings.Add("C");
            strings.Add("D");

        }

        [TestInitialize()]
        public void TestInit()
        {
            if (!TestContext.Properties.Contains("strings"))
            testContextInstance.Properties.Add("strings", strings);
        }

        [TestMethod]
        [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "DataDriven1.csv", "DataDriven1#csv", DataAccessMethod.Sequential)]
        [DeploymentItem("DataDriven1.csv")]
        public void TestMethodStrings(string s)

        {
            int value1 = Convert.ToInt32(TestContext.DataRow["Col1"]); ;
            TestContext.WriteLine(String.Format("{0}:{1}", s, value1));
        }
    }
}

请注意,我们的测试方法接受一个参数,来自测试循环器。 我还使用数据驱动测试来展示这一点,以表明您可以将两者结合在一起以在数据集中生成大型排列。

I needed to do something similar, so I came up with a solution to this.

It's not simple, but once everything is setup you can reuse it across projects. I also have a download of this code on GitHub (https://github.com/johnkoerner/MSTestLooper), but in case that goes away at some point, here is how I did it.

First we create an attribute that we will apply to our class to tell it run all the tests multiple times. Do all of this in a separate assembly, because the DLL needs to live in a special location.

[Serializable]
public class TestLooperAttribute :  TestClassExtensionAttribute
{
    private static readonly Uri thisGuy = new Uri("urn:TestLooperAttribute");

    private string _PropertyName;
    public string PropertyName
    {
        get
        { return _PropertyName; }
        set
        {
            _PropertyName = value;
        }
    }
    public override Uri ExtensionId
    {

        get {
            return thisGuy; }
    }


        public override TestExtensionExecution GetExecution()
    {

        return new TestLooperExecution(PropertyName);
    }
}

Next we have to create a custom test class execution class:

class TestLooperExecution : TestExtensionExecution
{
    private string PropertyName;

    public TestLooperExecution(string PropertyName)
    {
        this.PropertyName = PropertyName;
    }

    public override ITestMethodInvoker CreateTestMethodInvoker(TestMethodInvokerContext InvokerContext)
    {
        return new TestLooperInvoker(InvokerContext, PropertyName);
    }

    public override void Dispose()
    {
        //TODO: Free, release or reset native resources
    }

    public override void Initialize(TestExecution Execution)
    {
        //TODO: Wire up event handlers for test events if needed

    }
}

Finally we add a custom invoker, which is where we perform the looping:

class TestLooperInvoker : ITestMethodInvoker
{
    private TestMethodInvokerContext m_invokerContext;
    private string PropertyName;

    public TestLooperInvoker(TestMethodInvokerContext InvokerContext, string PropertyName)
    {
        m_invokerContext = InvokerContext;
        this.PropertyName = PropertyName;
    }

    public TestMethodInvokerResult Invoke(params object[] args)
    {

        // Our helper results class to aggregate our test results
        HelperTestResults results = new HelperTestResults();

        IEnumerable<object> objects = m_invokerContext.TestContext.Properties[PropertyName] as IEnumerable<object>;

        foreach (var d in objects)
            results.AddTestResult(m_invokerContext.InnerInvoker.Invoke(d), new object[1] { d.GetType().ToString()});

        var output = results.GetAllResults();
        m_invokerContext.TestContext.WriteLine(output.ExtensionResult.ToString());

        return output;
    }
}

The HelperTestResults class just builds up strings for output, you can handle this how you want and I don't want to include that code because it will just make this post that much longer.

Compile this into a DLL and then you need to copy it to

C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\PublicAssemblies

You also have to create a registry entry for the class:

Windows Registry Editor Version 5.00 
[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\11.0\EnterpriseTools\QualityTools\TestTypes\{13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b}\TestTypeExtensions\TestLooperAttribute]
"AttributeProvider"="TestLooper.TestLooperAttribute, TestLooper"

Now that you have all of that done, you can finally use the class:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestLooper;
using System.Collections.Generic;
namespace UnitTestSamples
{
    [TestLooper(PropertyName="strings")]
    public class UnitTest1
    {
        public static List<String> strings = new List<String>();
        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }
        [ClassInitialize()]
        public static void Init(TestContext x)
        {
            strings.Add("A");
            strings.Add("B");
            strings.Add("C");
            strings.Add("D");

        }

        [TestInitialize()]
        public void TestInit()
        {
            if (!TestContext.Properties.Contains("strings"))
            testContextInstance.Properties.Add("strings", strings);
        }

        [TestMethod]
        [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "DataDriven1.csv", "DataDriven1#csv", DataAccessMethod.Sequential)]
        [DeploymentItem("DataDriven1.csv")]
        public void TestMethodStrings(string s)

        {
            int value1 = Convert.ToInt32(TestContext.DataRow["Col1"]); ;
            TestContext.WriteLine(String.Format("{0}:{1}", s, value1));
        }
    }
}

Notice that our test method accepts a parameter, which comes from the test looper. I also show this using a data driven test, to show you can combine the two together to generate large permutations across your data sets.

梦年海沫深 2024-07-27 16:49:05
[TestMethod()]
public void RepetableTest(){
   for(int i = 0; i < repeatNumber; i++){

     //test code goes here


   }
}
[TestMethod()]
public void RepetableTest(){
   for(int i = 0; i < repeatNumber; i++){

     //test code goes here


   }
}
只是在用心讲痛 2024-07-27 16:49:05

考虑创建一个测试来分离几个线程。 测试列表不允许您对同一测试有多个条目。 但是,您可以将多线程测试分配到其自己的列表,并仅在您想要运行该特定测试时调用它。

Consider creating a test to spin off a couple of threads. The Test List won't allow you to have multiple entries for the same test. You could assign, however, the multi-threaded test to its own list and call it only when you want to run that particular test.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文