Pratik
Pratik

Reputation: 992

Data driven testing with ruby testunit

I have a very basic problem for which I am not able to find any solution.

So I am using Watir Webdriver with testunit to test my web application. I have a test method which I would want to run against multiple set of test-data.

While I can surely use old loop tricks to run it but that would show as only 1 test ran which is not what I want.

I know in testng we have @dataprovider, I am looking for something similar in testunit.

Any help!!

Here is what I have so far:

[1,2].each do |p|
define_method :"test_that_#{p}_is_logged_in" do
 # code to log in
end
end

This works fine. But my problem is how and where would I create data against which I can loop in. I am reading my data from excel say I have a list of hash which I get from excel something like [{:name =>abc,:password => test},{:name =>def,:password => test}]

Current Code status:

    class RubyTest < Test::Unit::TestCase

    def setup
      @excel_array = util.get_excel_map //This gives me an array of hash from excel
    end

     @excel_array.each do |p|
     define_method :"test_that_#{p}_is_logged_in" do
       //Code to check login
     end
     end

I am struggling to run the loop. I get an error saying "undefined method `each' for nil:NilClass (NoMethodError)" on class declaration line

Upvotes: 1

Views: 912

Answers (1)

vgoff
vgoff

Reputation: 11323

You are wanting to do something like this:

require 'minitest/autorun'

describe 'Login' do
  5.times do |number|
    it "must allow user#{number} to login" do
      assert true # replace this assert with your real code and validation
    end
  end
end

Of course, I am mixing spec and test/unit assert wording here, but in any case, where the assert is, you would place the assertion/expectation.

As this code stands, it will pass 5 times, and if you were to report in story form, it would be change by the user number for the appropriate test.

Where to get the data from, that is the part of the code that is missing, where you have yet to try and get errors.

Upvotes: 1

Related Questions