How to Use ZenTest with Ruby

An introduction to using refactoring, unit testing and ZenTest on existing code.
Resources

If you're interested in learning more about refactoring, grab a copy of Refactoring: Improving the Design of Existing Code and take a look at www.refactoring.com. For more information about unit testing, please see c2.com/cgi/wiki?UnitTest www.junit.org/index.htm and www.extremeprogramming.org/rules/unittests.html.

The latest information about Test::Unit and ZenTest can be found on theirs home pages, testunit.talbott.ws and www.zenspider.com/ZSS/Products/ZenTest.

______________________

-- -pate http://on-ruby.blogspot.com

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Concatenate strings with << instead

Roman2K's picture

Avoid concatenating strings using += because it duplicates the receiver string that will remain unused in memory until the GC starts. You should rather use << that modifies the receiver by appending the given string to it. It's also quite faster.

... continuation of the above comment discussing the << operator

Roman2K's picture

(sorry for the truncated comment above) ... You should rather use the double "lower than" operator (cannot be written here or the comment gets cut) that modifies the receiver appending the given string to it and returns the string to allow chained calls. It's also quite faster.

Note that Ryan has moved Zentest to RubyForge...

Tom Copeland's picture

....right here.

Yours,

Tom

Re: How to Use ZenTest with Ruby

Anonymous's picture

I don't think assert(1) is a good thing. That test should fail, imo. Maybe raising something like NotImplementedError would be better.
A pleasant article, anyway, now I'm wondering how cool an integration of ZenTest with FreeRIDE (and the included Refactoring-Browser plugin) could be..

Re: How to Use ZenTest with Ruby

Anonymous's picture

Great idea for another article or two!

Re: How to Use ZenTest with Ruby

Anonymous's picture

ooh -- ZenTest, rrb, and FreeRIDE together? that sounds really good! Rich, Ryan? are you guys listening?

Re: How to Use ZenTest with Ruby

Anonymous's picture

you can try it yourself :)
I wrote a simple test generation plugin long time ago (see DumbTestBuilder on the FR wiki) in an hour, and I had never used the FreeRIDE/FreeBASE api before..

Re: How to Use ZenTest with Ruby

Anonymous's picture

the assert(1) test set my alarm bells ringing, but I see your point...

Test::Unit is Another Good Reason To Use Ruby, but I always find myself copying an existing testsuite and editing because I cant' remember syntax worth a damn, so anything that bootstraps a test*rb creation is worth its weight in gold..

Re: How to Use ZenTest with Ruby

Anonymous's picture

Excellent. I always wondered what was the easiest way of adding unit tests and what you show here is really simple and straightforward. I love it.

Re: How to Use ZenTest with Ruby

Anonymous's picture

Hey, that's excellent! Thanks for the article.

--Gavin Sinclair

Re: How to Use ZenTest with Ruby

Anonymous's picture

Never used ZenTest before, but it seems like a good way to get started with adding unit testing to those old project I started before I learned how to write decently tested code.
I always like helpers that save me a step or two.

Re: How to Use ZenTest with Ruby

Anonymous's picture

I had never looked into ZenTest, but now that I realize it creates Test::Unit tests for you, it does sound like a great way to deal with legacy (i.e., "not written test-first", in TDD parlance) code.

Re: How to Use ZenTest with Ruby

Anonymous's picture

ZenTest is bidirectional, so it is also a great way to do test-first. Write the tests, run them... throw exceptions galore because the classes referenced do not exist. ZenTest will fill in those blanks for you as well.

Re: How to Use ZenTest with Ruby

Anonymous's picture

Very cool article. I often scan over the code with my eyes and see what I am missing and try to do the refactoring manually. I had never heard of Zentest before this article. I will have to check it out....it looks like it'll make my life easier and less error prone. ;)

Re: How to Use ZenTest with Ruby

Anonymous's picture

Wow! ZenTest looks like a really cool way to generate the unit testing scaffolding code. Looks like it takes the tedium out of unit testing.

Very informative.

It would be great to see more Ruby articles like this as well.

thanks for great article,

polypus's picture

thanks for great article, will try zentest

just to point out typo, in last assertion here, no closing paren

def test_name=
aCustomer = Customer.new("Fred Jones")
assert_equal("Fred Jones",aCustomer.name)
aCustomer.name = "Freddy Jones"
assert_equal("Freddy Jones",aCustomer.name
end

can testers use this for functional testing

Anonymous's picture

can testers use this for functional testing?.
(or)
Developers can use it...

pls let me know.if tester can use this, what are the things he wants to know

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix