Ruby中单元测试(Unit Test)方法

Ruby中也提供了单元测试的框架,类似Java中的JUnit,此框架在Ruby中被成为mini test。
我们先看一个例子,这个是我的源代码:

require ‘json’module PMU_INTERFACE	class IUserLoginReq		def initialize(command_id=nil, user_name=nil, user_password=nil, auth_code=nil, token=nil)			@command_id = command_id			@user_name = user_name			@user_password = user_password			@auth_code = auth_code			@token = token		end		def to_json(*a)			{				"json_class" => self.class,				"data" => self.to_json_hash			}.to_json(*a)		end		def to_json_hash			{:command_id => @command_id, :user_name => @user_name, :user_password => @user_password, :auth_code => @auth_code, :token => @token}		end		def self.json_create(json_str)			data = json_str["data"]			new(data["command_id"], data["user_name"], data["user_password"], data["auth_code"], data["token"])		end		attr_accessor :command_id, :user_name, :user_password, :auth_code	end	class IUserLoginResp		def initialize(result=nil, user_name=nil, user_password = nil)			#the login result			@result = result			#the token holding by client			@user_name = user_name			@user_password = user_password		end		def to_json(*a)			{				"json_class" => self.class,				"data" => {:result => @result, :user_name => @user_name, :user_password => @user_password}			}.to_json(*a)		end		def self.json_create(json_str)			data = json_str["data"]			new(data["result"], data["user_name"], data["user_password"])		end		attr_accessor :result, :user_password, :user_name	endend

给上面的源代码写测试代码:
require ‘test/unit’require ‘json’require_relative ‘../../../source/server/service/pmu_interface/app_interface’class TestInterface < Test::Unit::TestCase	def test_user_login_req		req = PMU_INTERFACE::IUserLoginReq.new(1, ‘a@b.com’, ‘aa’, ‘1234’ , ”)		str = req.to_json		req2 = JSON.parse(str)		assert(str != nil && req2.command_id == req.command_id)	end	def test_user_login_resp		req = PMU_INTERFACE::IUserLoginResp.new(1, ‘1234’, ‘1234’)		str = req.to_json		req2 = JSON.parse(str)		assert(str != nil && req2.result == req.result)	endend

我们可以看到,测试类继承了Test::Unit::TestCase类,这个类在test/unit库中,test/unit库是Ruby的系统库,成为mini test。
每个测试方法都是以test开头,这点也与JUnit的规则一致,然后assert也是一致,所以如果你熟悉JUnit,那么做Ruby代码的单元测试就不用学习可以直接写。

还有一点,我们都知道JUnit提供了TestSuite这个类,可以将很多TestCase汇总到一块执行,这个对于持续集成非常有用,因为持续集成需要执行所有的TestCase并输入报告。

要在Ruby中执行TestSuite不是那么简单,因为Ruby内置的库里面没有包含TestSuite,需要额外安装一个第三方的gem(test-unit):

sudo gem install test-unit

安装好了之后,就可以使用TestSuite了:

require ‘test/unit/testsuite’require ‘test/unit/ui/console/testrunner’require_relative ‘./service/pmu_dao/test_dao’require_relative ‘./service/pmu_dao/test_db_conn_pool’require_relative ‘./service/pmu_communication/test_comm8n’require_relative ‘./service/pmu_service/test_user_service’require_relative ‘./service/pmu_interface/test_interface’class PMUTestSuite	def self.suite	suite = Test::Unit::TestSuite.new	suite << TestDBConnPool.suite	suite << TestDAOManager.suite        suite << TestMessageDispatcher.suite        suite << TestMessage.suite        suite << TestUserService.suite        suite << TestInterface.suite		return suite	endendTest::Unit::UI::Console::TestRunner.run(PMUTestSuite)

我们把每个TestCase都返回一个suite对象:suite << TestInterface.suite,然后增加到suite中,并使用TestRunner执行。我们要注意的是,mini test中的TestCase类是没有suite方法的(TestInterface.suite),suite方法是通过require 'test/unit/testsuite'之后,'test/unit/testsuite' 使用了ruby中module 的 mixin特性,给TestCase类增加了suite方法。最后我们看运行结果:[code lang="ruby"]Loaded suite Unnamed TestSuiteStarted..latin1tbl_car_private_infotbl_requesttbl_tasktbl_user_credittbl_user_infotbl_user_logintbl_user_private_info.........EError:test_regist_all_handler(TestMessageDispatcher):ArgumentError: wrong number of arguments (0 for 1) /Users/maoxuepeng/uproject/utopia-project-code/main/source/server/service/pmu_communication/comm8n.rb:125:in `regist_all_handlers' /Users/maoxuepeng/uproject/utopia-project-code/main/test/service/pmu_communication/test_comm8n.rb:33:in `test_regist_all_handler'.FFailure:test_regist_handler_duplicate(TestMessageDispatcher) [/Users/maoxuepeng/uproject/utopia-project-code/main/test/service/pmu_communication/test_comm8n.rb:47]:<false> is not true.............Finished in 0.231342 seconds.26 tests, 25 assertions, 1 failures, 1 errors, 0 pendings, 0 omissions, 0 notifications92.3077% passed112.39 tests/s, 108.07 assertions/s[/code]注意结果中有两个用例,一个是错误一个执行失败。当然,TestSuite的执行结果是可以美化的,但是还没有研究,有兴趣的同学可以研究后交流一下。

本文链接:http://www.yunweipai.com/779.html

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/53147.html

(0)
上一篇 2021年8月6日 17:57
下一篇 2021年8月6日 17:57

相关推荐

发表回复

登录后才能评论