我正在尝试使用 tSQLtAssertResultSetsHaveSameMetaData
从返回大量列的查询中检查元数据。
当它失败时,详细说明“预期/但是”详细信息的消息将被截断,因此我无法通过这两条信息来查看问题所在。有没有办法输出消息以使其不被截断(例如,到文件)?
我正在尝试使用 tSQLtAssertResultSetsHaveSameMetaData
从返回大量列的查询中检查元数据。
当它失败时,详细说明“预期/但是”详细信息的消息将被截断,因此我无法通过这两条信息来查看问题所在。有没有办法输出消息以使其不被截断(例如,到文件)?
这取决于您实际测试的内容。我同意在广泛的结果集上,AssertResultsSetsHaveSameMetaData 的输出可能有点笨拙。对于存储过程,您可以这样编写测试:
创建过程 [ProcedureTests].[测试 SelectProcedure 结果集合同] 作为 开始 创建表#expected ( 名称 varchar(500) 不为空 , column_ordinal int not null identity(1,1) , system_type_name varchar(500) 不为空 , 可空性 varchar(16) not null ) ; 具有预期Cte(名称,system_type_name,Nullability) 作为 ( 选择 'ItemId' 、 'int' 、 'not null' union all select 'ActorId' , 'int' , 'not null' union all select 'LanId' , 'nvarchar(200)' , 'not null' union all select 'ConsumerId' , 'int' , 'not null' union all select 'ConsumerMoniker' , 'nvarchar(200)' , 'not null' union all select 'ProfileTypeId' , 'int' , 'not null' union all select 'ProfileTypeName' , 'varchar(50)' , 'not null' union all select 'ProfileId' , 'int' , 'not null' ) 插入#预期 ( 姓名 , 系统类型名称 , 可空性 ) 从 expectedCte 中选择名称、system_type_name、Nullability; ——!行为 选择 姓名 , column_ordinal , 系统类型名称 , case is_nullable when 1 then 'null' else 'not null' end as [Nullability] 进入 #实际的 从 sys.dm_exec_describe_first_result_set_for_object(object_id('mySchema.SelectProcedure'), 0); ——!断言 执行 tSQLt.AssertEqualsTable #expected, #actual; 结尾; 去
虽然对于视图,您可以使用这种(略有不同)的方法:
更改过程 [ViewTests].[测试 ViewName 结果集合同] 作为 开始 创建表 [ViewTests].[预期] ( TransactionId int 不为空 , SourceId int 不为空 , SourceKey nvarchar(50) 不为空 , TransactionTypeId int 不为空 , TransactionStatusId int 不为空 , LastModified 日期时间不为空 ); ——!您的比较可能就像这样简单(但请参阅下面的替代方法) exec tSQLt.AssertEqualsTableSchema '[ViewTests].[expected]', 'mySchema.ViewName'; ——! ——!似乎 dm_exec_describe_first_result_set 上的 is_nullable 列(由 tSQLt.AssertEqualsTableSchema 使用) ——!在涉及视图的地方可能有点不稳定,因此您可能需要在测试时忽略可空性 ——!此视图(因此在两个 SELECT 中注释掉该列) ——! 选择 c.name 为 [ColumnName] , c.column_id 作为 [ColumnPosition] , 案子 当 st.name in ('char', 'varchar', 'varbinary') 然后 st.name + '(' + case when c.max_length = -1 then 'max' else coalesce(cast(c.max_length as varchar(8)), '???') end + ')' 当 st.name in ('nchar', 'nvarchar') 然后 st.name + '(' + case when c.max_length = -1 then 'max' else coalesce(cast(c.max_length / 2 as varchar(8)), '???') end + ')' 当 st.name in ('decimal', 'numeric') 然后 st.name + '(' + coalesce(cast(c.precision as varchar(8)), '???') + ',' + coalesce(cast(c.scale as varchar(8)), '? ??') + ')' 当 st.name in ('time', 'datetime2', 'datetimeoffset') 然后 st.name + '(' + coalesce(cast(c.precision as varchar(8)), '???') + ')' 否则 st.name 以 [DataType] 结尾 , c.[精度] 为 [NumericScale] , c.scale 为 [NumericPrecision] , c.collation_name 为 [CollationName] , cast(case c.is_nullable when 1 then 'null' else 'not null' end as varchar(16)) as [Nullability] 进入 #预期的 从 sys.columns 为 c 内连接 sys.types as st 在 st.system_type_id = c.system_type_id 和 st.user_type_id = c.user_type_id 在哪里 c.[object_id] = object_id('[ViewTests].[expected]') 选择 名称为 [ColumnName] , column_ordinal 为 [ColumnPosition] , system_type_name 为 [DataType] , [精度]as [NumericScale] , 缩放为 [NumericPrecision] , collation_name 为 [CollationName] , cast(case is_nullable when 1 then 'null' else 'not null' end as varchar(16)) as [Nullability] 进入 #实际的 从 sys.dm_exec_describe_first_result_set('select * from mySchema.ViewName, null, null) exec tSQLt.AssertEqualsTable '#expected', '#actual' ; 结尾 去
即使发生任何故障,原因也会更清楚,因为输出更像 AssertEqualsTable。
您可以EXEC [tSQLt].[XmlResultFormatter];
在运行测试之前尝试。这旨在用于“构建服务器”场景,但可能会被压入服务以向您显示 SSMS 中的更多输出。