MSTestExtensions/CombinatorialTestMethodAttribute.cs
/// <summary>
/// Executes a combinatorial test method.
/// </summary>
/// <param name="testMethod">The test method to execute.</param>
/// <returns>An array of TestResult objects that represent the outcomes of the test.</returns>
Executes a combinatorial test method.
Executes a combinatorial test method .
public override TestResult[] Execute(ITestMethod testMethod)
        {
            try
            {
                ValidateAttributes(testMethod);
                object[][] args = GetArgumentsInOrder(testMethod);
                ValidateArguments(testMethod, args);
                int totalTestsDigits = ComputeTotalTestCountDigits(args);
                var indexes = new int[args.Length];
                bool done;
                var results = new List<TestResult>();
                do
                {
                    object[] argsToPass = Enumerable.Range(0, args.Length)
                                                    .Select(i => args[i][indexes[i]])
                                                    .ToArray();
                    TestResult result = testMethod.Invoke(argsToPass);
                    string number = (results.Count + 1).ToString().PadLeft(totalTestsDigits, '0');
                    result.DisplayName = $"#{number}: {GetTestDisplayName(testMethod, argsToPass)}";
                    results.Add(result);
                    int updateIdx = indexes.Length - 1;
                    for (; updateIdx >= 0; updateIdx--)
                    {
                        indexes[updateIdx]++;
                        if (indexes[updateIdx] < args[updateIdx].Length)
                        {
                            break;
                        }
                        indexes[updateIdx] = 0;
                    }
                    done = (updateIdx < 0);
                } while (!done);
                return results.ToArray();
            }
            catch (Exception ex)
            {
                return new[]
                {
                    new TestResult
                    {
                        Outcome = UnitTestOutcome.Error,
                        TestFailureException = ex
                    }
                };
            }
        }
public override TestResult [ ] Execute ( ITestMethod testMethod ) { try { ValidateAttributes ( testMethod ) ; object [ ] [ ] args = GetArgumentsInOrder ( testMethod ) ; ValidateArguments ( testMethod , args ) ; int totalTestsDigits = ComputeTotalTestCountDigits ( args ) ; var indexes = new int [ args . Length ] ; bool done ; var results = new List < TestResult > ( ) ; do { object [ ] argsToPass = Enumerable . Range ( 0 , args . Length ) . Select ( i => args [ i ] [ indexes [ i ] ] ) . ToArray ( ) ; TestResult result = testMethod . Invoke ( argsToPass ) ; string number = ( results . Count + 1 ) . ToString ( ) . PadLeft ( totalTestsDigits , ' ' ) ; result . DisplayName = $" # { number } :  { GetTestDisplayName ( testMethod , argsToPass ) } " ; results . Add ( result ) ; int updateIdx = indexes . Length - 1 ; for ( ; updateIdx >= 0 ; updateIdx -- ) { indexes [ updateIdx ] ++ ; if ( indexes [ updateIdx ] < args [ updateIdx ] . Length ) { break ; } indexes [ updateIdx ] = 0 ; } done = ( updateIdx < 0 ) ; } while ( ! done ) ; return results . ToArray ( ) ; } catch ( Exception ex ) { return new [ ] { new TestResult { Outcome = UnitTestOutcome . Error , TestFailureException = ex } } ; } }
Executes a combinatorial test method.
Executes a combinatorial test method .
// get the count of tests so we know how to format the # in the test name. // run the test with the current values. // In case the results are alphabetized (I'm looking at you // VS test explorer), prepend the test # padded with 0s. // update the current indexes. // still values left for this parameter. // carry to the next slot // If we carried off the end of the array, we're done // something went wrong - fail the test with the exception info. // note: I believe the MSTest executor will catch any exceptions that escape // tests, and return a failed TestResult (if it wasn't an ExpectedException).
An array of TestResult objects that represent the outcomes of the test.
An array of TestResult objects that represent the outcomes of the test .
testMethod
ITestMethod
The test method to execute.
The test method to execute .