如何在 tSQLt 中查看测试结果?

How to view test results in tSQLt?

我们正在使用 tSQLt 对我们的数据库进行单元测试并在下面执行:

EXEC tSQLt.Run '[testComplianceDimensions].[test CountOfPropertiesWithLatestRepairJob]'
EXEC tSQLt.Run '[testComplianceDimensions].[test FactPropertyLatestRepairJobAgg]'

这是输出消息:

(1 row(s) affected)
[testComplianceDimensions].[test CountOfPropertiesWithLatestRepairJob] failed: (Failure) Fact_PropertyLatestRepairJobAgg failure - property counts do not match Expected: <324211> but was: <0>
 
+----------------------+
|Test Execution Summary|
+----------------------+
 
|No|Test Case Name                                                        |Dur(ms)|Result |
+--+----------------------------------------------------------------------+-------+-------+
|1 |[testComplianceDimensions].[test CountOfPropertiesWithLatestRepairJob]|  16037|Failure|
-----------------------------------------------------------------------------
Msg 50000, Level 16, State 10, Line 1
Test Case Summary: 1 test case(s) executed, 0 succeeded, 1 failed, 0 errored.
-----------------------------------------------------------------------------

(1 row(s) affected)
 
+----------------------+
|Test Execution Summary|
+----------------------+
 
|No|Test Case Name                                                  |Dur(ms)|Result |
+--+----------------------------------------------------------------+-------+-------+
|1 |[testComplianceDimensions].[test FactPropertyLatestRepairJobAgg]|     14|Success|
-----------------------------------------------------------------------------
Test Case Summary: 1 test case(s) executed, 1 succeeded, 0 failed, 0 errored.
-----------------------------------------------------------------------------

是否有table保存所有测试结果的地方?

感谢您的帮助。

您可以尝试使用 tSQLt.SetTestResultFormatter 更改输出格式并由另一个应用程序使用结果:

There is currently no way to change the layout of the default output. however, there is an xml format generator that returns the test results in a JUnit compatible format. That can be used to run tSQLt tests inside a CI environment. See https://www.simple-talk.com/sql/sql-tools/using-sql-test-database-unit-testing-with-teamcity-continuous-integration/ for an example implementation.

正如所解释的那样 tSQLt.SetTestResultFormatter

is an extension point that allows the output to be formatted in different ways. There is for example an XML formatter that you can use to generate JUnit compatible output instead of human readable. We have not stabilized that piece yet. that is, why it is not documented yet. The goal is to allow external formatters to be installed through this as extension to the existing ones. However, you can get the XML output through other means. See for example http://tsqlt.org/177/integrating-tsqlt-with-cruise-control/

如果这不是您要找的,您将在 GitHub repository.

中获得更多帮助

此外,在投资 tSQLt 之前,您可能会停下来三思而后行,因为实施您自己的解决方案不会那么困难,而且您将拥有更多控制权(就像我们所做的那样)。

table tSQLt.TestResult 包含最后一次执行 tSQLt.RuntSQLt.RunAll

的结果

table 的内容不会在这些过程的执行之间保留 - 在您的示例中,table 将包含第一个测试的结果,直到执行第二个测试。如果要收集多个测试的结果,则需要将测试作为同一命令的一部分执行 - 在这种情况下,可能使用 EXEC tSQLt.Run '[testComplianceDimensions]'(这将 运行 中的所有测试套间)

我能够相当简单地修改我的 tSQLt 安装以将结果记录到另一个数据库 table 使用 @gotqn 建议的方法来更改输出格式。 请注意,我还必须修改另一个过程以允许 RedGate 的 SQL 测试 UI 到 post 结果。

我在这里开始了我的旅程:https://groups.google.com/g/tsqlt/c/qP1iazrp-pE

这让我创建了一个脚本,我在每个新的 tSQLt 安装上 运行 以便将所有结果收集到一个 table 中,以及其他指标帮助我们构建用于测试的仪表板 passes/failures.

[tSQLt].[NullTestResultFormatter] 已修改,因为 运行ning 测试通过 RedGate 的 SQL 测试 UI 使用此格式化程序。 如果您没有使用 RedGate 的 SQL 测试 UI 而只是执行 EXEC tSQLt.Run* 那么您可能 ignore/delete 这一步。

[tSQLt].[SaveTestResultFormatter] 是我创建的新 Formatter。只需将最后的测试结果转储到我的 table.

EXECUTE @RC = [tSQLt].[SetTestResultFormatter] @Formatter 将默认格式器更改为我的新格式器。

这是我 运行 在每个新的 tSQLt 安装上的完整代码块。它假定数据库和 table 存在:[tSQLtRunHistory].[dbo].[TestResults]

/*
tSQLt Customization - Logging of TestResult
This Script, when run on a database with tSQLt installed, will modify
the ResultFormatters to insert the TestResult data to an external
database (tSQLtRunHistory.dbo.TestResults)
*/

USE --CHOOSE DATABASE TO ENABLE TEST LOGGING
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER   PROCEDURE [tSQLt].[NullTestResultFormatter]
AS
BEGIN

    --Manually added for custom reporting of each test result
    --Changing this allows RedGate SQL Test to post, since it runs tests with the NullFormatter, not the DefaultFormatter
    --Adds the test result to another table - doesn't seem to post when running tests via the GUI, but does when tSQLt.RunAll is executed
    INSERT INTO [tSQLtRunHistory].[dbo].[TestResults]
        (
            [Server]
            , [LoginUserName]
            , [HostName]
            , [Database]
            , [Class]
            , [TestCase]
            , [TranName]
            , [Result]
            , [Msg]
            , [TestStartTime]
            , [TestEndTime]
        )
    SELECT
        @@SERVERNAME
        , SUSER_NAME()
        , HOST_NAME()
        , DB_NAME()
        , [Class]
        , [TestCase]
        , [TranName]
        , [Result]
        , [Msg]
        , [TestStartTime]
        , [TestEndTime]
    FROM    [tSQLt].[TestResult];
    --END of Manually added

    RETURN 0;
END;
GO

CREATE OR ALTER PROCEDURE [tSQLt].[SaveTestResultFormatter]
AS
BEGIN
    --Custom ResultFormatter which adds each TestResult to a central database for cataloguing
    --Adds the test result to another table
    INSERT INTO [tSQLtRunHistory].[dbo].[TestResults]
        (
            [Server]
            , [LoginUserName]
            , [HostName]
            , [Database]
            , [Class]
            , [TestCase]
            , [TranName]
            , [Result]
            , [Msg]
            , [TestStartTime]
            , [TestEndTime]
        )
    SELECT
        @@SERVERNAME
        , SUSER_NAME()
        , HOST_NAME()
        , DB_NAME()
        , [Class]
        , [TestCase]
        , [TranName]
        , [Result]
        , [Msg]
        , [TestStartTime]
        , [TestEndTime]
    FROM    [tSQLt].[TestResult];
END;
GO

DECLARE @RC INT
DECLARE @Formatter NVARCHAR(4000)

SET @Formatter = 'tSQLt.SaveTestResultFormatter' --'tSQLt.DefaultResultFormatter'

EXECUTE @RC = [tSQLt].[SetTestResultFormatter] 
   @Formatter
GO

作为参考,我的 [dbo].[TestResults] table 创建脚本是:

USE [tSQLtRunHistory]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[TestResults](
    [Id] [INT] IDENTITY(1,1) NOT NULL,
    [Server] [NVARCHAR](MAX) NOT NULL,
    [LoginUserName] [NVARCHAR](MAX) NOT NULL,
    [HostName] [NVARCHAR](MAX) NOT NULL,
    [Database] [NVARCHAR](MAX) NOT NULL,
    [Class] [NVARCHAR](MAX) NOT NULL,
    [TestCase] [NVARCHAR](MAX) NOT NULL,
    [TranName] [NVARCHAR](MAX) NOT NULL,
    [Result] [NVARCHAR](MAX) NULL,
    [Msg] [NVARCHAR](MAX) NULL,
    [TestStartTime] [DATETIME] NOT NULL,
    [TestEndTime] [DATETIME] NULL,
PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO