Objective
In the previous example, it was shown that we can built functional tests for our web code making sure it comply with the needed functionality or that it continues to work as expected after our modifications.
The next step is to permanently store the result as proof that it was checked properly and that it is ready for acceptance tests from our clients.
Ruby Interface with a Database
In this example the result is stored in RTH database (Requirements and Testing Hub) -an open source system-. We’ll use a file (RTH_Verify.rb) that was posted in the original RTH forum page and unfortunatelly no longer available. There are four steps, first the initialization of the odbc interface, creation of a test suite record and update that a test as beeing run, notification of the completed test and close of the connection.
First Part (RTH_Verify.rb)
[This code is from the original developers of RTH]
In this class they initialize the odbc interface to MySQL rth database in a initialize call.
For your instalation you will have to change: yourserver, rth (or your database), yourdbuser, yourdbpassword.
Class Verify require 'dbi' def initialize(projectID, testID, testSetID, testName, testDir, action, expectedResult, actualResult) @projectID = projectID @testID = testID @testSetID = testSetID @testName = testName @testDir = testDir @action = action @expectedResult = expectedResult @actualResult = actualResult @uniqueRunID = "0" #The connection string in your odbc string is: #connectionString = '{DSN=rth;DRIVER=MySQL;SERVER=yourserver;DATABASE=rth;UID=yourdbuser;PWD=yourdbpassword;PORT=3306;}' @dbh = DBI.connect("dbi:ODBC:rth","yourdbuser","yourdbpassword") end
The Second Part (RTH_Verify.rb)
It’s about setting variables: time, sql INSERT string (you can check the file for the complete syntax), sql UPDATE string for the status record. And excecuting the initial notifications of a test being run.
def testStarted projid = @projectID.to_s tsid = @testSetID.to_s tid = @testID.to_s tName = @testName.to_s tDir = @testDir.to_s timeStarted = DateTime.now @uniqueRunID = "S"+Time.now.tv_sec.to_s # First SQL sql = "INSERT INTO ..." + "VALUES(...)" q = @dbh.prepare(sql) q.execute # Second SQL sql = "UPDATE testset_testsuite_assoc SET ... WHERE ..." q = @dbh.prepare(sql) q.execute end
The Thirdh Part (RTH_Verify.rb)
Is the notificaction that the test was compleated, in our watir code it means it was succesfull.
def testCompleted tsid = @testSetID.to_s ttid = @testID.to_s timeFinished = DateTime.now date=timeFinished.strftime("%Y")+"-"+timeFinished.strftime("%m")+ "-"+timeFinished.strftime("%d")+ " " date=date+timeFinished.strftime("%H")+ ":"+timeFinished.strftime("%M")+":"+ timeFinished.strftime("%S") # First SQL sql = "UPDATE testsuiteresults SET ... WHERE ..."; q = @dbh.prepare(sql) q.execute # Second SQL sql="UPDATE testset_testsuite_assoc SET ... WHERE ..." q = @dbh.prepare(sql) q.execute end
Fourth Part (RTH_Verify.rb)
And finally the end of the code to close and disconnect.
def close @dbh.disconnect end end
How It Is Used
This is how I use this code in my tests. You can see that the only change to our code is the requirement for dbi and the RTH_Verify.rb file
# Test with DB result stored require 'watir' require 'test/unit' require 'dbi' require 'RTH_Verify.rb' class TC_recorded < Test::Unit::TestCase def test_toDB ie = Watir::IE.new ie.wait ie.bring_to_front ie.wait
Then the actual test is excecuted. I choose only to record a successfull test, so at the end the rutines are called.
#Here goes the test ie.close #Variables testDir = Dir.getwd testName = File.basename(testDir) # This are RTH specific projectID = 2 testID = 4 stepNum = 1 testSetID = 33 db = Verify.new(projectID, testID, testSetID, testName, testDir, "", "", "") db.testStarted db.writeVerification(stepNum, "Test identification string","Detail test description string","Success Message String", "Pass","Module test string", "N/A", "N/A", "#") db.testCompleted db.close end end
If it the test terminates normally, the test is successfull, and the database has been modified so the ‘results page’ on the RTH system wi’ll show that an automated test exist and user or QA confirmation is needed to change status to ‘signed’.
You can modify this file for your particular system or try RTH which I recommend.