Introduction to the MySQL Test Framework
MySQL distributions include a test suite: a set of test cases and programs for running them.
MySQL发行版包括一个测试套件:一组用于运行它们的测试用例和程序。
(If you find that the test suite is not included in your distribution, look for a similar distribution with -test in the name and install that as well.) These tools constitute the MySQL test framework that provides a means for verifying that MySQL Server and its client programs operate according to expectations. The test cases consist mostly of SQL statements, but can also use test language constructs that control how to run tests and verify their results. Distributions also provide facilities for running unit tests and creating new unit tests.
(如果您发现该测试套件不包含在您的发行版中,请查找名称中带有-test的类似发行版并安装它。)这些工具构成了MySQL测试框架,提供了一种方法来验证MySQL服务器及其客户端程序是否按照预期运行。测试用例主要由SQL语句组成,但也可以使用控制如何运行测试和验证结果的测试语言结构。发行版还提供了运行单元测试和创建新单元测试的工具。
This document describes the components of the MySQL test framework, how the test programs work, and the language used for writing test cases. It also provides a tutorial for developing test cases and executing them.
本文档描述了MySQL测试框架的组成部分,测试程序如何工作,以及用于编写测试用例的语言。它还提供了开发测试用例和执行它们的教程。
The application that runs the test suite is named mysql-test-run.pl. Its location is the mysql-test directory, which is present both in source and binary MySQL Server distributions.
运行测试套件的应用程序名为mysql-test-run.pl。它的位置是MySQL -test目录,它在源码和二进制MySQL服务器发行版中都有。
On platforms other than Windows, mysql-test-run.pl is also available through the shortened name mtr in the same directory, as either a symbolic link or a copy.
在Windows以外的平台上,也可以通过同一个目录中的简短名称mtr使用mysql-test-run.pl,可以作为符号链接或副本使用。
The mysql-test-run.pl application starts MySQL servers, restarts them as necessary when a specific test case needs different start arguments, and presents the test result. For each test case, mysql-test-run.pl invokes the mysqltest program (also referred to as the “test engine”) to read the test case file, intepret the test language constructs, and send SQL statements to the server.
mysql-test-run.pl 应用程序启动MySQL服务器,当特定的测试用例需要不同的启动参数时,根据需要重新启动它们,并显示测试结果。对于每个测试用例,mysql-test-run.pl调用mysqltest程序(也称为“测试引擎”)来读取测试用例文件,解释测试语言构造,并将SQL语句发送到服务器。
Input for each test case is stored in a file, and the expected result from running the test is stored in another file. The actual result is compared to the expected result after running the test.
每个测试用例的输入存储在一个文件中,而运行测试的预期结果存储在另一个文件中。运行测试后,将实际结果与预期结果进行比较。
For a MySQL source distribution, mysql-test-run.pl is located in the mysql-test directory, and mysqltest is located in the client directory. The mysql-test and client directories are located in the root directory of the distribution.
对于MySQL源码,MySQL -test-run.pl位于MySQL -test目录下,而mysqltest位于客户端目录下。mysql-test和client目录位于发行版的根目录中。
For a MySQL binary distribution, mysql-test-run.pl is located in the mysql-test directory, and mysqltest is located in the same directory where other client programs such as mysql or mysqladmin are installed. The locations of the mysql-test and other directories depend on the layout used for the distribution format.
对于MySQL二进制版本,MySQL -test-run.pl位于MySQL -test目录下,mysqltest与其他客户端程序(如MySQL或mysqladmin)位于同一个目录下。mysql-test和其他目录的位置取决于用于分发格式的布局。
Within the mysql-test directory, test case input files and result files are stored in the t and r directories, respectively. The input and result files have the same basename, which is the test name, but have extensions of .test and .result, respectively. For example, for a test named “decimal”, the input and result files are mysql-test/t/decimal.test and mysql-test/r/decimal.result.
在mysql-test目录中,测试用例输入文件和结果文件分别存储在t和r目录中。输入文件和结果文件具有相同的基本名,即测试名称,但扩展名分别为.test和.result。例如,对于一个名为“decimal”的测试,输入和结果文件是mysql-test/t/decimal.test 和mysql-test / r / decimal.result。
Each test file is referred to as one test case, but usually consists of a sequence of related tests. An unexpected failure of a single statement in a test case makes the test case fail.
每个测试文件被称为一个测试用例,但是通常由一系列相关的测试组成。测试用例中单个语句的意外失败会导致测试用例失败。
There are several ways a test case can fail:
测试用例可能会以几种方式失败:
The mysqltest test engine checks the result codes from executing each SQL statement in the test input. If the failure is unexpected, the test case fails.
mysqltest测试引擎检查执行测试输入中的每个SQL语句的结果代码。如果失败是意外的,测试用例就会失败。
A test case can fail if an error was expected but did not occur (for example, if an SQL statement succeeded when it should have failed).
如果预期出现错误但没有发生错误,测试用例可能会失败(例如,如果SQL语句在应该失败的情况下成功了)。
The test case can fail by producing incorrect output. As a test runs, it produces output (the results from SELECT, SHOW, and other statements). This output is compared to the expected result found in the mysql-test/r directory (in a file with a .result suffix). If the expected and actual results differ, the test case fails. The actual test result is written to a file in the mysql-test/var/log directory with a .reject suffix, and the difference between the .result and .reject files is presented for evaluation.
测试用例可能会因为产生不正确的输出而失败。当测试运行时,它会产生输出(SELECT、SHOW和其他语句的结果)。该输出将与mysql-test/r目录(后缀为.result的文件)中的预期结果进行比较。如果预期的和实际的结果不同,测试用例就失败了。实际的测试结果被写入mysql-test/var/log目录下的一个文件中,后缀为.reject,并且.result和.reject文件之间的差异会被显示出来以供评估。
A test case will fail if the MySQL server dies unexpectedly during the test. If this happens, the mysqltest test client will usually also report a failure due to loosing the connection.
如果MySQL服务器在测试过程中意外死亡,测试用例将失败。如果发生这种情况,mysqltest测试客户端通常也会报告一个由于失去连接而导致的失败。
Finally, the test case will fail if the error log written by the MySQL server during the test includes warnings or errors which are not filtered (suppressed). See Suppressing Errors and Warning for more about suppressing warnings.
最后,如果测试期间由MySQL服务器编写的错误日志包含未过滤(抑制)的警告或错误,则测试用例将失败。有关抑制警告的更多信息,请参阅抑制错误和警告。
This method of checking test results puts some restrictions on how test cases can be written. For example, the result cannot contain information that varies from run to run, such as the current time. However, if the information that varies is unimportant for test evaluation, there are ways to instruct the test engine to replace those fields in the output with fixed values.
这种检查测试结果的方法对如何编写测试用例施加了一些限制。例如,结果不能包含随运行而变化的信息,例如当前时间。但是,如果变化的信息对测试评估不重要,那么有一些方法可以指示测试引擎将输出中的这些字段替换为固定值。
Because the test cases consist mostly of SQL statements in a text file, there is no direct support for test cases that are written in C, Java, or other languages. Such tests are not within the scope of this test framework. But the framework does support executing your own scripts and initiating them with your own data. Also, a test case can execute an external program, so in some respects the test framework can be extended for uses other than testing SQL statements. Finally, it is possible to embed small pieces of Perl code within the test. This can sometimes be used to perform actions or execute logic which is beyond the capabilities of the test language or SQL.
因为测试用例主要由文本文件中的SQL语句组成,所以对用C、Java或其他语言编写的测试用例没有直接的支持。这样的测试不在这个测试框架的范围内。但是框架支持执行您自己的脚本并使用您自己的数据初始化它们。此外,测试用例可以执行外部程序,因此在某些方面,测试框架可以扩展为测试SQL语句以外的用途。最后,可以在测试中嵌入一小段Perl代码。这有时可以用于执行超出测试语言或SQL能力的操作或逻辑。