I'm trying to implement shoulda for test automation. I tried this code:
require 'test/unit'
require 'shoulda'
require 'shoulda-context'
class TestClass < Test::Unit::TestCase
context 'Languages' do
should 'Ruby' do
puts 'Ruby'
end
should 'Java' do
puts 'Java'
end
should 'Python' do
puts 'Python'
end
end
end
When I execute that code, it outputs according to alphabetical order of shoulda test methods:
Java
Python
Ruby
Actually, I want the output in the order I wrote the methods:
Ruby
Java
Python
To do that, what will I have to use in my code?
Actually it is preferred not to run test in a specific order. It is better to run them in a random order, because that allows you to recognize if there are tests that depend on other tests. If tests only pass in a specific order, it is a indicator that you are doing something wrong.
If you want to do that nevertheless:
class TestClass < Test::Unit::TestCase
context 'Languages' do
self.test_order = :defined
...
See: http://test-unit.rubyforge.org/test-unit/en/Test/Unit/TestCase.html#test_order=-class_method
Related
I have a requirement,
Simplecov should print this line
Coverage report generated ...
only if the test suite is green.
Is this possible?
Here's my initializer
SimpleCov.start do
add_filter "/test/"
coverage_dir "/tmp/coverage/"
end
There is a at_exit hook in SimpleCov where you can skip the message
SimpleCov.at_exit do
SimpleCov.result.format! if all_test_passed
end
but could not find a workaround for 'all_test_passed'. Did not find a way to get summary so that i can check if all tests passed.
this is my first time using RubyMine and I can't make the Test Framework recognize the tests.
If I run the greed.rb file it shows the result of the tests:
But if I run the tests it shows this:
I have read the question about minitest and tried to apply it to test-unit but it did not worked.
This is the content of my Gemfile:
gem 'test-unit', '~> 3.0.8'
And this is part of the ruby class that contains the tests:
require 'test/unit'
def score(dice)
# code of the function
end
class Greed < Test::Unit::TestCase
def test_score_of_an_empty_list_is_zero
assert_equal 0, score([])
end
end
I'm probably missing something but I haven't been able to find a solution.
To solve it I firstly restructured my project in the following way
/lib
greed.rb
/tests
test_greed.rb
Gemfile
Then I moved all of my tests to the new file
require './lib/greed.rb'
require 'test/unit'
class Test_Greed < Test::Unit::TestCase
# code for the tests
end
Finally I went to File/Setting/Project/Project Structure and marked the tests folder as Test (this should change the color of the folder icon).
Now I can run the tests using the testing framework
I'm getting the following error when I try to run my cucumber tests in my (local) Jenkins:
/Users/kris/.rvm/rubies/ruby-1.9.3-p545/bin/rake --rakefile android/rakefile
(in /)
rake aborted!
cannot load such file -- cucumber
My rakefile looks like the following:
require 'cucumber'
require 'cucumber/rake/task'
Cucumber::Rake::Task.new(:features) do |t|
t.profile = 'ci'
end
task :default => :features
I'm just trying to set up a really basic run, it looks like I need to either set up a plugin for cucumber (which I thought I had done) - or set some variable to make cucumber available to Jenkins.
Thanks for any help/suggestions!
So I've just recently started writing some Ruby and whilst I understand how modules work, the following behaviour still throws me off.
module ModuleA
def a_greet
'Hello from module A'
end
end
module ModuleB
def b_greet
'Hello from module B'
end
end
include ModuleA
include ModuleB
# WHY DOES THIS WORK !!!!!
p ModuleA.b_greet
I get that the functions from the modules can be called without specifying Module. and that I'd never write code in this way, but I cannot understand why you can call a method included from ModuleB when explicitly stating ModuleA?
Wait, there's more:
"Why does this work?".b_greet # => "Hello from module B"
You're including those modules in a top-level object main. It's a special object: all methods defined on it become available to all objects (see the line above, there's now b_greet method on a String). ModuleA is an object too, so, when you include ModuleB, ModuleA gets its methods. If you include those modules in a regular class/object, you won't get this "sharing" behaviour.
If I have the following project structure
project/
lib/
subproject/
a.rb
b.rb
lib.rb
where lib.rb looks like this :-
module Subproject
def foo
do_some_stuff
end
end
and a.rb and b.rb both need to mixin some methods within lib.rb and are both namespaced within a module like so :-
require 'subproject/lib'
module Subproject
class A
include Subproject
def initialize()
foo()
end
end
end
What does ruby do when it encounters the include statement? How does it know that I want to only include the mixin from lib.rb rather than the whole module which includes both class A and class B, is this based purely on the require of subproject/lib or am I getting it wrong and it is including the whole of the module, including the definitions of Class A and B within themselves?
It is including the whole of the module. There is only one Subproject module, you've just reopened it in a.rb and b.rb and added classes A and B to it. I don't think require is related anyhow there.
BTW, you can always try it out in irb:
>> Subproject::A
=> Subproject::A
>> Subproject::A::A
=> Subproject::A