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

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

Related

I want to call def setup method before all tests in minitest 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

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

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

With Test::Unit, how can I run a bit of code before all tests (but not each test)?

In my test app, which uses test::unit, I need to start by pulling a bunch of data from various sources. I'd like to only do this once - the data is only read, not written, and doesn't change between tests, and the loading (and error checking for the loading), takes some time.
There are values that I DO want reset every time, and those are easy enough, but what if I want persistant accessible values? What's the best way to do this?
I'm especially interested in solutions that would let my push those assignments to some module that can be included in all my tests, since they all need access to this data.
Why do you need it inside the test? You could define it gloabl:
gem 'test-unit'#, '>= 2.1.1' #startup
require 'test/unit'
GLOBAL_DATA = 11
class My_Tests < Test::Unit::TestCase
def test_1()
puts "Testing startup 1"
assert_equal(11, GLOBAL_DATA)
end
end
GLOBAL_DATA could be a (singleton)-class (respective an instance).
If you have only one testclass, you may use TestCase.startup:
gem 'test-unit'#, '>= 2.1.1' #startup
require 'test/unit'
class My_Tests < Test::Unit::TestCase
def self.startup
puts "Define global_data "
##global_data = 11
end
def test_1()
puts "Testing 1"
assert_equal(11, ##global_data = 11)
end
def test_2()
puts "Testing 2"
assert_equal(11, ##global_data = 11)
end
end
You can just put them at the top of the class. They will get executed, and then your tests will get executed.
You could do this in the setup method:
def setup
if !defined?(##initial_data)
# Whatever you need to do to get your initial data
##initial_data = foo
end
#other_data = bar
end

Resources