4

Java の場合と同様: JUnit テストをランダムな順序で実行するにはどうすればよいですか?

.Net の場合はこれとは反対です: http://blogs.msdn.com/b/slange/archive/2010/06/02/ordering-method-execution-of-a-coded-ui-test.aspx

私の理由は最初のリンクと同じです。テストを実行するたびに順序をシャッフルして、テスト間の依存関係を明らかにしたいと考えています。

これはできますか?私のテストはGUIを駆動するため、使用する必要がありますMSTest-コード化されたUIを使用します。

4

2 に答える 2

3

これは答えられていないので、私はそれを突き刺します。

テスト フレームワークにこれが組み込まれているかどうかはわかりませんが、自分でコーディングするだけです。最も簡単な方法は、他のすべてのテスト メソッドをランダムな順序で呼び出すテスト メソッドを作成することです。これを行う方法で完全に創造的になることができます.Playback.PlaybackSettings.ContinueOnErrorをtrueに設定して、単一のテストが失敗したときにテスト全体が失敗しないようにするだけです.

于 2012-08-10T20:15:53.383 に答える
1

私は似たようなものが必要だったので、これはあなたや他の誰かに有利なスタートを切るかもしれません. これを独自のテスト クラスとしてドロップするだけで、同じアセンブリ内の他のすべての単体テスト メソッドが、単体テスト クラスにまたがるランダムな順序で 1 回だけ自動的に実行されます。

using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Company.UnitTests
{
    [TestClass]
    public class RandomizerTest
    {
        private class TestClassProxy
        {
            public object Instance { get; set; }
            public Type Type { get; set; }
            public Action ClassCleanup { get; set; }
            public Action<TestContext> ClassInitialize { get; set; }
            public Action TestCleanup { get; set; }
            public Action TestInitialize { get; set; }
            public List<Action> TestMethods { get; set; }
        }

        [TestMethod]
        public void TestRandom()
        {
            // ARRANGE

            // attributes we'll be using to build our proxies (change these for NInject, other UT frameworks)
            var classInitializeAttributeType = typeof (ClassInitializeAttribute);
            var classCleanupAttributeType = typeof (ClassCleanupAttribute);
            var testInitializeAttributeType = typeof (TestInitializeAttribute);
            var testCleanupAttributeType = typeof (TestCleanupAttribute);
            var testMethodAttributeType = typeof (TestMethodAttribute);

            var proxies = (
                              from type in Assembly.GetExecutingAssembly().GetTypes()
                              where
                                  type != typeof (RandomizerTest) && // don't include this class (infinite-loop)
                                  type.GetCustomAttributes(typeof (TestClassAttribute), false).Length > 0 // only classes attributed with [TestClass]
                              let methods = type.GetMethods() // keep the methods for re-use
                              let instance = Activator.CreateInstance(type)
                              select new TestClassProxy
                              {
                                  Type = type,
                                  Instance = instance,
                                  ClassInitialize = // select and wrap the method invokes inside an Action for re-use
                                      methods
                                          .Where(λ =>
                                                 λ.GetCustomAttributes(classInitializeAttributeType, false).Any())
                                          .Select(λ => (Action<TestContext>) (tc => λ.Invoke(instance, new object[] { tc })))
                                          .FirstOrDefault() ?? delegate { },
                                  ClassCleanup =
                                      methods
                                          .Where(λ =>
                                                 λ.GetCustomAttributes(classCleanupAttributeType, false).Any())
                                          .Select(λ => (Action) (() => λ.Invoke(instance, null)))
                                          .FirstOrDefault() ?? delegate { },
                                  TestInitialize =
                                      methods
                                          .Where(λ =>
                                                 λ.GetCustomAttributes(testInitializeAttributeType, false).Any())
                                          .Select(λ => (Action) (() => λ.Invoke(instance, null)))
                                          .FirstOrDefault() ?? delegate { },
                                  TestCleanup =
                                      methods
                                          .Where(λ =>
                                                 λ.GetCustomAttributes(testCleanupAttributeType, false).Any())
                                          .Select(λ => (Action) (() => λ.Invoke(instance, null)))
                                          .FirstOrDefault() ?? delegate { },
                                  TestMethods =
                                      methods
                                      .Where(λ =>
                                             λ.GetCustomAttributes(testMethodAttributeType, false).Any())
                                      .Select(λ => (Action) (() => λ.Invoke(instance, null))).ToList(),
                              }).ToList();

            var random = new Random();

            // ACT

            // Note that the following may not work depending on how you developed your unit tests. 
            // If they're sharing state in any way (SQL DB, etc.) this may not be what you want.
            // If that's the case alter the code below to only randomly sample test methods inside each class 
            //   so that you can isolate tests per test class.
            // This methodology assumes the cardinal rule: All unit tests are atomic. (given their proper setup/teardown)
            // Plus if you're testing in a random order this is likely what you're after anyway.

            // initialize all classes
            foreach (var testClassProxy in proxies)
            {
                testClassProxy.ClassInitialize(null);
            }

            // run all test methods in a random order spanning all test classes until we run out 
            while (proxies.Count > 0)
            {
                // get random test class proxy
                var proxy = proxies[random.Next(0, proxies.Count)];

                // get random test method from proxy
                var testMethod = proxy.TestMethods[random.Next(0, proxy.TestMethods.Count)];

                // run test initialize
                proxy.TestInitialize();

                // run test method
                testMethod(); // (ASSERT)

                // run test cleanup
                proxy.TestCleanup();

                // remove test method from processing
                proxy.TestMethods.Remove(testMethod);

                // still have methods?
                if (proxy.TestMethods.Count > 0)
                {
                    continue;
                }

                // no, run class cleanup routine
                proxy.ClassCleanup();

                // remove the proxy from processing
                proxies.Remove(proxy);
            }
        }
    }
}
于 2012-10-05T19:36:21.097 に答える