ruby功能模块测试方法集,仅供参考
2014-12-24 21:12
239 查看
#coding: utf-8
require 'open-uri'
require 'net/http'
require 'nokogiri'
#require 'iconv'
require 'uri'
require 'pathname'
class A
def initialize
@way = ""
end
def a_methods
puts '-----a methods----'
end
end
module TestModule
def test_one
end
def test_two
end
end
class TestFunction
def initialize
out('alert',"欢迎使用本脚本!!")
@messages = ""
@methods = {}
TestFunction.instance_methods(false).each_with_index do |m, i|
@methods[i+1] = m.to_s
end
end
def test_single_class
a = A.new
c = "zhouzhen"
puts a.a_methods
class << a
def test_a
puts "---<#{c}>--test a----"
end
end
puts a.test_a
end
def test_block
out('input', "请输入你的姓名:")
out('start')
params = $stdin.gets.chomp()
puts main_block(params){|name|
name+" 一名工程师!!"
}
out('end')
end
def test_excel
out('start')
sheet1 = open_excel(@url)
sheets = open_excel(@url) {|book| book.worksheets }
puts "1================="
sheet1.each do |row|
puts "----#{row}"
end
puts "2================="
sheets.each_with_index do |sheet, index|
puts "#{2+index+1}================="
puts sheet.name
sheet.each do |row|
puts "----#{row}"
end
end
out('end')
end
def main
out('input',"*. 回车直接查看所有测试模块:")
out('input',"*. 退出:(exit/quit)")
function = $stdin.gets.chomp()
if function.upcase == "EXIT" || function.upcase == "QUIT"
out('alert',"欢迎下次使用本脚本!!")
puts "goodbye!"
else
function = "get_all_function" if function.empty?
self.send(function)
main
end
end
private
def method_missing(name, *arg)
if @methods.has_key?(name.to_s.to_i)
self.send(@methods[name.to_s.to_i])
else
puts "#{name} 这个方法不存在!"
super
end
end
def get_all_function(id=nil)
@methods.collect do |key, value|
if id.nil?
puts "#{key}: #{value}"
else
if id == key
return value
end
end
end
out('input',"*. 请选择指定的方法进行测试:")
end
def open_excel(url,&block)
Spreadsheet.client_encoding = "UTF-8"
begin
book = Spreadsheet.open(url)
if block.nil?
sheets = book.worksheet 0
else
sheets = yield book
end
return sheets
rescue
return nil
end
end
def main_block(name, &block)
new_name = yield name
end
def get_url(request_url)
result = []
request_url.collect do |k, v|
result << "#{k}=#{v}"
end
return result.join("&")
end
def http_request(method, uri, query_hash={})
query = query_hash.map{|k, v| "#{k}=#{v}"}.join('&')
query_escaped = URI.escape(query)
uri_parsed = URI.parse(uri)
http = Net::HTTP.new(uri_parsed.host)
case method.downcase!
when 'get'
return http.get(uri_parsed.path + '?' + query_escaped).body
when 'post'
return http.post(uri_parsed.path, query_escaped).body
end
end
def out(type,message=nil)
if type == 'start'
puts "开始测试...."
elsif type == 'end'
puts "测试结束!!!!"
elsif type == 'input'
elsif type == 'alert'
puts "提示:"
else
puts "==========="
end
puts message unless message.nil?
end
end
#TestFunction.new.send(ARGV[0])
TestFunction.new.main
require 'open-uri'
require 'net/http'
require 'nokogiri'
#require 'iconv'
require 'uri'
require 'pathname'
class A
def initialize
@way = ""
end
def a_methods
puts '-----a methods----'
end
end
module TestModule
def test_one
end
def test_two
end
end
class TestFunction
def initialize
out('alert',"欢迎使用本脚本!!")
@messages = ""
@methods = {}
TestFunction.instance_methods(false).each_with_index do |m, i|
@methods[i+1] = m.to_s
end
end
def test_single_class
a = A.new
c = "zhouzhen"
puts a.a_methods
class << a
def test_a
puts "---<#{c}>--test a----"
end
end
puts a.test_a
end
def test_block
out('input', "请输入你的姓名:")
out('start')
params = $stdin.gets.chomp()
puts main_block(params){|name|
name+" 一名工程师!!"
}
out('end')
end
def test_excel
out('start')
sheet1 = open_excel(@url)
sheets = open_excel(@url) {|book| book.worksheets }
puts "1================="
sheet1.each do |row|
puts "----#{row}"
end
puts "2================="
sheets.each_with_index do |sheet, index|
puts "#{2+index+1}================="
puts sheet.name
sheet.each do |row|
puts "----#{row}"
end
end
out('end')
end
def main
out('input',"*. 回车直接查看所有测试模块:")
out('input',"*. 退出:(exit/quit)")
function = $stdin.gets.chomp()
if function.upcase == "EXIT" || function.upcase == "QUIT"
out('alert',"欢迎下次使用本脚本!!")
puts "goodbye!"
else
function = "get_all_function" if function.empty?
self.send(function)
main
end
end
private
def method_missing(name, *arg)
if @methods.has_key?(name.to_s.to_i)
self.send(@methods[name.to_s.to_i])
else
puts "#{name} 这个方法不存在!"
super
end
end
def get_all_function(id=nil)
@methods.collect do |key, value|
if id.nil?
puts "#{key}: #{value}"
else
if id == key
return value
end
end
end
out('input',"*. 请选择指定的方法进行测试:")
end
def open_excel(url,&block)
Spreadsheet.client_encoding = "UTF-8"
begin
book = Spreadsheet.open(url)
if block.nil?
sheets = book.worksheet 0
else
sheets = yield book
end
return sheets
rescue
return nil
end
end
def main_block(name, &block)
new_name = yield name
end
def get_url(request_url)
result = []
request_url.collect do |k, v|
result << "#{k}=#{v}"
end
return result.join("&")
end
def http_request(method, uri, query_hash={})
query = query_hash.map{|k, v| "#{k}=#{v}"}.join('&')
query_escaped = URI.escape(query)
uri_parsed = URI.parse(uri)
http = Net::HTTP.new(uri_parsed.host)
case method.downcase!
when 'get'
return http.get(uri_parsed.path + '?' + query_escaped).body
when 'post'
return http.post(uri_parsed.path, query_escaped).body
end
end
def out(type,message=nil)
if type == 'start'
puts "开始测试...."
elsif type == 'end'
puts "测试结束!!!!"
elsif type == 'input'
elsif type == 'alert'
puts "提示:"
else
puts "==========="
end
puts message unless message.nil?
end
end
#TestFunction.new.send(ARGV[0])
TestFunction.new.main
相关文章推荐
- Ruby+Watir经验谈: 漫谈针对功能的自动化测试框架
- Cucumber + Watir webdriver + Ruby 功能自动化测试
- 上个星期回学校之前,整理了的一个模块的功能(辅助测试之用)
- 模块管理常规功能自定义系统的设计与实现(56--开源开发测试版发布 )
- WTD测试框架(一)框架功能模块
- Dlib人脸特征点检测性能测试(光照+各部分功能模块运行时间)
- 《Python Web开发测试驱动方法》第2章“unittest模块扩展功能测试”
- 功能、模块质量和非功能性测试
- linux中Apache 2.4.7打开cgi功能模块与测试
- 使用sqlite3(数据库知识)的接口函数完成一个用户注册功能模块设计 要封装成函数,在独立的main中调用测试
- C之基于signal信号的交互式的测试功能模块(触发时机)
- 使用sqlite3的接口函数完成一个用户登录验证功能模块设计 要封装成独立函数,在独立的main中调用测试;
- 模块功能点测试方法模板
- 常规功能和模块自定义系统 (cfcmms)—008测试试用版(6.0.0)下载及使用
- odoo11新开发功能模块测试指南
- [DNN模块开发]让模块支持“导入”“导出”功能
- 企业管理平台开发项目(开源)功能模块 dll 基准源代码
- 功能测试
- 用测试的方式来学习Ruby,有点意思!
- 测试一下DasBlog的异站同步功能