# Taming Test::Unit

**URL:** <https://rubytalk.org/t/taming-test-unit/11937>\
**Category:** ruby-talk\
**Created:** [4 June 2004 15:51 UTC](https://rubytalk.org/t/taming-test-unit/11937 "2004-06-04T15:51:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ben\_Giddings1](https://avatars.discourse-cdn.com/v4/letter/b/779978/32.png) [@Ben\_Giddings1](https://rubytalk.org/u/Ben_Giddings1)\
**Post date:** [4 June 2004 15:51 UTC](https://rubytalk.org/t/taming-test-unit/11937/1 "2004-06-04T15:51:55Z")

</div>

Recently I've been trying to use Ruby's Test::Unit framework to test a network server. When I do things a certain way, it works great. It runs all my tests and gives the expecte results. However there are a few things I don't understand how to do.

Right now, I have setup and teardown methods that make the TCP connection to the server. If these are simple like:

&nbsp;&nbsp;&nbsp;def setup  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@sock = TCPSocket.new($host, $port)  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def teardown  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@sock.close  
&nbsp;&nbsp;&nbsp;end

Then everything works. However, I'd also like to be able to keep the connection open while each test runs. Each method I try to do this fails, and I'm not quite sure why.

I first tried:

&nbsp;&nbsp;&nbsp;def setup  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if @sock.nil?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@sock = TCPSocket.new($host, $port)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

And commenting out the teardown method, figuring that the socket would be created only once, and that when the program exited it would take care of closing the socket, but instead what happens is that the first test works, but the for subsequent tests, @sock is nil, and because the socket is never closed, TCPSocket.new fails with a connection refused method. Does anybody know why @sock would become nil?

I also tried defining an 'initialize' method to set up the socket at the start of the test case, but evidently 'initialize' is used internally in some sneaky way I don't understand.

So, could someone who understands the unit test framework better than me explain how I can:

\* Pass a hostname and port as commandline parameters to a script  
\* Create a socket connected to that host/port  
\* Run a series of tests using that socket  
\* Disconnect the socket

Ideally, without using ugly things like global variables, etc.?

Also, in an oviously related question: how can I prevent unit tests from running just because my file happens to define a class deriving from Test::Unit::TestCase?

Thanks,

Ben

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [4 June 2004 20:55 UTC](https://rubytalk.org/t/taming-test-unit/11937/2 "2004-06-04T20:55:20Z")

</div>

Ben Giddings wrote:

> Recently I've been trying to use Ruby's Test::Unit framework to test a network server. When I do things a certain way, it works great. It runs all my tests and gives the expecte results. However there are a few things I don't understand how to do.
> 
> Right now, I have setup and teardown methods that make the TCP connection to the server. If these are simple like:
> 
> &nbsp;&nbsp;def setup  
> &nbsp;&nbsp;&nbsp;&nbsp;@sock = TCPSocket.new($host, $port)  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def teardown  
> &nbsp;&nbsp;&nbsp;&nbsp;@sock.close  
> &nbsp;&nbsp;end
> 
> Then everything works. However, I'd also like to be able to keep the connection open while each test runs. Each method I try to do this fails, and I'm not quite sure why.
> 
> I first tried:
> 
> &nbsp;&nbsp;def setup  
> &nbsp;&nbsp;&nbsp;&nbsp;if @sock.nil?  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@sock = TCPSocket.new($host, $port)  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end
> 
> And commenting out the teardown method, figuring that the socket would be created only once, and that when the program exited it would take care of closing the socket, but instead what happens is that the first test works, but the for subsequent tests, @sock is nil, and because the socket is never closed, TCPSocket.new fails with a connection refused method. Does anybody know why @sock would become nil?

IIRC, a new instance is created for each test\_XXX method, and setup and teardown are called on that instance. So @sock is nil because setup was called on a different instance.

> I also tried defining an 'initialize' method to set up the socket at the start of the test case, but evidently 'initialize' is used internally in some sneaky way I don't understand.
> 
> So, could someone who understands the unit test framework better than me explain how I can:
> 
> \* Pass a hostname and port as commandline parameters to a script  
> \* Create a socket connected to that host/port  
> \* Run a series of tests using that socket  
> \* Disconnect the socket
> 
> Ideally, without using ugly things like global variables, etc.?

Class variables? I wish I had a better answer. Sometimes Test::Unit doesn't fit the testing patterns I want to use...

---

<div class="post-metadata">

**Author:** ![Nathaniel\_Talbott1](https://avatars.discourse-cdn.com/v4/letter/n/a4c791/32.png) [@Nathaniel\_Talbott1](https://rubytalk.org/u/Nathaniel_Talbott1)\
**Post date:** [5 June 2004 02:47 UTC](https://rubytalk.org/t/taming-test-unit/11937/3 "2004-06-05T02:47:34Z")

</div>

> Recently I've been trying to use Ruby's Test::Unit framework to test a network server. When I do things a certain way, it works great. It runs all my tests and gives the expecte results. However there are a few things I don't understand how to do.

Hopefully I can help out a bit...

> And commenting out the teardown method, figuring that the socket would be created only once, and that when the program exited it would take care of closing the socket, but instead what happens is that the first test works, but the for subsequent tests, @sock is nil, and because the socket is never closed, TCPSocket.new fails with a connection refused method. Does anybody know why @sock would become nil?
> 
> I also tried defining an 'initialize' method to set up the socket at the start of the test case, but evidently 'initialize' is used internally in some sneaky way I don't understand.

As Joel said, each test method runs within the context of a \_new\_ instance of a test class. Thus you're starting out with a clean slate every time. This is by design, since it is usually best to test each piece of functionality in isolation to prevent side-effects from creating false positives. The one place where this is not practical is when testing resources are expensive; then it makes sense to only set things up once (a fact you're obviously already acquainted with).

> So, could someone who understands the unit test framework better than me explain how I can:
> 
> \* Pass a hostname and port as commandline parameters to a script  
> \* Create a socket connected to that host/port  
> \* Run a series of tests using that socket  
> \* Disconnect the socket
> 
> Ideally, without using ugly things like global variables, etc.?

There are several ways, none of them satisfactory. First of all, as Joel already mentioned, lazy initialize the resource and stuff it in a class variable (he problematic thing becomes disconnecting it when you're done - I don't have a good solution for this). Another way is to build your suites manually, and override TestSuite#run to call suite-level setup and teardown methods.

While I'm quite busy right now, I'll be finishing up the project that I'm on at the end of the month, and I plan to give Test::Unit some much-needed TLC. So please be patient with me... I haven't forgotten it 🙂

[P.S. Test::Unit won't touch any command-line arguments after a '--' in the list, so you should be able to just read the hostname and port out of the ARGV array.]

> Also, in an oviously related question: how can I prevent unit tests from running just because my file happens to define a class deriving from Test::Unit::TestCase?

Well, I can't really see how it's related, but the best way I know of is to not subclass TestCase except when you want the subclass to be run. Instead, mix-in common functionality from a Module.

HTH,

Nathaniel  
Terralien, Inc.

\<:((\>\<

> **···**
>
> On Jun 4, 2004, at 10:51, Ben Giddings wrote:

---

<div class="post-metadata">

**Author:** ![Bret\_Pettichord1](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@Bret\_Pettichord1](https://rubytalk.org/u/Bret_Pettichord1)\
**Post date:** [10 June 2004 06:49 UTC](https://rubytalk.org/t/taming-test-unit/11937/4 "2004-06-10T06:49:40Z")

</div>

> There are several ways, none of them satisfactory. First of all, as Joel already mentioned, lazy initialize the resource and stuff it in a class variable (he problematic thing becomes disconnecting it when you're done - I don't have a good solution for this).

What about putting the teardown in an END block?

> Another way is to build your suites manually, and override TestSuite#run to call suite-level setup and teardown methods.

I like this suggestion. It might be nice to do this automatically in future versions of test-unit.

> [P.S. Test::Unit won't touch any command-line arguments after a '--' in the list, so you should be able to just read the hostname and port out of the ARGV array.]

Another approach is to do this:

# ARGV need to be deleted to enable the Test::Unit functionatily that grabs  
# the remaining ARGV as a filter on what tests to run  
$HIDE\_IE = ARGV.include?('-b'); ARGV.delete('-b')

> **···**
>
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> &nbsp;&nbsp;Bret Pettichord, Software Tester  
> &nbsp;&nbsp;&nbsp;&nbsp;Consultant - [www.pettichord.com](http://www.pettichord.com)  
> &nbsp;&nbsp;&nbsp;&nbsp;Author - [www.testinglessons.com](http://www.testinglessons.com)  
> &nbsp;&nbsp;&nbsp;&nbsp;Blogger - [www.io.com/~wazmo/blog](http://www.io.com/~wazmo/blog)
> 
> &nbsp;&nbsp;&nbsp;&nbsp;Scripting Web Tests Tutorial  
> &nbsp;&nbsp;&nbsp;&nbsp;XP/Agile Universe, Aug 15, Calgary  
> &nbsp;&nbsp;&nbsp;&nbsp;www.pettichord.com/training.html
