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

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState