Ruby reads test parameterized data from various data sources

Source: Internet
Author: User
Tags dsn

Parameterization is a commonly used technology in automated testing. It is painful to create a testing framework that does not support parameterization. Qtp already exists, But ruby-like watir and selenium are not supported because these frameworks only provide the most basic Automatic Drive class libraries, execution Management and data management beyond driver are also a subject. Most of the execution frameworks used in selenium and watir similar projects are unit test frameworks, which naturally do not support parameterization. Currently, only junit4 supports parameterization, and testng supports multiple parameterization by default. If you start a project, you can give priority to the languages compatible with these frameworks for automation.

Although watir itself does not support parameterization, Ruby does not support unit tests, parameterization is also required. There is no way to think of a work und. Therefore, a parameterized class is written separately to supplement the shortcomings of the parameterized function. The method is to provide data reading for a unified test data source. This interface is called in unit test, but no specific parameter line is specified. This Parameter Line is configured in a separate configuration file. This allows you to manage the parameter lines used for each test execution in a unified manner. The followingCodeIs used to access the test data in various data sources.

# Encoding: utf-8require 'dbi' require 'odbc _ utf8' def generate_ SQL (table, what = nil, where = nil) what = "*" Unless what where = "1 = 1" Unless where "select % s from % s where % s" % [What, table, where] enddef generate_hash (header, all_data) t_arr = [] all_data.each do | row | t_hash = {} For I in 0 .. header. size-1 do t_hash [header [I] = row [I] end t_arr <t_hash end t_arrenddef select_hash_db (DSN, user, password, DB, SQL) Begin DBH = DBI. connect (DSN, user, password) DBH. do ("use # {dB}") DBH. do ("set names utf8") if DSN. split (':') [1] = "MySQL"… = dbh.exe cute (SQL) Arr = array. new. fetch_hash do | row | arr <row end something. finish arr rescue DBI: databaseerror => E puts "An error occurred" puts "error code: # {e. err} "puts" error message: # {e. errstr} "ensure DBH. disconnect if DBH endendendclass text_adapter def initialize (file_path, SEP = "", col_num = nil, row_num = nil) end def evaluate (ROW = nil) endclass mysql_def adapter initialize (ds_connector, table_name, what = nil, where = nil) @ SQL _str = generate_ SQL (table_name, what, where) @ ds_connector = ds_conneend def get_pars (ROW = nil) dsc_arr = @ ds_connector.split ("#") all_data = select_hash_db (dsc_arr [0], dsc_arr [1], dsc_arr [2], dsc_arr [3], @ SQL _str) If row. class = fixnum all_data [row] else all_data end endendclass excel_adapter def initialize (ds_connector, table_name, what = nil, where = nil) @ connection = win32ole. new ('ADODB. connection ') @ record_set = win32ole. new ('ADODB. recordset ') @ ds_connector = ds_connector @ SQL _str = generate_ SQL (table_name, what, where) end def get_pars (ROW = nil) t_arr = [] @ connection. open (@ ds_connector) @ record_set.open (@ SQL _str, @ connection) @ record_set.fields.count.times do | I | t_arr <@ record_set.fields.item (I ). name end all_data = @ record_set.getrows.transpose all_data = generate_hash (t_arr, all_data) If row. class = fixnum all_data [row] else all_data end endendclass parameter def initialize (ds_connector ctor, table_name, what = nil, where = nil) dsc_arr = ds_connector.split ("#", 2) eval ("@ ADP = # {dsc_arr [0]}. new dsc_arr [1], table_name, what, where ") end def get_pars (ROW = nil) @ ADP. get_pars (ROW) endend

Call method:

 
Par = parameter. New (ds_mysql_connector, 'Demo') P par. get_pars (0)

Configuration File Configuration:

 
# Link the Excel database string excel_dsn =%{ excel_adapter # provider = Microsoft. jet. oledb.4.0; Data Source = % s; extended properties = Excel 5.0;} excel_file =%{ D :\\ 1.xlsx }## connection string ds_mysql_connector =%{ mysql_adapter # DBI: mySQL: shoppingcart: 127.0.0.1 # root # password # shoppingcart} # specify the line number of the parameter for the current running side accident, starting from 0 par_row = 1

You need to modify the path, IP address, database name, user name, and password of the corresponding file.

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.