0

私は以下のプログラムを持っています

class Program
    {
        static void Main(string[] args)
        {
            List<Assembly> failedAssemblies = LoadAssembly();
            string batchFile = "D:\\1" + ".bat";
            FileStream fs = File.Create(batchFile);
            fs.Close();

            using (StreamWriter outFile = new StreamWriter(batchFile))
            {
                string command = @"echo off";
                outFile.WriteLine(command);

                Process(outFile, failedAssemblies);

            }           
        }

        private static void Process(StreamWriter outFile, List<Assembly> assembliesList)
        {
            string command = "mstest.exe ";
            string testcontainer = " /testcontainer:";
            List<string> testContainerAssemblies = new List<string>(4);

            outFile.WriteLine("SET Path=%MsTestPath%");

            foreach (Assembly assmbly in assembliesList)
            {

                command = string.Empty;
                if (!testContainerAssemblies.Contains(assmbly.AssemblyName))
                {
                    outFile.WriteLine(' ');

                    testContainerAssemblies.Add(assmbly.AssemblyName);                   
                    command = "mstest.exe ";
                    command += testcontainer + "\\" + assmbly.AssemblyName + " ";
                    command += "/resultsfile:\"" + "\\Resultfile_" + assmbly.AssemblyName.Replace(".dll", "") + "_" + "1".ToString() + ".trx\"";
                    command += " /runconfig:";
                    command += " /detail:owner";
                    command += " /detail:duration";
                    command += " /detail:description";

                    command += " /unique ";
                }

                command += " /test:" + assmbly.NameSpaceName + "." + assmbly.ClassName + "." + assmbly.FunctionName;

                outFile.Write(command);
            }
        }


        private static List<Assembly> LoadAssembly()
        {
            var assemblyCollection = new List<Assembly>();

            assemblyCollection.Add(new Assembly { AssemblyName = "AccountTestBase.dll", NameSpaceName = "ECardTest", ClassName = "ECardTest", FunctionName = "ECardTestDownLoadPKGSuccess" });
            assemblyCollection.Add(new Assembly { AssemblyName = "AccountTestBase.dll", NameSpaceName = "AccountTest", ClassName = "IAccountTest", FunctionName = "Somefunc" });
            assemblyCollection.Add(new Assembly { AssemblyName = "TestPayment.dll", NameSpaceName = "TestPayment", ClassName = "CreditCardTestCases", FunctionName = "BoletoFunctionalTestCase" });
            assemblyCollection.Add(new Assembly { AssemblyName = "TestPayment.dll", NameSpaceName = "TestPayment", ClassName = "CreditCardTestCases", FunctionName = "BoletoEndToEndFunctionalTestCase" });
            assemblyCollection.Add(new Assembly { AssemblyName = "TestPayment.dll", NameSpaceName = "TestPayment", ClassName = "CreditCardTestCases", FunctionName = "BoletoPurcahseTestCase" });
            assemblyCollection.Add(new Assembly { AssemblyName = "TestPayment.dll", NameSpaceName = "TestPayment", ClassName = "CreditCardTestCases", FunctionName = "CreditCard_ResumeOrder_Success" });
            assemblyCollection.Add(new Assembly { AssemblyName = "TestPayment.dll", NameSpaceName = "TestPayment", ClassName = "CreditCardTestCases", FunctionName = "CreditCard_EURCurr_USBilling_ENUS" });
            assemblyCollection.Add(new Assembly { AssemblyName = "TestPayment.dll", NameSpaceName = "TestPayment", ClassName = "CreditCardTestCases", FunctionName = "DinersClubPayment" });

            return assemblyCollection;

        }
    }

    public class Assembly
    {
        public string AssemblyName { get; set; }
        public string ClassName { get; set; }
        public string FunctionName { get; set; }
        public string NameSpaceName { get; set; }

    }

以下の出力を生成します

echo off
SET Path=%MsTestPath%

mstest.exe  /testcontainer:\AccountTestBase.dll /resultsfile:"\Resultfile_AccountTestBase_1.trx" /runconfig: /detail:owner /detail:duration /detail:description /unique  /test:ECardTest.ECardTest.ECardTestDownLoadPKGSuccess /test:AccountTest.IAccountTest.Somefunc 
mstest.exe  /testcontainer:\TestPayment.dll /resultsfile:"\Resultfile_TestPayment_1.trx" /runconfig: /detail:owner /detail:duration /detail:description /unique  /test:TestPayment.CreditCardTestCases.BoletoFunctionalTestCase /test:TestPayment.CreditCardTestCases.BoletoEndToEndFunctionalTestCase /test:TestPayment.CreditCardTestCases.BoletoPurcahseTestCase /test:TestPayment.CreditCardTestCases.CreditCard_ResumeOrder_Success /test:TestPayment.CreditCardTestCases.CreditCard_EURCurr_USBilling_ENUS /test:TestPayment.CreditCardTestCases.DinersClubPayment

ここで、渡された制限に基づいて出力を中断する必要がある新しい要件が発生しました。

Limit が 300 の場合、出力は次のようになります。

echo off
SET Path=%MsTestPath%

mstest.exe  /testcontainer:\AccountTestBase.dll /resultsfile:"\Resultfile_AccountTestBase_1.trx" /runconfig: /detail:owner /detail:duration /detail:description /unique  /test:ECardTest.ECardTest.ECardTestDownLoadPKGSuccess
mstest.exe  /testcontainer:\TestPayment.dll /resultsfile:"\Resultfile_TestPayment_1.trx" /runconfig: /detail:owner /detail:duration /detail:description /unique  /test:TestPayment.CreditCardTestCases.BoletoFunctionalTestCase /test:TestPayment.CreditCardTestCases.BoletoEndToEndFunctionalTestCase 

mstest.exe  /testcontainer:\TestPayment.dll /resultsfile:"\Resultfile_TestPayment_1.trx" /runconfig: /detail:owner /detail:duration /detail:description /unique /test:TestPayment.CreditCardTestCases.BoletoPurcahseTestCase /test:TestPayment.CreditCardTestCases.CreditCard_ResumeOrder_Success /test:TestPayment.CreditCardTestCases.CreditCard_EURCurr_USBilling_ENUS /test:TestPayment.CreditCardTestCases.DinersClubPayment

つまり、最初のものには合計 210 文字 (約) の制限があり、それ以降は同じままです。2 つ目は制限を超えているため、2 つの部分に分割する必要があります。最初のコマンドは 290 を超えて分割されました。コマンドを 1 つ追加すると制限を超えてしまうためです。そのため、2 つの部分に分割する必要があります。

もう 1 つのポイントは、指定された Limit 値に基づいて正確に分割することはできないということです。実行する必要があるのは MSTest コマンドであるためです。今後、

"mstest.exe /testcontainer:\TestPayment.dll /resultsfile:"\Resultfile_TestPayment_1.trx" /runconfig: /detail:owner /detail:duration /detail:description /unique"いつも来て、その後"test/..."

C#で同じことをする方法は?

MyShot(改善してください)

private static void Process(StreamWriter outFile, List<Assembly> failedAssemblies)
{
    int MAXLIMIT = 2000;
    string command = "mstest.exe ";
    string testcontainer = " /testcontainer:";
    List<string> testContainerAssemblies = new List<string>(4);
    int sum = 0;

    outFile.WriteLine("SET Path=%MsTestPath%");


    var failedAssemblyCollections = (from x in failedAssemblies
                                     group x by x.AssemblyName into g
                                     select new
                                     {
                                         AssemblyNames = g.Key,                                      
                                         FullyQualifiedTestMethods = g.Select(i => " /test:" + i.NameSpaceName + "." + i.ClassName + "." + i.FunctionName),
                                         FullyQualifiedTestMethodsLen = g.Select(i => Convert.ToString(" /test:" + i.NameSpaceName + "." + i.ClassName + "." + i.FunctionName).Length)
                                     });

        foreach (var item in failedAssemblyCollections)
        {
            var assemblyNames = item.AssemblyNames;
            var methodsLengths = item.FullyQualifiedTestMethodsLen.ToList();
            var flag = true;
            int counter = 0;

            //write for the very first time
            if (flag)
            {
                Write(outFile, ref command, testcontainer, assemblyNames);
                flag = false;
            }


            for (int i = 0; i < methodsLengths.Count; i++)
            {
                sum += methodsLengths[i];

                if (sum <= MAXLIMIT)
                {
                    command += item.FullyQualifiedTestMethods.ToList()[i];

                    //this will execute when a long command is splitted and is written in new trx files
                    if (flag)
                    {
                        counter++;
                        Write(outFile, ref command, testcontainer, assemblyNames);
                        flag = false;
                    }
                }


                //if the value crosses max limit
                //write the current output
                //then reset variables to original 
                if (sum >= MAXLIMIT)
                {
                    outFile.Write(command);
                    sum = 0;
                    flag = true;
                    i--;
                }
            }
            outFile.Write(command);

        }

}

private static void Write(StreamWriter outFile, ref string command, string testcontainer, string assemblyNames)
{
    outFile.WriteLine(' ');
    command = "mstest.exe ";
    command += testcontainer + "\\" + assemblyNames + " ";  
    command += " /runconfig:";
    command += " /detail:owner";
    command += " /detail:duration";
    command += " /detail:description";

    command += " /unique ";

}
4

2 に答える 2

0
private static void Process(StreamWriter outFile, List<Assembly> failedAssemblies)
{
    int MAXLIMIT = 2000;
    string command = "mstest.exe ";
    string testcontainer = " /testcontainer:";
    List<string> testContainerAssemblies = new List<string>(4);
    int sum = 0;

    outFile.WriteLine("SET Path=%MsTestPath%");


    var failedAssemblyCollections = (from x in failedAssemblies
                                     group x by x.AssemblyName into g
                                     select new
                                     {
                                         AssemblyNames = g.Key,                                      
                                         FullyQualifiedTestMethods = g.Select(i => " /test:" + i.NameSpaceName + "." + i.ClassName + "." + i.FunctionName),
                                         FullyQualifiedTestMethodsLen = g.Select(i => Convert.ToString(" /test:" + i.NameSpaceName + "." + i.ClassName + "." + i.FunctionName).Length)
                                     });

        foreach (var item in failedAssemblyCollections)
        {
            var assemblyNames = item.AssemblyNames;
            var methodsLengths = item.FullyQualifiedTestMethodsLen.ToList();
            var flag = true;
            int counter = 0;

            //write for the very first time
            if (flag)
            {
                Write(outFile, ref command, testcontainer, assemblyNames);
                flag = false;
            }


            for (int i = 0; i < methodsLengths.Count; i++)
            {
                sum += methodsLengths[i];

                if (sum <= MAXLIMIT)
                {
                    command += item.FullyQualifiedTestMethods.ToList()[i];

                    //this will execute when a long command is splitted and is written in new trx files
                    if (flag)
                    {
                        counter++;
                        Write(outFile, ref command, testcontainer, assemblyNames);
                        flag = false;
                    }
                }


                //if the value crosses max limit
                //write the current output
                //then reset variables to original 
                if (sum >= MAXLIMIT)
                {
                    outFile.Write(command);
                    sum = 0;
                    flag = true;
                    i--;
                }
            }
            outFile.Write(command);

        }

}

private static void Write(StreamWriter outFile, ref string command, string testcontainer, string assemblyNames)
{
    outFile.WriteLine(' ');
    command = "mstest.exe ";
    command += testcontainer + "\\" + assemblyNames + " ";  
    command += " /runconfig:";
    command += " /detail:owner";
    command += " /detail:duration";
    command += " /detail:description";

    command += " /unique ";

}
于 2013-04-02T02:02:46.163 に答える
0

私が打ち出した非常に大まかな例はここにあります。行の長さが制限に非常に近いか、正確に制限されている場合、または単一のテストケースが制限を超えている場合などのエッジケースについてはテストしていません。それらの演習はあなたに任せます。

場合によっては、単体テストを記述し、標準ループと長さチェックを吐き出してそれらのテストに合格し、LINQ、正規表現、またはコードのクリーンアップをいじって、保守可能で読みやすいものを取得するのが最も簡単な場合があります。

string input = @"mstest.exe  /testcontainer:\TestPayment.dll /resultsfile:""\Resultfile_TestPayment_1.trx"" /runconfig: /detail:owner /detail:duration /detail:description /unique  /test:TestPayment.CreditCardTestCases.BoletoFunctionalTestCase /test:TestPayment.CreditCardTestCases.BoletoEndToEndFunctionalTestCase /test:TestPayment.CreditCardTestCases.BoletoPurcahseTestCase /test:TestPayment.CreditCardTestCases.CreditCard_ResumeOrder_Success /test:TestPayment.CreditCardTestCases.CreditCard_EURCurr_USBilling_ENUS /test:TestPayment.CreditCardTestCases.DinersClubPayment";

const int LINE_LIMIT = 300;
const string TEST_MARKER = " /test:";

int testIndex = input.IndexOf(TEST_MARKER);
string prefix = input.Substring(0, testIndex);
int availableCharacters = LINE_LIMIT - prefix.Length;

Queue<string> tests = new Queue<string>(input.Substring(testIndex).Split(new []{TEST_MARKER}, StringSplitOptions.RemoveEmptyEntries));

List<string> testSets = new List<string>();
string currentTest = "";
while (tests.Count > 0)
{
    //restores the test marker from splitting
    string test = TEST_MARKER + tests.Dequeue(); 

    if (currentTest.Length + test.Length > availableCharacters)
    {
        testSets.Add(currentTest);
        currentTest = "";
    }
    else
    {
        currentTest += test;
    }
}

if (currentTest != "")
{
    testSets.Add(currentTest);
}

var lines = testSets.Select(t => prefix + t).ToList(); //your line outputs
于 2013-04-01T11:11:27.383 に答える