I want to call def setup method before all tests in minitest ruby - ruby

This is my code
class TestLogin < MiniTest::Test
def setup
#driver=Selenium::WebDriver.for :firefox
#driver.manage.window.maximize
#driver.navigate.to "http://google.com"
end
def test_case1
puts "testcase1"
end
def test_case2
puts "testcase2"
end
end
I want to run setup method only once for two testcases at the starting.

You can use minitest-hooks gem with before_all something like:
require "minitest/autorun"
require 'minitest/hooks/test'
class TestLogin < MiniTest::Test
include Minitest::Hooks
def before_all
puts "setup .."
end
def test_case1
puts "testcase1"
end
def test_case2
puts "testcase2"
end
end
Now when you run the test you should see something like:
Run options: --seed 58346
# Running:
setup ..
testcase1
.testcase2
.
Finished in 0.001259s, 1588.7504 runs/s, 0.0000 assertions/s.
2 runs, 0 assertions, 0 failures, 0 errors, 0 skips

Related

Writing a test for a case statement in Ruby

I'm trying to write a test for a case statement using minitest. Would I need to write separate tests for each "when"? I included my code below. Right now it just puts statements, but eventually it's going to redirect users to different methods. Thanks!
require 'pry'
require_relative 'messages'
class Game
attr_reader :user_answer
def initialize(user_answer = gets.chomp.downcase)
#user_answer = user_answer
end
def input
case user_answer
when "i"
puts "information"
when "q"
puts "quitter"
when "p"
puts "player play"
end
end
end
This answer will help you. Nonetheless I'll post one way of applying it to your situation. As suggested by #phortx when initializing a game, override the default user-input with the relevant string. Then by using assert_output we can do something like:
#test_game.rb
require './game.rb' #name and path of your game script
require 'minitest/autorun' #needed to run tests
class GameTest < MiniTest::Test
def setup
#game_i = Game.new("i") #overrides default user-input
#game_q = Game.new("q")
#game_p = Game.new("p")
end
def test_case_i
assert_output(/information\n/) {#game_i.input}
end
def test_case_q
assert_output(/quitter\n/) {#game_q.input}
end
def test_case_p
assert_output(/player play\n/) {#game_p.input}
end
end
Running the tests...
$ ruby test_game.rb
#Run options: --seed 55321
## Running:
#...
#Finished in 0.002367s, 1267.6099 runs/s, 2535.2197 assertions/s.
#3 runs, 6 assertions, 0 failures, 0 errors, 0 skips
You have to test each case branch. Via RSpec it would work that way:
describe Game do
subject { Game }
describe '#input' do
expect_any_instance_of(Game).to receive(:puts).with('information')
Game.new('i').input
expect_any_instance_of(Game).to receive(:puts).with('quitter')
Game.new('q').input
expect_any_instance_of(Game).to receive(:puts).with('player play')
Game.new('p').input
end
end
However due the fact that puts is ugly to test, you should refactor your code to something like that:
require 'pry'
require_relative 'messages'
class Game
attr_reader :user_answer
def initialize(user_answer = gets.chomp.downcase)
#user_answer = user_answer
end
def input
case user_answer
when "i"
"information"
when "q"
"quitter"
when "p"
"player play"
end
end
def print_input
puts input
end
end
Then you can test with RSpec via:
describe Game do
subject { Game }
describe '#print_input' do
expect_any_instance_of(Game).to receive(:puts).with('quitter')
Game.new('q').print_input
end
describe '#input' do
expect(Game.new('i').input).to eq('information')
expect(Game.new('q').input).to eq('quitter')
expect(Game.new('i').input).to eq('player play')
expect(Game.new('x').input).to eq(nil)
end
end

How to create HTML test report with Ruby by using test-unit-runner-html

I am working on Ruby with Selenium and Watir. I have implemented Test-Unit, now I have a need to implement HTML Test Report. I have gone through test-unit-runner-html, but not so lucky. Can somebody help me implementing test-unit-runner-html with my project. Suppose there is a suite containing different test cases and I have to implement on it. Here is the sample test suite:
require 'rubygems'
gem 'test-unit'
require 'test-unit'
require 'test/unit/runner/html'
class TestExample < Test::Unit::TestCase
def setup
puts "running the setup........."
#number1 = 4
#number2 = 2
end
def test_add
puts "asserting the add function: "
numb1=#number1+#number2
assert_equal(7, numb1 + 1, "added correctly")
end
def test_sub
puts "asserting the subtract function"
numb2=#number1-#number2
assert_equal(2, numb2 - 0, "subtracted correctly")
end
def teardown
puts "running teardown......."
#number = nil
end
end

Webdriver with Ruby and Minitest. Share test cases code between test suites

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
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

Minitest - Tests Don't Run - No Rails

I'm just starting a small project to emulate a Carnival's ticket sales booth and one of the guidelines was to test that a user can enter the number of tickets. The program runs in the console and I eventually (hopefully) figured it out how to implement this test thanks to #Stefan's answer on this question.
The problem is that now, when I run the test file, minitest says:
0 runs, 0 assertions, 0 failures, 0 errors, 0 skips
I get the same result when I try to run the test by name using ruby path/to/test/file.rb --name method-name. I'm not sure if this is because my code is still faulty of if it's because I've set up minitest incorrectly. I've tried to look up similar problems on SO but most questions seem to involve using minitest with rails and I just have a plain ruby project.
Here's my test file:
gem 'minitest', '>= 5.0.0'
require 'minitest/spec'
require 'minitest/autorun'
require_relative 'carnival'
class CarnivalTest < MiniTest::Test
def sample
assert_equal(1, 1)
end
def user_can_enter_number_of_tickets
with_stdin do |user|
user.puts "2"
assert_equal(Carnival.new.get_value, "2")
end
end
def with_stdin
stdin = $stdin # global var to remember $stdin
$stdin, write = IO.pipe # assign 'read end' of pipe to $stdin
yield write # pass 'write end' to block
ensure
write.close # close pipe
$stdin = stdin # restore $stdin
end
end
In a file called carnival.rb in the same folder as my test file I have
Class Carnival
def get_value
gets.chomp
end
end
If anyone can help figure out why the test is not running I'd be grateful!
By convention, tests in Minitest are public instance methods that start with test_, so the original test has no actual test methods. You need to update your test class so that the methods with assertions follow the convention as:
class CarnivalTest < Minitest::Test
def test_sample
assert_equal(1, 1)
end
def test_user_can_enter_number_of_tickets
with_stdin do |user|
user.puts "2"
assert_equal(Carnival.new.get_value, "2")
end
end
# snip...
end
Yeah always start all your tests with test_ so it knows that you want to that function/method
class CarnivalTest < MiniTest::Test
def test_sample
assert_equal(1, 1)
end
def test_user_can_enter_number_of_tickets
with_stdin do |user|
user.puts "2"
assert_equal(Carnival.new.get_value, "2")
end
end
and that should work for you

How assertion counts are calculated in test unit

Method 1:-
test.rb
class Test < Test::Unit::TestCase
def test_sample
assert_true(test)
assert_equal(a,b)
end
end
Result:-
Finished in 38.329532529 seconds.
1 tests, 2 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
Method 2:-
test.rb
class Test < Test::Unit::TestCase
require 'helper'
include AssertionHelper
def test_sample
test_assertion
end
end
helper.rb
include Test::Unit::Assertions
module AssertionHelper
def test_assertion
assert_true(test)
assert_equal(a,b)
end
end
Result:-
Finished in 38.329532529 seconds.
1 tests, 2 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
Method 3:-
test.rb
class Test < Test::Unit::TestCase
require 'helper'
def test_sample
AssertionHelper.test_assertion()
end
end
helper.rb
include Test::Unit::Assertions
module AssertionHelper
def self.test_assertion
assert_true(test)
assert_equal(a,b)
end
end
Result:-
Finished in 38.329532529 seconds.
1 tests, 0 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
When using Method 3, I am getting assertion count as "0" instead of "2".
Is it possible for me to get assertion count as 2 using Method 2 ?
You can pass your current TestCase to your module, like this:
sample_test.rb:
require 'test-unit'
require 'helper'
def a; true ; end
def b; true ; end
def test; true ; end
class SampleTest < Test::Unit::TestCase
def test_sample
AssertionHelper.my_assertion(self)
end
end
helper.rb:
module AssertionHelper
def self.my_assertion(test_case)
test_case.instance_exec do
assert_true(test)
assert_equal(a, b)
end
end
end
Sorry, but I can't reproduce your situation, could you please provide Test::Unit version and your ruby version? Best of all would be your Gemfile with Gemfile.lock. The following setup works for me (I use ruby 2.2.0 and test-unit 3.0.8):
ruby-2.2.0 in ~/projects/test-unit ♥ tree
.
├── Gemfile
├── Gemfile.lock
└── test
├── helper.rb
└── sample_test.rb
1 directory, 4 files
ruby-2.2.0 in ~/projects/test-unit ♥ cat Gemfile
# A sample Gemfile
source "https://rubygems.org"
# gem "rails"
gem 'test-unit', '~> 3.0.8'
ruby-2.2.0 in ~/projects/test-unit ♥ cat Gemfile.lock
GEM
remote: https://rubygems.org/
specs:
power_assert (0.2.2)
test-unit (3.0.8)
power_assert
PLATFORMS
ruby
DEPENDENCIES
test-unit (~> 3.0.8)
sample_test.rb:
require 'test-unit'
def a; true ; end
def b; true ; end
def test; true ; end
class SampleTest < Test::Unit::TestCase
require 'helper'
include AssertionHelper
def test_sample
my_assertion
end
end
helper.rb:
module AssertionHelper
def my_assertion
assert_true(test)
assert_equal(a, b)
end
end
Running testrb gives 2 assertions, as expected.
ruby-2.2.0 in ~/projects/test-unit ♥ testrb
Loaded suite .
Started
.
Finished in 0.000828 seconds.
1 tests, 2 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
1207.73 tests/s, 2415.46 assertions/s
ruby-2.2.0 in ~/projects/test-unit ♥
UPDATE: This is actually strange that you don't get any error (on your method 3), because I get this: NoMethodError: undefined method 'assert_true' for AssertionHelper:Module and this is true, since AssertionHelper doesn't implement any other methods, you can't run any assert_* methods on it. Just use my code above (your method 2) and you should be fine. If you're still curious what can be done, have a look at Test::Unit::Assertions, there's also a lot of built-in assertions defined, maybe you find that useful.
Or, better, use MiniTest or RSpec, since Test::Unit is deprecated and is left in standard library only for legacy test suites.

Resources