0

I need to share test code between a set of suites, but the thing is that I don't know how to define the test cases in the separate file so every other test suite can use the same test cases.

This is a sample of my files:

test_api.rb:

require_relative 'test_helper'
require 'rubygems'
require 'minitest/spec'
require 'minitest/autorun'
#require 'minitest/hell'
require 'uri'
require 'net/http'
require 'mysql2'
require 'json'
require 'digest/sha1'
require_relative 'cases_api_shared.rb'

class API_pc_qubit
    #parallelize_me!
    def setup

        ### DEFINITIONS

        ### URL
        ENV['ws_url'] = 'http://.../'

        ### DATA DEFINITION (USERNAME, PASSWORD, ETC)
        ENV['username'] = 'user'
        ENV['password'] = 'pass'

        ### DEVICE = PC
        ENV['APK_ID'] = "1"
        ENV['APK_SECRET'] = "secret"
    end

    def test_user_create

        # User registration test case

        case_user_create
    end

    def test_user_login

        # User login test case

        case_user_login
    end

    def test_content_search

        # Search for content test case

        case_content_search
    end

    def test_utils_channels

        # channels test case

        case_utils_channels
    end

    def test_content_list

        # Content list case

        case_content_list
    end

    def test_user_modify

        # Change user data test case

        case_user_modify
    end

    def test_user_change_suscription

        # Change user suscription test case

        case_user_change_suscription
    end

    def test_user_favorite

        # User profiles test case

        case_user_favorite
    end

    def test_commercial_buyandplay

        # Change user login password test case

        case_commercial_buyandplay
    end

    #def test_content_status
    #   
    #   # Test content status currenttime test case
    #   
    #   case_content_status
    #end

    def teardown

    end

end

And this is a sample of the cases_api_share.rb file:

   require_relative 'obj_api_shared.rb'

def case_user_create

    # User registration test case

    case code...
end

def case_user_login

    # User login test case

    another test code...
end

And but when I run the suite, it didn't run the code in 'cases_api_share':

ruby tests/test_api.rb 
Started with run options --seed 30622


Finished in 0.00055s
0 tests, 0 assertions, 0 failures, 0 errors, 0 skips
Lea2501
  • 311
  • 6
  • 22
  • I'm pretty sure that the relevant code is not present in the question (line 593)... Also in a language which relies heavily on convention over configuration, I suggest you go over your naming conventions (`ApiObject` rather than `Api_object`) – Uri Agassi Feb 15 '16 at 19:19
  • @UriAgassi Thanks for the comment, I have updated the question with the current state of the problem. Thanks! – Lea2501 Feb 15 '16 at 20:44
  • ...you are not extending `MiniTest::Unit::TestCase`... – Uri Agassi Feb 16 '16 at 06:29
  • @UriAgassi I see, do I need to add `extend API_pc_qubit` to the "cases_api_share.rb" and "obj_api_shared.rb" files? – Lea2501 Feb 16 '16 at 12:02
  • or maybe is better to make other classes and instantiate them? – Lea2501 Feb 16 '16 at 12:04
  • Look here: http://ruby-doc.org/stdlib-2.0.0/libdoc/minitest/rdoc/MiniTest.html – Uri Agassi Feb 16 '16 at 12:18

1 Answers1

1

This is probably a duplicated. See here. Based on that, here is an example.

If you want to group test, there is a easy way, just use autorun in the test files and require the test files in the suit.

#suit1.rb
require './test1.rb'

#suit2.rb
require './test1.rb'
require './test2.rb'

#test1.rb
require 'minitest/autorun'
class TestOne < MiniTest::Unit::TestCase
  def test_one
     assert_equal 1, 1
   end
end

#test2.rb
require 'minitest/autorun'
class TestTwo < MiniTest::Unit::TestCase
  def test_two
     assert_equal 2, 2
   end
end

You can run the suit or each test individually:

ruby suit1.rb
ruby suit2.rb
ruby test1.rb
ruby test2.rb

You can get the example code here

Community
  • 1
  • 1
  • How do you add the "def setup" and "def teardown" in these example? So that I could have only one setup block to run before each test case? Because In my example I have only one Class for the whole test suite, but with this example I have one Class for every test case. And I don't understand how to add them in these examples. Thanks in advance! – Lea2501 Apr 09 '16 at 14:49
  • And, if I need to have one "setup" and "teardown" method for each test case, how can I share the code of them between many test cases? – Lea2501 Apr 11 '16 at 14:09