# Has anyone combined Test::Unit with CGI?

**URL:** <https://rubytalk.org/t/has-anyone-combined-test-unit-with-cgi/669>\
**Category:** ruby-talk\
**Created:** [15 July 2002 02:35 UTC](https://rubytalk.org/t/has-anyone-combined-test-unit-with-cgi/669 "2002-07-15T02:35:25Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dossy](https://avatars.discourse-cdn.com/v4/letter/d/b9bd4f/32.png) [@Dossy](https://rubytalk.org/u/Dossy)\
**Post date:** [15 July 2002 02:35 UTC](https://rubytalk.org/t/has-anyone-combined-test-unit-with-cgi/669/1 "2002-07-15T02:35:25Z")

</div>

Doing some Test::Unit work on my CGI scripts, I wondered if anyone  
had done any work combining Test::Unit with CGI?

Specifically, I’m thinking:

Create your Test::Unit test case file. Just as it auto-magically  
runs the test runner by simply requiring ‘test/unit’ and defining  
a class that inherits from Test::Unit::TestCase, I’m thinking if  
you require ‘cgi’ – well, if a class named “CGI” is defined – that  
the Test::Unit test runner generates its output as a HTML  
document.

Perhaps this is too magical: if your testing a CGI script, it’ll  
have a CGI class defined and you may not want the output in HTML.  
So, maybe defining a flag would be necessary.

I’m just thinking of an easy way of turning a Test::Unit test  
case (or suite) into something that can be published to the web  
… or, in my case, run the test cases as a CGI script – each hit  
to the CGI runs the test cases and sends the results as HTML  
back to the browser.

If anyone has already _done_ this, please speak up! I’d love to  
use whatever you’ve got. Otherwise, I’m going to start working  
on this now.

– Dossy

> **···**
>
> –  
> Dossy Shiobara mail: [dossy@panoptic.com](mailto:dossy@panoptic.com)  
> Panoptic Computer Network web: [http://www.panoptic.com/](http://www.panoptic.com/)  
> “He realized the fastest way to change is to laugh at your own  
> folly – then you can let go and quickly move on.” (p. 70)

---

<div class="post-metadata">

**Author:** ![Jim\_Freeze2](https://avatars.discourse-cdn.com/v4/letter/j/e480ec/32.png) [@Jim\_Freeze2](https://rubytalk.org/u/Jim_Freeze2)\
**Post date:** [15 July 2002 03:13 UTC](https://rubytalk.org/t/has-anyone-combined-test-unit-with-cgi/669/2 "2002-07-15T03:13:36Z")

</div>

I’ve heard of a webunit, but I have not had the chance  
to use it yet.

> **···**
>
> On Mon, Jul 15, 2002 at 11:35:25AM +0900, Dossy wrote:
> 
> > Doing some Test::Unit work on my CGI scripts, I wondered if anyone  
> > had done any work combining Test::Unit with CGI?
> 
> –  
> Jim Freeze  
> If only I had something clever to say for my comment…  
> ~

---

<div class="post-metadata">

**Author:** ![Patrick\_May](https://avatars.discourse-cdn.com/v4/letter/p/48db29/32.png) [@Patrick\_May](https://rubytalk.org/u/Patrick_May)\
**Post date:** [15 July 2002 08:22 UTC](https://rubytalk.org/t/has-anyone-combined-test-unit-with-cgi/669/3 "2002-07-15T08:22:43Z")

</div>

Dossy [dossy@panoptic.com](mailto:dossy@panoptic.com) wrote in message news:[20020715023521.GH24646@panoptic.com](mailto:20020715023521.GH24646@panoptic.com)…

> Just as it auto-magically  
> runs the test runner by simply requiring ‘test/unit’ and defining  
> a class that inherits from Test::Unit::TestCase, I’m thinking if  
> you require ‘cgi’ – well, if a class named “CGI” is defined – that  
> the Test::Unit test runner generates its output as a HTML  
> document.

Would something like this work:

print “Content-type: text/html\r\n”  
print “\r\n”  
print “

```
”

… run tests, do output here …

print “
```
”

It would make your shell run test output uglier, but I think this  
should work online. Alternatively, you could have two seperate entry  
points to the application, one of which was a cgi:

run\_my\_tests.rb  
cgi\_run\_my\_tests.rb

I don’t think it’s simple to autodetect whether a script is running  
within a cgi enviroment. Look at the code for the QueryExtension  
module within cgi for the enviroment checking code.

~ Patrick

---

<div class="post-metadata">

**Author:** ![Dossy](https://avatars.discourse-cdn.com/v4/letter/d/b9bd4f/32.png) [@Dossy](https://rubytalk.org/u/Dossy)\
**Post date:** [15 July 2002 03:23 UTC](https://rubytalk.org/t/has-anyone-combined-test-unit-with-cgi/669/4 "2002-07-15T03:23:55Z")

</div>

I’m not talking of combining them in that way.

I’m talking more like a test/unit/ui/web/testrunner.rb … which I’ve  
got something that “works” for me now already … no unit tests for  
it, though. Just a mad copy-and-paste from  
test/unit/ui/console/testrunner.rb and test/unit.rb …

– Dossy

> **···**
>
> On 2002.07.15, Jim Freeze [jim@freeze.org](mailto:jim@freeze.org) wrote:
> 
> > On Mon, Jul 15, 2002 at 11:35:25AM +0900, Dossy wrote:
> > 
> > > Doing some Test::Unit work on my CGI scripts, I wondered if anyone  
> > > had done any work combining Test::Unit with CGI?
> > 
> > I’ve heard of a webunit, but I have not had the chance  
> > to use it yet.
> 
> –  
> Dossy Shiobara mail: [dossy@panoptic.com](mailto:dossy@panoptic.com)  
> Panoptic Computer Network web: [http://www.panoptic.com/](http://www.panoptic.com/)  
> “He realized the fastest way to change is to laugh at your own  
> folly – then you can let go and quickly move on.” (p. 70)

---

<div class="post-metadata">

**Author:** ![Holden\_Glova](https://avatars.discourse-cdn.com/v4/letter/h/57b2e6/32.png) [@Holden\_Glova](https://rubytalk.org/u/Holden_Glova)\
**Post date:** [15 July 2002 07:49 UTC](https://rubytalk.org/t/has-anyone-combined-test-unit-with-cgi/669/5 "2002-07-15T07:49:29Z")

</div>

I think webunit is a different beast - it is modeled tightly off httpunit  
(java testing framework for testing http responses IIRC).

* * *

Signed,  
Holden Glova

> **···**
>
> On Mon, 15 Jul 2002 15:13, Jim Freeze wrote:
> 
> > On Mon, Jul 15, 2002 at 11:35:25AM +0900, Dossy wrote:
> > 
> > > Doing some Test::Unit work on my CGI scripts, I wondered if anyone  
> > > had done any work combining Test::Unit with CGI?
> > 
> > I’ve heard of a webunit, but I have not had the chance  
> > to use it yet.

---

<div class="post-metadata">

**Author:** ![Dossy](https://avatars.discourse-cdn.com/v4/letter/d/b9bd4f/32.png) [@Dossy](https://rubytalk.org/u/Dossy)\
**Post date:** [15 July 2002 11:10 UTC](https://rubytalk.org/t/has-anyone-combined-test-unit-with-cgi/669/6 "2002-07-15T11:10:22Z")

</div>

> Would something like this work:
> 
> print “Content-type: text/html\r\n”  
> print “\r\n”  
> print “
> 
> ```
> ”
> 
> … run tests, do output here …
> 
> print “
> ```
> ”

This is what the initial version of mine did. Of course,

```
isn’t
enough – you really want because the expected and actual strings
are guarded with “< … >” and therefore those < and > need to be
HTML escaped as < and > … unless you use …

Now, where my code is at … can pretty much handle everything the  
right way, IMHO. 😉

> I don’t think it’s simple to autodetect whether a script is running  
> within a cgi enviroment. Look at the code for the QueryExtension  
> module within cgi for the enviroment checking code.

I test for ENV[‘GATEWAY\_INTERFACE’].nil? which is Good Enough for me.

– Dossy

> **···**
>
> On 2002.07.15, Patrick May [patrick-may@monmouth.com](mailto:patrick-may@monmouth.com) wrote:
> 
> –  
> Dossy Shiobara mail: [dossy@panoptic.com](mailto:dossy@panoptic.com)  
> Panoptic Computer Network web: [http://www.panoptic.com/](http://www.panoptic.com/)  
> “He realized the fastest way to change is to laugh at your own  
> folly – then you can let go and quickly move on.” (p. 70)
```

---

<div class="post-metadata">

**Author:** ![Dossy](https://avatars.discourse-cdn.com/v4/letter/d/b9bd4f/32.png) [@Dossy](https://rubytalk.org/u/Dossy)\
**Post date:** [15 July 2002 03:51 UTC](https://rubytalk.org/t/has-anyone-combined-test-unit-with-cgi/669/7 "2002-07-15T03:51:49Z")

</div>

If anyone is keen to try my changes out …

See what it does here:

```
http://panoptic.com/ruby/test_unit/tc_web_testrunner.cgi

```

See how it does it here:

```
http://panoptic.com/ruby/test_unit/tc_web_testrunner.cgi.txt
http://panoptic.com/ruby/test_unit/web_testrunner.rb.txt

```

Yes, it’s just mostly copy-and-paste. Yes, I don’t have unit  
tests for my changes (yet). Yes, I crammed this together in  
probably under an hour – it could use lots of cleaning up, and  
some snazzier styling to boot.

I may be inclined to clean things up if there’s interest from  
anyone but myself. 😉

– Dossy

> **···**
>
> On 2002.07.15, Dossy [dossy@panoptic.com](mailto:dossy@panoptic.com) wrote:
> 
> > On 2002.07.15, Jim Freeze [jim@freeze.org](mailto:jim@freeze.org) wrote:
> > 
> > > On Mon, Jul 15, 2002 at 11:35:25AM +0900, Dossy wrote:
> > > 
> > > > Doing some Test::Unit work on my CGI scripts, I wondered if anyone  
> > > > had done any work combining Test::Unit with CGI?
> > > 
> > > I’ve heard of a webunit, but I have not had the chance  
> > > to use it yet.
> > 
> > I’m not talking of combining them in that way.
> > 
> > I’m talking more like a test/unit/ui/web/testrunner.rb … which I’ve  
> > got something that “works” for me now already … no unit tests for  
> > it, though. Just a mad copy-and-paste from  
> > test/unit/ui/console/testrunner.rb and test/unit.rb …
> 
> –  
> Dossy Shiobara mail: [dossy@panoptic.com](mailto:dossy@panoptic.com)  
> Panoptic Computer Network web: [http://www.panoptic.com/](http://www.panoptic.com/)  
> “He realized the fastest way to change is to laugh at your own  
> folly – then you can let go and quickly move on.” (p. 70)

---

<div class="post-metadata">

**Author:** ![R\_Bryant](https://avatars.discourse-cdn.com/v4/letter/r/9de053/32.png) [@R\_Bryant](https://rubytalk.org/u/R_Bryant)\
**Post date:** [15 July 2002 14:15 UTC](https://rubytalk.org/t/has-anyone-combined-test-unit-with-cgi/669/8 "2002-07-15T14:15:42Z")

</div>

What about using…

print “Content-type: text/plain\r\n”  
print “\r\n”

Would that work?

> **···**
>
> On Mon, Jul 15, 2002 at 08:10:22PM +0900, Dossy wrote:
> 
> > On 2002.07.15, Patrick May [patrick-may@monmouth.com](mailto:patrick-may@monmouth.com) wrote:
> > 
> > > Would something like this work:
> > > 
> > > print “Content-type: text/html\r\n”  
> > > print “\r\n”  
> > > print “
> > > 
> > > ```
> > > ”
> > > 
> > > … run tests, do output here …
> > > 
> > > print “
> > > ```
> > > ”
> > 
> > This is what the initial version of mine did. Of course,
> > 
> > ```
> > isn’t
> > enough – you really want because the expected and actual strings
> > are guarded with “< … >” and therefore those < and > need to be
> > HTML escaped as < and > … unless you use …
> > ```
> 
> –
> 
> Rob | I prefer plaintext email…
> 
> “Erm, I don’t want to expose too much of my code, as I’m writing it  
> fairly plainly at present and I’m sure to be told how all of my  
> routines can be compressed down into one line with no alphanumeric  
> characters in them.” [Ross Clement in comp.lang.perl.misc]

---

<div class="post-metadata">

**Author:** ![Eric\_Hodel1](https://avatars.discourse-cdn.com/v4/letter/e/a9adbd/32.png) [@Eric\_Hodel1](https://rubytalk.org/u/Eric_Hodel1)\
**Post date:** [15 July 2002 20:18 UTC](https://rubytalk.org/t/has-anyone-combined-test-unit-with-cgi/669/9 "2002-07-15T20:18:32Z")

</div>

[http://www.w3.org/TR/REC-html40/appendix/changes.html#h-A.3.1](http://www.w3.org/TR/REC-html40/appendix/changes.html#h-A.3.1)

Obsolete elements

The following elements are obsolete: LISTING, PLAINTEXT, and XMP. For  
all of them, authors should use the PRE element instead.

> **···**
>
> Dossy ([dossy@panoptic.com](mailto:dossy@panoptic.com)) wrote:
> 
> > This is what the initial version of mine did. Of course,
> > 
> > ```
> > isn’t
> > enough – you really want because the expected and actual strings
> > are guarded with “< … >” and therefore those < and > need to be
> > HTML escaped as < and > … unless you use …
> > ```
> 
> –  
> Eric Hodel - [drbrain@segment7.net](mailto:drbrain@segment7.net) - [http://segment7.net](http://segment7.net)  
> All messages signed with fingerprint:  
> FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04
