Monday, June 02, 2008

Unit Testing Ruby on Netbeans IDE

For ruby unit testing, we need to extend from the class "Test::Unit::TestCase?".

Suppose the following "Demo" class exists. It has only one method ie “return_two”.

class Demo

def return_two
2
end
end

The method "return_two" returns the value 2. We could have written "return 2" or just "2" since ruby returns the value of the last line in the method.

Now let us write the class to test the "Demo" class. I have used Netbeans IDE.

By convention, we have to name test classes as "Test+ClassName?". So we have "TestDemo?" as the classname. Here is the ruby code.

$:.unshift File.join(File.dirname(__FILE__),'..','lib')

require 'test/unit'
require 'demo'

class TestDemo < Test::Unit::TestCase

def test_foo
d= Demo.new
assert_equal 2, d. return_two

# assert_equal("foo", bar)
end
def test_fail
d= Demo.new
assert_equal 3, d. return_two
end

end

Now the method "test_foo" tests the method "return_two" of "Demo" class.

In detail:

1. First the class “Demo” is instantiated using “Demo.new”

2. Next, the variable “d” is used to store the reference to the newly instantiated “Demo” object.

3. The method “assert_equal” checks whether the expected value (2) matches with the actual value (d. return_two). Since this matches, the test case will pass.

4. There is another method called “test_fail”. This checks whether the expected value (3) is equal to the actual value (d. return_two). Of course, this method will fail.

When we run the “TestDemo?” program, following output is obtained.

Loaded suite C:\Documents and Settings\manoj_tharayil\My Documents\NetBeansProjects\RubyTesting\lib\test_demo
Started
F.
Finished in 0.131 seconds.

1) Failure:
test_fail(TestDemo) [C:\Documents and Settings\manoj_tharayil\My Documents\NetBeansProjects\RubyTesting\lib\test_demo.rb:14]:
<3> expected but was
<2>.

2 tests, 2 assertions, 1 failures, 0 errors

Running multiple tests – Testsuite

Suppose we have another class “example”.

Here is the code.

class Example
def return_five
5
end
end

The method “return_five” returns 5.

So here is the class “TestExample?” to test the above class.

$:.unshift File.join(File.dirname(__FILE__),'..','lib')

require 'test/unit'
require 'example'

class TestExample < Test::Unit::TestCase

def test_foo
k=Example.new
assert_equal 5, k.return_five,"Success"

end

def test_fail
k=Example.new
assert_equal 6, k.return_five,"Success"

end
end

The sample method “test_foo” would always pass.

The method “test_fail” would always fail.

Here is the output when the “TestExample?” class is run.

Loaded suite C:\Documents and Settings\manoj_tharayil\My Documents\NetBeansProjects\RubyTesting\lib\test_example
Started
F.
Finished in 0.195 seconds.

1) Failure:
test_fail(TestExample) [C:\Documents and Settings\manoj_tharayil\My Documents\NetBeansProjects\RubyTesting\lib\test_example.rb:21]:
Success.
<6> expected but was
<5>.

2 tests, 2 assertions, 1 failures, 0 errors

Now we have two classes

1. TestExample?

2. TestDemo?

We need to run the tests in both the classes. Test suite comes into the picture.

TestSuite? code

Create a test suite class, say “ts_suite”

require 'test/unit' 

# Add your testcases here

require 'test_example'
require 'test_demo'
#require 'tc_3'

The above test suite says

1. First run the test case "test_example".

2. Then run the test case "test_demo".

3. It will not run "tc_3" since it is commented out.

We can go on adding test cases to the test suite as above. Here is the output from the test suite when it is run.

Loaded suite C:\Documents and Settings\manoj_tharayil\My Documents\NetBeansProjects\RubyTesting\lib\ts_suite
Started
F.F.
Finished in 0.17500000000000002 seconds.

1) Failure:
test_fail(TestDemo) [C:/Documents and Settings/manoj_tharayil/My Documents/NetBeansProjects/RubyTesting/lib/../lib/test_demo.rb:14]:
<3> expected but was
<2>.

2) Failure:
test_fail(TestExample) [C:/Documents and Settings/manoj_tharayil/My Documents/NetBeansProjects/RubyTesting/lib/test_example.rb:21]:
Success.
<6> expected but was
<5>.

4 tests, 4 assertions, 2 failures, 0 errors

You can see that there are 2 failures – one in each class.

The failures are “hyperlinks” – when clicked would take us to the exact line in the method of the class which failed.

Happy Testing!!

Sunday, June 01, 2008

Interesting website for books

Just joined goodreads.com


Configuring thunderbird with gmail

Not able to send email from my desktop.

sending a picture from Gwenview opened up "KMail composer" asking me to configure the pop3 account.

Searching in google - for "gmail pop3".

Google supports only following mail clients. ( http://mail.google.com/support/bin/answer.py?answer=12103 )

So "Thunderbird 2.0" it was.

Configuring thunderbird with gmai is given in detail here.

http://mail.google.com/support/bin/answer.py?answer=38343

So fired up Yast and entered thunderbird. Downloading the latest version (around 25 MB).

QT4 - developing application on opensuse linux

There is a cool feature introduced in Windows xp.

If your computer is going to hang, just pull out "task bar" and "End task" the application.

Why don't we have something similar in linux?

I decided to create one.

Which is the language and the IDE we can use here.

My brother Justin suggested QT4.
I researched. He was right.

The remaining blogs would be dedicated to QT4.

Fedora core 9 crashed on amd64 bit.

I spent four days downloading Fedora core 9. It was a whopping 4.5 GB.
Yesterday I tried installing it and it crashed.
The computer just went off. Phit!!!

Reason: Fedora core 9 is using the latest bleeding edge technologies.
Currently Nvidia is not supporting drivers for KDE 4.
Guess what? Fedora core 9 is using KDE 4.

Well, bad luck for the users!!
All you can see is the crash of Fedora core 9.
Dont bother to read the release notes if you are using Nvidia drivers.
See the crash, reboot your machine and install another Linux.

My favourite Linux is Open Suse 10
I love the way these people do everything.
Right from the time, the installer begins to the end - where you install additional software using Yast, everythng is a breeze!!

Do remember NOT to install ZEN Virtualization. It is a real pain in the ass!!

For suse 10.3, they have recongnized the mistake and removed the software from the official release.

Advantages of blogging

We face a lot of issues in our day to day life.

As software engineers, most of them are related to software testing.
We find answers to these problems after much research.

Why not share it in the blog?
People all over the world who face similar problems could benefit from it.


Blogging from Firefox using scribe fire

We could use scribe fire to blog from firefox.

Scribe is available as a plugin from the firefox website.

After installation, you can configure scribe to connect to blogspot.com easily.


Testing with scribe fire

This is a test.