2

我创建了一个简单的数据库来存储测试结果,但是我正在努力使用 SQL 计数来汇总我的通过/失败/警告来呈现这个。

这个想法是测试运行(TRID)将有几个测试集(TSID),每个测试集都有几个测试用例(TCID)。

  1. 该 TSID 的测试用例总数应该相等
  2. 通过的测试用例数量应该相等,所有步骤的 StatusID=1
  3. Fail 应该与 StatusID=2 的 1 个或多个步骤的测试用例数量相等。
  4. 警告应该有相同数量的测试用例,1 个或多个 StatusID=3 步骤,但相同的测试用例应该有零失败。如果有一个失败的步骤,那么测试用例应该按照上面的方法失败。

SQL 创建我的结果表的简化示例:-

create table Results (StatusID int, TRID int, TSID int, TCID int);

--Test Set 1 / Test Case 1.
INSERT INTO Results VALUES (1, 1, 1, 1)
INSERT INTO Results VALUES (1, 1, 1, 1)
INSERT INTO Results VALUES (1, 1, 1, 1)
--Test Set 1 / Test Case 2
INSERT INTO Results VALUES (1, 1, 1, 2)
INSERT INTO Results VALUES (1, 1, 1, 2)

--Test Set 2 / Test Case 1
INSERT INTO Results VALUES (1, 1, 2, 1)
INSERT INTO Results VALUES (1, 1, 2, 1)
INSERT INTO Results VALUES (1, 1, 2, 1)
--Test Set 2 / Test Case 2
INSERT INTO Results VALUES (1, 1, 2, 2)
INSERT INTO Results VALUES (2, 1, 2, 2)

--Test Set 3 / Test Case 1
INSERT INTO Results VALUES (1, 1, 3, 1)
INSERT INTO Results VALUES (1, 1, 3, 1)
INSERT INTO Results VALUES (1, 1, 3, 1)
--Test Set 3 / Test Case 2
INSERT INTO Results VALUES (1, 1, 3, 2)
INSERT INTO Results VALUES (3, 1, 3, 2)

--Test Set 4 / Test Case 1
INSERT INTO Results VALUES (1, 1, 4, 1)
INSERT INTO Results VALUES (1, 1, 4, 1)
INSERT INTO Results VALUES (1, 1, 4, 1)
--Test Set 4 / Test Case 2
INSERT INTO Results VALUES (3, 1, 4, 2)
INSERT INTO Results VALUES (2, 1, 4, 2)

SELECT * FROM Results

我当前的 SQL(您将看到它为我提供了错误的警告计数:-

DECLARE @trid INT
SET @trid = 1

SELECT TRID, TSID,
(SELECT COUNT(DISTINCT TCID) FROM Results WHERE R.TRID = @trID AND R.TSID = TSID) As Total,
(SELECT COUNT(DISTINCT TCID) FROM Results WHERE TRID = @trID AND R.TSID = TSID) - (SELECT COUNT(DISTINCT TCID) FROM Results WHERE TRID = @trID AND R.TSID = TSID AND StatusID = 2) - (SELECT COUNT(DISTINCT TCID) FROM Results WHERE TRID = @trID AND R.TSID = TSID AND StatusID = 3 AND (SELECT COUNT(DISTINCT TCID) FROM Results WHERE TRID = @trID AND R.TSID = TSID AND StatusID = 2) = 0) AS Pass,
(SELECT COUNT(DISTINCT TCID) FROM Results WHERE R.TSID = TSID AND StatusID=2) As Fail,
(SELECT COUNT(DISTINCT TCID) FROM Results WHERE R.TSID = TSID AND StatusID=3) As Warning
FROM Results R
WHERE TRID = @TRID
GROUP BY TRID, TSID

从上面的SQL来看,目前不正确的结果是:-

TRID    TSID    Total   Pass    Fail    Warning
1       1       2       2       0       0
1       2       2       1       1       0
1       3       2       1       0       1
1       4       2       1       1       1*

结果应该是......

TRID    TSID    Total   Pass    Fail    Warning
1       1       2       2       0       0
1       2       2       1       1       0
1       3       2       1       0       1
1       4       2       1       1       0*  

谢谢

4

1 回答 1

4

您可以在子查询中计算每个测试用例 (TCID) 的统计信息。然后,外部查询可以计算每个测试集 (TSID) 的统计信息。例如:

select  TRID
,       TSID
,       count(*) as Total
,       sum(case when FailSteps = 0 and WarnSteps = 0 then 1 else 0 end) as Pass
,       sum(case when FailSteps > 0 then 1 else 0 end) as Fail
,       sum(case when FailSteps = 0 and WarnSteps > 0 then 1 else 0 end) as Warning
from    (
        select  TRID
        ,       TSID
        ,       TCID
        ,       sum(case when StatusID = 1 then 1 else 0 end) as PassSteps
        ,       sum(case when StatusID = 2 then 1 else 0 end) as FailSteps
        ,       sum(case when StatusID = 3 then 1 else 0 end) as WarnSteps
        from    Results
        group by
                TRID
        ,       TSID
        ,       TCID
        ) as TestCases
group by
        TRID
,       TSID

SQL Fiddle 上的实时示例。

于 2013-02-04T09:33:16.273 回答