私は以下のプログラムを持っています
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 ";
}