-1

我需要找到失败的测试用例的数量。这里 TC003 包含(3 个 SUB 测试用例),其中 2 个通过,1 个失败。

我需要计算总测试用例(4)通过:-(3)失败-(1)。我该怎么做。

TC001-Passed
TC002-Passed
TC003-Passed
TC003-Passed
TC003-Failed
TC004-Passed

我有一个数据表,即ItemArray[0]包含 TestCaseID 和ItemArray[3]包含状态

4

4 回答 4

1

好的,这很尴尬,但让我们直截了当:

void Main()
{
    List<string> AllTheTestCasesStrings = new List<string>();
    AllTheTestCasesStrings.Add("TC001 Passed");
    AllTheTestCasesStrings.Add("TC002 Passed");
    AllTheTestCasesStrings.Add("TC003 Passed");
    AllTheTestCasesStrings.Add("TC003 Passed");
    AllTheTestCasesStrings.Add("TC003 Failed");
    AllTheTestCasesStrings.Add("TC004 Passed");

    List<TestCase> AllTheTestCases = new List<TestCase>();

    foreach(string CurrentTest in AllTheTestCasesStrings)
    {
        TestCase NewTest = new TestCase();
        NewTest.Name = CurrentTest.Split(' ')[0];
        NewTest.Passed = CurrentTest.Split(' ')[1].Equals("Passed");

        AllTheTestCases.Add(NewTest);
    }

    int FailedTestCases = AllTheTestCases.Where<TestCase>(x => !x.Passed).GroupBy<TestCase, string>(x => x.Name).Count();
    int PassedTestCases = AllTheTestCases.Where<TestCase>(x => x.Passed).GroupBy<TestCase, string>(x => x.Name).Count();

    Console.WriteLine("Failed: " + FailedTestCases);
    Console.WriteLine("Passed: " + PassedTestCases);
}

public class TestCase
{
    public string Name;
    public bool Passed;
}
于 2012-09-04T12:30:06.220 回答
1

假设您有 LINQ 可用,您可以尝试这样的解决方案,dt您的数据表的列索引0为 testcase-id (1, 2, 3, 4),列索引3为状态 ( Passed, Failed):

var rows = dt.Select();
var results = rows.GroupBy (r => r[0])
    .Select (r => new {
        TestCaseID = r.Key,
        Status = r.Any (x => x[3].Equals("Failed")) ? "Failed" : "Passed"
    } );

var total = results.Count();
var passed = results.Count(r => r.Status == "Passed");
var failed = results.Count (r => r.Status == "Failed");

Console.WriteLine("Total Testcases: " + total);    // 4
Console.WriteLine("Passed: " + passed);            // 3
Console.WriteLine("Failed: " + failed);            // 1
于 2012-09-04T12:34:27.360 回答
1

我有一个数据表,即 ItemArray[0] 包含 TestCaseID 和 ItemArray[3] 包含状态

好的,这是一种使用 DataTable 和的方法Linq-To-DataSet

var tcaseGroups = tbl.AsEnumerable()
    .GroupBy(r => r.Field<String>("TestCaseID"))
    .Select(g => new
    {
        Testcase = g.Key,
        Count = g.Count(),
        CaseInfo = g.GroupBy(r => r.Field<String>("Status"))
                     .Select(gStatus => new
                     {
                         Status = gStatus.Key,
                         Count = gStatus.Count()
                     })
    });

// output the result
foreach (var x in tcaseGroups)
    Console.WriteLine("Testcase:{0}, Count:{1} Results:[{2}]", x.Testcase, x.Count
        , String.Join(",", x.CaseInfo.Select(xx => 
            String.Format("Status:{0} Count:{1}", xx.Status, xx.Count))));
于 2012-09-04T12:36:55.817 回答
1

使用 Linq 按 ItemArray[0] 分组,然后计算 ItemArray[1] 的实例。

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication
{
    internal class Program
    {
        private static void Main()
        {
            //table to mimik your structure
            var table = new List<string[]>(new[]
                {
                    new[] {"TC001", null, null, "Passed"},
                    new[] {"TC002", null, null, "Passed"},
                    new[] {"TC003", null, null, "Passed"},
                    new[] {"TC003", null, null, "Passed"},
                    new[] {"TC003", null, null, "Failed"},
                    new[] {"TC004", null, null, "Passed"}
                });

            //calculate
            var grouped = from itemArray in table
                          //group by the testid
                          group itemArray by itemArray[0]
                          into g select new
                              {
                                  TestCaseId = g.Key,
                                  //add up the passes and fails
                                  PassedCount = g.Count(i => i[3] == "Passed"),
                                  FailedCount = g.Count(i => i[3] == "Failed")
                              };

            //enumerate and output to screen
            foreach (var group in grouped)
            {
                Console.WriteLine("TestCase {0} Passed: {1}, Failed: {2}",
                                  group.TestCaseId, group.PassedCount,
                                  group.FailedCount);
            }
            Console.ReadLine();
        }
    }
}
于 2012-09-04T12:59:25.237 回答