search
HomeDatabaseMysql Tutorial解剖SQLSERVER 第十一篇 对SQLSERVER的多个版本进行自动化测

解剖SQLSERVER 第十一篇 对SQLSERVER的多个版本进行自动化测试(译) http://improve.dk/automated-testing-of-orcamdf-against-multiple-sql-server-versions/ 自从我发布了OrcaMDF Studio,我已经意识到SQL2005和SQL2008之间的一些系统表的差异。 这些差异

解剖SQLSERVER 第十一篇    对SQLSERVER的多个版本进行自动化测试(译)

http://improve.dk/automated-testing-of-orcamdf-against-multiple-sql-server-versions/

自从我发布了OrcaMDF Studio,我已经意识到SQL2005和SQL2008之间的一些系统表的差异。

这些差异导致OrcaMDF 解析失败因为代码是针对 2008 R2的格式的

 

当需要做SQL2005的兼容时,我渐渐意识到我需要扩大多个SQLSERVER版本的测试覆盖,替代之前的只对一个版本的测试。

而且,我也需要对一些特定版本功能进行特定的测试(例如:稀疏列测试只能运行在SQLSERVER2008及以上版本)

 

 

NUnit TestCaseSourceAttribute 解决问题

NUnit支持通过TestCase属性进行内联参数测试。更好的是,对于动态测试用例我们还可以提供参数数据,使用TestCaseSource属性,使用TestCaseSource attribute。

首先,我实现了一个简单的枚举来覆盖我目前工作所支持的版本:

<span>public</span> <span>enum</span><span> DatabaseVersion
{
    SqlServer2005,
    SqlServer2008,
    SqlServer2008R2,
}</span>

 

然后我创建SqlServerTestAttribute类,直接继承自TestCaseSourceAttribute,就像这样:

<span>public</span> <span>class</span><span> SqlServerTestAttribute : TestCaseSourceAttribute
{
    </span><span>private</span> <span>static</span> IEnumerable<testcasedata><span> versions
    {
        </span><span>get</span><span>
        {
            </span><span>foreach</span> (<span>var</span> value <span>in</span> Enum.GetValues(<span>typeof</span><span>(DatabaseVersion)))
                </span><span>yield</span> <span>return</span> <span>new</span><span> TestCaseData(value).SetCategory(value.ToString());
        }
    }

    </span><span>public</span> SqlServerTestAttribute() : <span>base</span>(<span>typeof</span>(SqlServerTestAttribute), <span>"</span><span>versions</span><span>"</span><span>)
    { }
}</span></testcasedata>

 

这个SqlServerTestAttribute类告诉TestCaseSourceAttribute 在私有静态的版本属性(private static)里去找测试用例的源数据。

版本属性里枚举出所有的DatabaseVersion值并一个接一个的返回它们——确保将测试类别设置到DatabaseVersion值的名称


接下来,我将当前的测试转换为使用新的SqlServerTest attribute,替代先前的vanilla NUnit Test attribute:

<span>[SqlServerTest]
</span><span>public</span> <span>void</span><span> HeapForwardedRecord(DatabaseVersion version)
{
    ...
}</span>

这将导致我所有的测试都需要根据DatabaseVersion枚举里面的每个枚举值都运行一次,自动获取输入的版本参数里面的每一个值



支持不同的开发环境
现在,我不想强迫每个人都安装所有版本的SQL Server--他们可能只是想软件支持SQL Server 2005 & 2008R2就够了。在OrcaMDF.Core.Tests项目里,我定义了每种受支持的测试数据库的连接字符串,就像这样

<connectionstrings>
    <clear></clear>
    <add name="<span">"<span>SqlServer2005</span><span>"</span> connectionString=<span>"</span><span>Data Source=.SQL2005;Integrated Security=SSPI</span><span>"</span>/>
    <add name="<span">"<span>SqlServer2008R2</span><span>"</span> connectionString=<span>"</span><span>Data Source=.;Integrated Security=SSPI</span><span>"</span>/>
</add></add></connectionstrings>

 

 

如果一个数据库没有相应的连接字符串(名称对应的DatabaseVersion 枚举值)那么该版本的测试就不会运行。因为这个,我目前忽略SQL Server 2008 并且在我的机器上只安装了SQL 2005 和SQL 2008R2

为了对当前可用数据库进行过滤,我修改了我的测试用例让基类去运行实际的测试,使用lambda表达式:

<span>[SqlServerTest]
</span><span>public</span> <span>void</span><span> HeapForwardedRecord(DatabaseVersion version)
{
    RunDatabaseTest(version, db </span>=><span>
    {
        </span><span>var</span> scanner = <span>new</span><span> DataScanner(db);
        </span><span>var</span> rows = scanner.ScanTable(<span>"</span><span>HeapForwardedRecord</span><span>"</span><span>).ToList();

        Assert.AreEqual(</span><span>25</span>, rows[<span>0</span>].Fieldint>(<span>"</span><span>A</span><span>"</span><span>));
        Assert.AreEqual(</span><span>""</span>.PadLeft(<span>5000</span>, <span>'</span><span>A</span><span>'</span>), rows[<span>0</span>].Fieldstring>(<span>"</span><span>B</span><span>"</span><span>));

        Assert.AreEqual(</span><span>28</span>, rows[<span>1</span>].Fieldint>(<span>"</span><span>A</span><span>"</span><span>));
        Assert.AreEqual(</span><span>""</span>.PadLeft(<span>4000</span>, <span>'</span><span>B</span><span>'</span>), rows[<span>1</span>].Fieldstring>(<span>"</span><span>B</span><span>"</span><span>));
    });
}</span>

 

这个RunDatabase 方法在SqlServerSystemTestBase类里面声明

<span>protected</span> <span>void</span> RunDatabaseTest(DatabaseVersion version, Action<database><span> test)
{
    </span><span>string</span> versionConnectionName =<span> version.ToString();

    </span><span>//</span><span> Only run test for this version if a connection string has been provided</span>
    <span>if</span> (ConfigurationManager.ConnectionStrings[versionConnectionName] == <span>null</span><span>)
        Assert.Inconclusive();

    </span><span>//</span><span> Setup database and store file paths, if we haven't done so already</span>
<span>    ensureDatabaseIsSetup(version);

    </span><span>//</span><span> Run actual test</span>
    <span>using</span> (<span>var</span> db = <span>new</span><span> Database(databaseFiles[version]))
        test(db);
}</span></database>

 


如果对应的连接字符串没有在配置文件里面声明,我们会放弃测试并且会将他标记为不确定的- 根据当前的配置情况我们根本不能运行他。

接下来,ensureDatabaseIsSetup()执行通常的配置代码(详细内容可以参考先前的文章)
尽管这一次每个数据库版本,每个测试固件都需要执行。最后 一个OrcaMDF实例将会被创建并传入实际的测试参数

 

支持不同SQLSERVER版本的特性
正如前面提到的,我需要针对特定SQLSERVER版本的执行的测试方法。
标准的SqlServerTestAttribute 自动枚举所有的DatabaseVersion enumeration里面的值,不过我没有理由再单独创建一个SqlServer2005TestAttribute 就像这样

<span>public</span> <span>class</span><span> SqlServer2005TestAttribute : TestCaseSourceAttribute
{
    </span><span>private</span> <span>static</span> IEnumerable<testcasedata><span> versions
    {
        </span><span>get</span><span>
        {
            </span><span>yield</span> <span>return</span> <span>new</span><span> TestCaseData(DatabaseVersion.SqlServer2005).SetCategory(DatabaseVersion.SqlServer2005.ToString());
        }
    }

    </span><span>public</span> SqlServer2005TestAttribute() : <span>base</span>(<span>typeof</span>(SqlServer2005TestAttribute), <span>"</span><span>versions</span><span>"</span><span>)
    { }
}</span></testcasedata>

那如果需要将测试运行在SQL Server 2008上呢?

<span>public</span> <span>class</span><span> SqlServer2008PlusTestAttribute : TestCaseSourceAttribute
{
    </span><span>private</span> <span>static</span> IEnumerable<testcasedata><span> versions
    {
        </span><span>get</span><span>
        {
            </span><span>foreach</span> (<span>var</span> value <span>in</span> Enum.GetValues(<span>typeof</span><span>(DatabaseVersion)))
                </span><span>if</span>((DatabaseVersion)value >=<span> DatabaseVersion.SqlServer2008)
                    </span><span>yield</span> <span>return</span> <span>new</span><span> TestCaseData(value).SetCategory(value.ToString());
        }
    }

    </span><span>public</span><span> SqlServer2008PlusTestAttribute()
        : </span><span>base</span>(<span>typeof</span>(SqlServer2008PlusTestAttribute), <span>"</span><span>versions</span><span>"</span><span>)
    { }
}</span></testcasedata>

一旦我们有attributes,那么将会比较容易的对于我们的版本运行单独的测试

<span>[SqlServer2008PlusTest]
</span><span>public</span> <span>void</span><span> ScanAllNullSparse(DatabaseVersion version)
{
    RunDatabaseTest(version, db </span>=><span>
    {
        </span><span>var</span> scanner = <span>new</span><span> DataScanner(db);
        </span><span>var</span> rows = scanner.ScanTable(<span>"</span><span>ScanAllNullSparse</span><span>"</span><span>).ToList();  //稀疏列

        Assert.AreEqual(</span><span>null</span>, rows[<span>0</span>].Fieldint?>(<span>"</span><span>A</span><span>"</span><span>));
        Assert.AreEqual(</span><span>null</span>, rows[<span>0</span>].Fieldint?>(<span>"</span><span>B</span><span>"</span><span>));
    });
}</span>

 

对于ReSharper test runner 的支持
为了运行测试,我们需要ReSharper 6.0 因为ReSharper 5.1不支持TestCaseSource attribute。
一旦你执行了测试,你将会看到下面的测试结果(已经支持SQL Server 2005 & 2008 R2 测试)

解剖SQLSERVER 第十一篇    对SQLSERVER的多个版本进行自动化测

每一个测试用例都会自动对多个版本的DatabaseVersion 进行测试(除了解析测试,因为他没有实现SqlServerSystemTestBase 因此不能运行多版本测试)。

因为我没有对 SQL Server 2005作出支持所以大部分对 SQL Server 2005的测试都失败。当我运行测试的时候所有SQL2008的测试都是inconclusive 。

最后,所有对于SQL2008R2的测试都通过了 

 

 

测试过滤
很明显,我们不能总是对所有版本的SQLSERVER进行测试,那太浪费时间了。其中一种禁用对特定版本的测试的方法就是删除连接字符串。

然而,这样依然会产生不明确的输出,而且总是修改配置文件会很麻烦

不幸的是,ReSharper test runner不支持对使用TestCaseSourceAttribute创建的参数化测试的category 过滤。
我已经在 YouTRACK上面开了一个特性要求,我希望ReSharper团队会考虑将这个特性添加到ReSharper6.1。如果你也觉得这个特性很棒,请考虑投票支持

幸运的是,NUnit test runner 不支持这种过滤。在NUnit test runner里打开OrcaMDF.Core.Tests程序集会给你以下结果

 解剖SQLSERVER 第十一篇    对SQLSERVER的多个版本进行自动化测

注意在我们运行测试之前,Nunit怎么知道参数化的测试参数的!同时也需要注意Nunit怎么让DifferingRecordFormats 测试只运行在SQL2008或以上 ,

而FGSpecificClusteredAllocation测试只让运行在SQL2005或以上


解剖SQLSERVER 第十一篇    对SQLSERVER的多个版本进行自动化测

通过显式选择特定的版本类别,我们可以选择运行某些版本,一旦运行了,没有被选择的类别类别头部的小圆点会变成灰色

解剖SQLSERVER 第十一篇    对SQLSERVER的多个版本进行自动化测

可以注意到 运行时间用了89秒,基本上1秒一个测试,98%的时间花费在了lob类型特性的测试上。
由于类别格式,我能够在主要的测试类别上进行选择,从而轻松过滤掉长时间运行的测试项目并只关注快速完成的那些类别。

lob 类型特别需要进行测试因为在数据库启动之前他们涉及大量的磁盘活动,配置表和配置行的创建

 

 

展望未来
添加新版本就犹如安装SQLSERVER那样简单,在配置项里添加一个连接字符串,最后,添加SQLSERVER版本名字进去DatabaseVersion 枚举里。就这么多了。

 

进一步,在某种程度上我需要按顺序测试多种升级途径。基于我自己做的一些测试,一个从SQL Server 2005升级到SQLSERVER2008 R2之后的数据库

可能跟在SQLSERVER2008 R2本地创建的数据库有不同,或者从SQL2008升级到SQL2008R2。因此,我需要测试多种不同的升级途径确保完全的兼容性。

然而,兼容性测试的优先级在我的测试优先级列表里比较低,因为这些兼容性测试会花费很多时间

 

第十一篇完

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL String Types: Storage, Performance, and Best PracticesMySQL String Types: Storage, Performance, and Best PracticesMay 10, 2025 am 12:02 AM

MySQLstringtypesimpactstorageandperformanceasfollows:1)CHARisfixed-length,alwaysusingthesamestoragespace,whichcanbefasterbutlessspace-efficient.2)VARCHARisvariable-length,morespace-efficientbutpotentiallyslower.3)TEXTisforlargetext,storedoutsiderows,

Understanding MySQL String Types: VARCHAR, TEXT, CHAR, and MoreUnderstanding MySQL String Types: VARCHAR, TEXT, CHAR, and MoreMay 10, 2025 am 12:02 AM

MySQLstringtypesincludeVARCHAR,TEXT,CHAR,ENUM,andSET.1)VARCHARisversatileforvariable-lengthstringsuptoaspecifiedlimit.2)TEXTisidealforlargetextstoragewithoutadefinedlength.3)CHARisfixed-length,suitableforconsistentdatalikecodes.4)ENUMenforcesdatainte

What are the String Data Types in MySQL?What are the String Data Types in MySQL?May 10, 2025 am 12:01 AM

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,2)VARCHARforvariable-lengthtext,3)BINARYandVARBINARYforbinarydata,4)BLOBandTEXTforlargedata,and5)ENUMandSETforcontrolledinput.Eachtypehasspecificusesandperformancecharacteristics,sochoose

How to Grant Permissions to New MySQL UsersHow to Grant Permissions to New MySQL UsersMay 09, 2025 am 12:16 AM

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

How to Add Users in MySQL: A Step-by-Step GuideHow to Add Users in MySQL: A Step-by-Step GuideMay 09, 2025 am 12:14 AM

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

MySQL: Adding a new user with complex permissionsMySQL: Adding a new user with complex permissionsMay 09, 2025 am 12:09 AM

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

MySQL: String Data Types and CollationsMySQL: String Data Types and CollationsMay 09, 2025 am 12:08 AM

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

MySQL: What length should I use for VARCHARs?MySQL: What length should I use for VARCHARs?May 09, 2025 am 12:06 AM

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software