Application and exploration of Python in database testing

Source: Internet
Author: User
Tags connect python script

The Python (Python) language is an object-oriented, very versatile programming language with a very clear syntax that works for a variety of operating systems and can be run on systems such as Windows and UNIX. Currently in the international very popular, is getting more and more applications. Python can accomplish many tasks and is very powerful.

For our testing work, Python's most attractive features are as follows:

1 ability to have language adhesives

2 explanation of the enforcement mechanisms

3 The grammar is simple and easy to learn

4 relatively high performance

Language binders are more figurative, specifically, Python supports the goal of executing code in an existing dynamic library in a Python script by introducing its own Cytpes library. Therefore, the DM can be provided by the external interface based on C + + to be accessed through this method, if the use of Python as a carrier of interface testing, the resulting test will be easier than the use of compiled language test program to modify debugging.

The above is for the C + + interface, for the. NET platform and the Java platform, we can use Python for. NET and Jython separately. The former allows Python scripts to invoke CLR code directly, which allows Python to invoke any Java class library directly.

Believe this, the advantage of using Python as a test tool across languages is already clear:

For functional and performance testing, we just need to write an interpreter call script ourselves, we can just maintain a set of test scripts, and can be tested on any of the driver interfaces, the idea is generally as shown in the following figure:

In the above diagram, the test engineer only needs to close the new top-level testcasen.py, and their written use cases can be pure test logic without concern for the implementation of different interfaces, such as a simple sample script that can be as follows:

Testcase1.py:
from dbop import * #这里的dbop就是odbc版本的连接与支持类
from dmServer import * #dmServer类用于对被测试的服务器进行启动、停止、配置等操作
from ctypes import *
rc = -10;
dmserver1 = dmServer('d:\\dmdbms\\bin\\dmserver.exe','win','12345','service',0);
dmserver1.runserver();
tb1 = dbop();
rc = tb1.connect('127.0.0.1','SYSDBA','SYSDBA','12345','SYSTEM');
rc = tb1.exec_sql_ignore("drop table aa;");#执行drop语句而不关心是否执行成功
rc = tb1.exec_sql("create table aa(c int);");#执行create语句且预期成功
rc = tb1.exec_sql_failed("create table aa(c int);");#再次执行该语句,预期失败
k=((1,1),(1,1));
#执行select且预期结果集与上面定义的k相同
rc = tb1.exec_sql_with_result("select top 2 name,id from systables;",k);
rc = tb1.disconnect();
dmserver1.stopserver();

As you can see, the person writing the use case is not at all concerned about how the connect,exec_sql_ignore,tb1.exec_sql_failed of this class can be implemented, as long as they use and give the logic of the test. The test developer will implement an ODBC version, a JDBC version, and a ado.net version of these interfaces, for example.

Between the test case and the support library is a Testloader program that completes these several things:

1, read the configuration file based on the test to be executed, determine the location of the interpreter and what kind of driver will be used;

2. Read the contents of the test case and change the reference to the support class to the corresponding driver, as in the script above:

From Dbop Import * may be changed to: from DBOP_JDBC import *

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.